Re: TDA7419 + энкодер + ИК пульт + анализатор спектра
спасибо все работает, буду подключать к 7419 пробовать
Старое подключение кнопок с подтягивающими резисторами
#include <Wire.h> #include <TDA7419.h> #include <EEPROM.h> #include <LiquidCrystal_I2C.h> #include <Encoder.h> #include <MsTimer2.h> #include <boarddefs.h> #include <IRremote.h> TDA7419 tda; IRrecv irrecv(12); // указываем вывод модуля IR приемника Encoder myEnc(9, 8);//CLK, DT decode_results ir; LiquidCrystal_I2C lcd(0x27,16,2);// A4,A5 boolean w,w2,www,mute=1,gr1,gr2; byte z,z0,z1,gain1,gain2,gain3,gain_d,rf,lf,rt,lt,sab, in_d,lon_f,lon_d,vol_d, bass_d,treb_d,middle_d; int menu,vol,bass,treb,middle,in,gain0; int lon,out,vol_out,treb_c,mid_c,bas_c,sab_f; unsigned long time,oldPosition = -999,newPosition; unsigned long gainTimer; byte a1[8] = {0b00000,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b00000}; byte a2[8] = {0b00000,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b00000}; byte a3[8] = {0b00000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b00000}; void setup(){ Serial.begin(9600);Wire.begin(); lcd.init();lcd.backlight(); irrecv.enableIRIn(); // запускаем модуль IR lcd.setCursor(0,0);lcd.print(" TDA7419 ");delay(1000);// ЗАСТАВКА lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3); pinMode(10,INPUT);// МЕНЮ КНОПКА SW энкодера pinMode(11,INPUT);// ВЫБОР ВХОДА КНОПКА IN pinMode(13,INPUT);// КНОПКА SET MsTimer2::set(3, to_Timer);MsTimer2::start(); // АКТИВИРУЕМ ПРЕРЫВАНИЯ ДЛЯ ЭНКОДЕРА if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// очистка памяти при первом включении /// считываем все настройки с памяти при включении vol = EEPROM.read(0);treb = EEPROM.read(1)-15;middle = EEPROM.read(3)-15;bass = EEPROM.read(3)-15; in = EEPROM.read(4);gain1 = EEPROM.read(5);gain2 = EEPROM.read(6);gain3 = EEPROM.read(7); lon = EEPROM.read(8);lon_f = EEPROM.read(9);rf = EEPROM.read(10);lf = EEPROM.read(11); rt = EEPROM.read(12);lt = EEPROM.read(13);sab = EEPROM.read(14);treb_c = EEPROM.read(15); mid_c = EEPROM.read(16);bas_c = EEPROM.read(17);sab_f = EEPROM.read(18); switch(in){ case 1: gain0 = gain1;break; case 2: gain0 = gain2;break; case 3: gain0 = gain3;break; } audio(); } void loop(){ if(digitalRead(10)==LOW){menu++;cl();time=millis();w=1;w2=1;www=1;if(menu>4){menu=0;}}// меню if(digitalRead(11)==HIGH&&menu<5){in++;;cl();time=millis()-7000;w=1;w2=1;www=1;menu=4;if(in>3){in=1;};audio();}// КНОПКА ВЫБОРА ВХОДА IN if(digitalRead(13)==HIGH&&menu<5){menu=5;cl();time=millis();w=1;w2=1;}// меню 2 включение меню 2 if(digitalRead(13)==HIGH&&menu>=5){menu++;cl();time=millis();w=1;w2=1;if(menu>11){menu=0;}}// перебор меню 2 и выход в меню громкости ////////////////// ИК приемник - обработка кодов кнопок /////////////////////////////////////// if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();time=millis();w=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок if(ir.value==0x2FDD02F){menu++;gr1=0;gr2=0;cl();time=millis();w=1;w2=1;if(menu>4){menu=0;}}// меню кнопка вверх if(ir.value==0x2FD32CD){menu--;gr1=0;gr2=0;cl();time=millis();w=1;w2=1;if(menu<0){menu=4;}}// меню кнопка вниз if(ir.value==0x2FD708F&&menu<5){in++;cl();time=millis()-7000;w=1;w2=1;www=1;menu=4;if(in>3){in=1;};gr1=0;gr2=0;audio();}// IN /////////////////////////////// VOLUME -80 ... 0 дБ //////////////////////////////////////////////// if(menu==0){ if(ir.value==0x2FD906F){vol++;gr1=1;gr2=0;cl();w2=1;vol_func();audio();}// кнопка > if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl();w2=1;vol_func();audio();}// кнопка >>>>>> if(ir.value==0x2FDF20D){vol--;gr1=0;gr2=1;cl();w2=1;vol_func();audio();}// кнопка < if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl();w2=1;vol_func();audio();}// кнопка <<<<<< if (newPosition != oldPosition){oldPosition = newPosition; vol=vol+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;vol_func();audio();} lcd.setCursor(0,0);lcd.print("Volume "); lcd.print(" ");lcd.print(-80+vol);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");vol_d=map(vol, 0, 80, 0,48); if(w2==1){ for(z=0,z0=0,z1=0;z<=vol_d;z++,z1++){if(z1>2){z1=0;z0++;} if(z1==1){lcd.setCursor(z0,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1,1);lcd.print(" ");}} if(z1==3){lcd.setCursor(z0,1);lcd.write((uint8_t)1);} if(z1==2){lcd.setCursor(z0,1);lcd.write((uint8_t)2);}w2=0;}} ///////////////////////// TREBLE +/-15 dB ///////////////////////////////////////////////////////// if(menu==1){ if(ir.value==0x2FD906F){treb++;gr1=1;gr2=0;cl();w2=1;treb_func();audio();}// кнопка > if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl();w2=1;treb_func();audio();}// кнопка >>>>>> if(ir.value==0x2FDF20D){treb--;gr1=0;gr2=1;cl();w2=1;treb_func();audio();}// кнопка < if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl();w2=1;treb_func();audio();}// кнопка <<<<<< if (newPosition != oldPosition) { oldPosition = newPosition; treb=treb+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;treb_func();audio();} lcd.setCursor(0,0);lcd.print("Treble "); lcd.print(" ");lcd.print(treb);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");treb_d=treb+15; if(w2==1){ for(z=0,z0=0,z1=0;z<=treb_d;z++,z1++){if(z1>2){z1=0;z0++;} if(z1==1){lcd.setCursor(z0+3,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1+3,1);lcd.print(" ");}} if(z1==3){lcd.setCursor(z0+3,1);lcd.write((uint8_t)1);} if(z1==2){lcd.setCursor(z0+3,1);lcd.write((uint8_t)2);}w2=0;}} /////////////////////////// MIDDLE +/-15 dB //////////////////////////////////////////////////////// if(menu==2){ if(ir.value==0x2FD906F){middle++;gr1=1;gr2=0;cl();w2=1;middle_func();audio();}// кнопка > if(ir.value==0xFFFFFFFF and gr1==1){middle++;gr2=0;cl();w2=1;middle_func();audio();}// кнопка >>>>>> if(ir.value==0x2FDF20D){middle--;gr1=0;gr2=1;cl();w2=1;middle_func();audio();}// кнопка < if(ir.value==0xFFFFFFFF and gr2==1){middle--;gr1=0;cl();w2=1;middle_func();audio();}// кнопка <<<<<< if (newPosition != oldPosition) { oldPosition = newPosition; middle=middle+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;middle_func();audio();} lcd.setCursor(0,0);lcd.print("Middle "); lcd.print(" ");lcd.print(middle);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");middle_d=middle+15; if(w2==1){ for(z=0,z0=0,z1=0;z<=middle_d;z++,z1++){if(z1>2){z1=0;z0++;} if(z1==1){lcd.setCursor(z0+3,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1+3,1);lcd.print(" ");}} if(z1==3){lcd.setCursor(z0+3,1);lcd.write((uint8_t)1);} if(z1==2){lcd.setCursor(z0+3,1);lcd.write((uint8_t)2);}w2=0;}} /////////////////////// BASS +/-15 dB /////////////////////////////////////////////////////////// if(menu==3){ if(ir.value==0x2FD906F){bass++;gr1=1;gr2=0;cl();w2=1;bass_func();audio();}// кнопка > if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl();w2=1;bass_func();audio();}// кнопка >>>>>> if(ir.value==0x2FDF20D){bass--;gr1=0;gr2=1;cl();w2=1;bass_func();audio();}// кнопка < if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl();w2=1;bass_func();audio();}// кнопка <<<<<< if (newPosition != oldPosition) { oldPosition = newPosition; bass=bass+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;bass_func();audio();} lcd.setCursor(0,0);lcd.print("Bass "); lcd.print(" ");lcd.print(bass);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");bass_d=bass+15; if(w2==1){ for(z=0,z0=0,z1=0;z<=bass_d;z++,z1++){if(z1>2){z1=0;z0++;} if(z1==1){lcd.setCursor(z0+3,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1+3,1);lcd.print(" ");}} if(z1==3){lcd.setCursor(z0+3,1);lcd.write((uint8_t)1);} if(z1==2){lcd.setCursor(z0+3,1);lcd.write((uint8_t)2);}w2=0;}} //////////////// IN + GAIN ///////////////////////////////////////////////////////////// if(menu==4){ switch(in){ case 1: gain0 = gain1;break; case 2: gain0 = gain2;break; case 3: gain0 = gain3;break; } if(ir.value==0x2FD906F){gain0++;gr1=1;gr2=0;cl();w2=1;gain_func();audio();}// кнопка > if(ir.value==0xFFFFFFFF and gr1==1){gain0++;gr2=0;cl();w2=1;gain_func();audio();}// кнопка >>>>>> if(ir.value==0x2FDF20D){gain0--;gr1=0;gr2=1;cl();w2=1;gain_func();audio();}// кнопка < if(ir.value==0xFFFFFFFF and gr2==1){gain0--;gr1=0;cl();w2=1;gain_func();audio();}// кнопка <<<<<< if (newPosition != oldPosition) { oldPosition = newPosition; gain0=gain0+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;gain_func();audio();} switch(in){ case 1: gain1 = gain0;break; case 2: gain2 = gain0;break; case 3: gain3 = gain0;break; } if(www==1){audio();www=0;} lcd.setCursor(0,0);lcd.print("Gain IN ");lcd.print(in); lcd.setCursor(10,0);lcd.print(" ");lcd.print(gain0);lcd.print(" ");lcd.setCursor(14,0);lcd.print("dB");gain_d=gain0*2; if(w2==1){ for(z=0,z0=0,z1=0;z<=gain_d;z++,z1++){if(z1>2){z1=0;z0++;} if(z1==1){lcd.setCursor(z0+3,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1+3,1);lcd.print(" ");}} if(z1==3){lcd.setCursor(z0+3,1);lcd.write((uint8_t)1);} if(z1==2){lcd.setCursor(z0+3,1);lcd.write((uint8_t)2);}}w2=0;} ////////////////////////////// loudness //////////////////////////////////////////////////// if(menu==5){ if (newPosition != oldPosition) { oldPosition = newPosition; lon_f=lon_f+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;if(lon_f>3){lon_f=0;}if(lon_f<0){lon_f=3;}audio();} lcd.setCursor(0,0);lcd.print("Loudness: "); switch(lon_f){ case 0: lcd.print("FLAT ");break; case 1: lcd.print("400Hz ");break; case 2: lcd.print("800Hz ");break; case 3: lcd.print("2400Hz");break;}} //////////////////////////// loudness Att //////////////////////////////////////////////// if(menu==6){ if (newPosition != oldPosition) { oldPosition = newPosition; lon=lon+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;if(lon>15){lon=15;}if(lon<0){lon=0;}audio();} lcd.setCursor(0,0);lcd.print("Loud att"); lcd.print(" ");lcd.print(-lon);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");lon_d=lon*2; if(w2==1){ for(z=0,z0=0,z1=0;z<=lon_d;z++,z1++){if(z1>2){z1=0;z0++;} if(z1==1){lcd.setCursor(z0+3,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1+3,1);lcd.print(" ");}} if(z1==3){lcd.setCursor(z0+3,1);lcd.write((uint8_t)1);} if(z1==2){lcd.setCursor(z0+3,1);lcd.write((uint8_t)2);}w2=0;}} ////////////////////////////////////////// out ///////////////////////////////////////////////// if(menu==7){ if(digitalRead(11)==HIGH){out++;cl();www=1;w=1;if(out>4){out=0;}audio();} // перебор выходов кнопкой IN switch(out){ case 0:vol_out=rf;break; case 1:vol_out=lf;break; case 2:vol_out=rt;break; case 3:vol_out=lt;break; case 4:vol_out=sab;break; } if (newPosition != oldPosition) { oldPosition = newPosition; vol_out=vol_out+newPosition;myEnc.write(0);newPosition=0;time=millis();www=1;w=1;w2=1;if(vol_out>15){vol_out=15;}if(vol_out<0){vol_out=0;}audio();} lcd.setCursor(0,0);lcd.print("Volume OUT");lcd.setCursor(0,1); switch(out){ case 0:rf=vol_out;lcd.print("RF: +");lcd.print(rf);break; case 1:lf=vol_out;lcd.print("LF: +");lcd.print(lf);break; case 2:rt=vol_out;lcd.print("RT: +");lcd.print(rt);break; case 3:lt=vol_out;lcd.print("LT: +");lcd.print(lt);break; case 4:sab=vol_out;lcd.print("SUBW: +");lcd.print(sab);break; } if(www==1){audio();www=0;} lcd.print(" dB ");} /////////////////////////////////treble center////////////////////////////////////////////////// if(menu==8){ if (newPosition != oldPosition) { oldPosition = newPosition; treb_c=treb_c+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;if(treb_c>3){treb_c=0;}if(treb_c<0){treb_c=3;}audio();} lcd.setCursor(0,0);lcd.print("Treble Center "); lcd.setCursor(1,6); switch(treb_c){ case 0: lcd.print("10.0 kHz");break; case 1: lcd.print("12.5 kHz");break; case 2: lcd.print("15.0 kHz");break; case 3: lcd.print("17.5 kHz");break;}} /////////////////////////////////middle center////////////////////////////////////////////////// if(menu==9){ if (newPosition != oldPosition) { oldPosition = newPosition; mid_c=mid_c+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;if(mid_c>3){mid_c=0;}if(mid_c<0){mid_c=3;}audio();} lcd.setCursor(0,0);lcd.print("Middle Center "); lcd.setCursor(1,6); switch(mid_c){ case 0: lcd.print("0.5 kHz");break; case 1: lcd.print("1.0 kHz");break; case 2: lcd.print("1.5 kHz");break; case 3: lcd.print("2.5 kHz");break;}} ////////////////////////////////////bass center/////////////////////////////////////////// if(menu==10){ if (newPosition != oldPosition) { oldPosition = newPosition; bas_c=bas_c+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;if(bas_c>3){bas_c=0;}if(bas_c<0){bas_c=3;}audio();} lcd.setCursor(0,0);lcd.print("Bass Center "); lcd.setCursor(1,6); switch(bas_c){ case 0: lcd.print("60 Hz ");break; case 1: lcd.print("80 Hz ");break; case 2: lcd.print("100 Hz");break; case 3: lcd.print("200 Hz");break;}} //////////////////////////////////////subw//////////////////////////////////////////////// if(menu==11){ if (newPosition != oldPosition) { oldPosition = newPosition; sab_f=sab_f+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;if(sab_f>3){sab_f=0;}if(sab_f<0){sab_f=3;}audio();} lcd.setCursor(0,0);lcd.print("Subwoofer Freq."); lcd.setCursor(1,6); switch(sab_f){ case 0: lcd.print("FLAT ");break; case 1: lcd.print("80 Hz ");break; case 2: lcd.print("120 Hz");break; case 3: lcd.print("160 Hz");break;}} ////////////////////////////////////////////////////////////////////////////////////// Serial.println(time); if(millis()-time>10000 && w==1){ EEPROM.update(0,vol);EEPROM.update(1,treb+15);EEPROM.update(2,middle+15);EEPROM.update(3,bass+15); EEPROM.update(4,in);EEPROM.update(5,gain1);EEPROM.update(6,gain2);EEPROM.update(7,gain3); EEPROM.update(8,lon);EEPROM.update(9,lon_f); EEPROM.update(10,rf);EEPROM.update(11,lf);EEPROM.update(12,rt);EEPROM.update(13,lt); EEPROM.update(14,sab);EEPROM.update(15,treb_c);EEPROM.update(16,mid_c);EEPROM.update(17,bas_c); EEPROM.update(18,sab_f); menu=0;lcd.clear();w=0;w2=1;cl();} }//loop void gain_func(){if(gain0<0){gain0=0;}if(gain0>15){gain0=15;}} void bass_func(){if(bass>15){bass=15;}if(bass<-15){bass=-15;}} void middle_func(){if(middle>15){middle=15;}if(middle<-15){middle=-15;}} void treb_func(){if(treb>15){treb=15;}if(treb<-15){treb=-15;}} void vol_func(){if(vol<0){vol=0;}if(vol>80){vol=80;}} void cl(){ir.value=0;delay(300);lcd.clear();} void to_Timer(){newPosition = myEnc.read()/4;} void audio(){ tda.setInput(in,gain0,0); //вх1=1, вх2=2, вх3=3 tda.setInput2(0,0,0); // не используется tda.setAtt_loudness(lon,lon_f,0,0); tda.setSoft(mute,0,0,0,0);//mute off (не менять) tda.setVolume(vol-79,0); // общая громкость от 0 до 79 макс tda.setAtt_LF(64+lf,0); // гром. лк фронт tda.setAtt_RF(64+rf,0); // гром. пк фронт tda.setAtt_LT(64+lt,0); // гром. лк тыл tda.setAtt_RT(64+rt,0); // гром. пк тыл tda.setAtt_SUB(64+sab,0); // гром. савбуфер tda.setAtt_Mix(79,0); // не использует tda.setFilter_Treble(treb,treb_c,0); tda.setFilter_Middle(middle,0,0); tda.setFilter_Bass(bass,0,0); tda.setSub_M_B(sab_f,mid_c,bas_c,0,0); tda.setMix_Gain_Eff(1,0,0,0,0); // не используется tda.setSpektor(0,0,1,0,1,0,0); }