Тема: Минималистичное переключение меню TDA7419
Здравствуйте, использую данный скетч. Могли бы Вы сделать так чтобы второе меню открывалось при долгом нажатии на кнопку энкодера. Делаю усилитель в небольшом корпусе и хочется сделать панель без кнопок, только дисплей и энкодер. Буду благодарен, если можно что-то тут изменить.
#include <Wire.h>
#include <TDA7419.h>
#include <EEPROM.h>//#include <EEPROMex.h>
#include <LiquidCrystal.h>
#include <Encoder.h>
#include <MsTimer2.h>
Encoder myEnc(9, 8);//CLK, DT
TDA7419 tda; LiquidCrystal lcd(7, 6, 2, 3, 4, 5);// RS,E,D4,D5,D6,D7
int menu,vol,bas,w,w1,vol_d,bass_d,treb,treble_d,middle,middle_d,z,in,gain0,gain1,gain2,gain3,gain_d,set;
int lon, lon_f, lon_d,out,vol_out,rf,lf,rt,lt,sab,treb_c,mid_c,bas_c,sab_f,mute=1,vol_ram;
unsigned long time, oldPosition = -999,time1;
byte a1[8]={0b00000,0b11011,0b11011,0b11011,0b11011,0b11011,0b11011,0b00000};
byte a2[8]={0b00000,0b11000,0b11000,0b11000,0b11000,0b11000,0b11000,0b00000};
void setup(){
lcd.begin(16, 2);lcd.createChar(0,a1); lcd.createChar(1,a2);Wire.begin();Serial.begin(9600);
pinMode(13,INPUT);// set
pinMode(12,INPUT);// меню
pinMode(11,INPUT);// плюс
pinMode(10,INPUT);// минус
pinMode(A0,INPUT);// КНОПКА ЭНКОДЕРА
vol = EEPROM.read(0);treb = EEPROM.read(1)-15;middle = EEPROM.read(3)-15;bas = 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);
MsTimer2::set(1, to_Timer);MsTimer2::start();
audio();
}
long newPosition;
void to_Timer(){ //функция таймера
newPosition = myEnc.read()/4;
}
void loop(){
if((analogRead(A0)<900||digitalRead(12)==HIGH)&&menu<4){menu++;cl();myEnc.write(0);time=millis();w1=1;if(menu>3){menu=0;}}// меню
if(digitalRead(12)==HIGH&&menu>=4){menu++;cl();myEnc.write(0);time=millis();w1=1;if(menu>10){menu=0;}}// меню 2
if(digitalRead(12)==HIGH&&digitalRead(13)==HIGH){menu=4;cl();}
if(digitalRead(10)==HIGH&&digitalRead(11)==HIGH){mute++;w=1;if(mute>1){mute=0;}}
//////////////////////////////////////// Volume -80 ... 0 дБ //////////////////////////////////////
if(menu==0){
if (newPosition != oldPosition) {
oldPosition = newPosition;
vol=vol+newPosition;myEnc.write(0);newPosition=0;audio();lcd.clear();time=millis();w1=1;if(vol>80){vol=80;}if(vol<0){vol=0;}}
if(digitalRead(11)==HIGH){vol++;w=1;if(vol>80){vol=80;}}// 80 максимальная громкость
if(digitalRead(10)==HIGH){vol--;w=1;if(vol<0){vol=0;}}// 0 минимум громкости
if(w==1){audio();cl();time=millis();w=0;w1=1;}
lcd.setCursor(0,0);if(mute==1){lcd.print("Volume ");}else{lcd.print("MUTE ");}
lcd.print(-80+vol);lcd.setCursor(13,0);lcd.print("dB");vol_d=vol/2-17;
if(vol_d>=0){for(z=0;z<=vol_d;z++){lcd.setCursor(z/2,1);lcd.write((uint8_t)0);}}
if((vol_d)%2==0){lcd.setCursor(z/2,1);lcd.write((uint8_t)1);}
lcd.setCursor(13,1);lcd.print(vol);}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////// индикация тембр ВЧ + управление кнопками -15...15 дБ ///////////////////
if(menu==1){
if (newPosition != oldPosition) {
oldPosition = newPosition;
treb=treb+newPosition;myEnc.write(0);newPosition=0;audio();lcd.clear();time=millis();w1=1;if(treb>15){treb=15;}if(treb<-15){treb=-15;}}
if(digitalRead(11)==HIGH){treb++;w=1;if(treb>15){treb=15;}}
if(digitalRead(10)==HIGH){treb--;w=1;if(treb<-15){treb=-15;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
lcd.setCursor(0,0);lcd.print("Treble ");
lcd.print(treb);lcd.setCursor(13,0);lcd.print("dB");treble_d=treb+15;
if(treble_d>=0){for(z=0;z<=treble_d;z++){lcd.setCursor(z/2,1);lcd.write((uint8_t)0);}}
if((treble_d)%2==0){lcd.setCursor(z/2,1);lcd.write((uint8_t)1);}}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////// индикация тембр CЧ + управление кнопками -15...15 дБ ///////////////////
if(menu==2){
if (newPosition != oldPosition) {
oldPosition = newPosition;
middle=middle+newPosition;myEnc.write(0);newPosition=0;audio();lcd.clear();time=millis();w1=1;if(middle>15){middle=15;}if(middle<-15){middle=-15;}}
if(digitalRead(11)==HIGH){middle++;w=1;if(middle>15){middle=15;}}
if(digitalRead(10)==HIGH){middle--;w=1;if(middle<-15){middle=-15;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
lcd.setCursor(0,0);lcd.print("Middle ");
lcd.print(middle);lcd.setCursor(13,0);lcd.print("dB");middle_d=middle+15;
if(middle_d>=0){for(z=0;z<=middle_d;z++){lcd.setCursor(z/2,1);lcd.write((uint8_t)0);}}
if((middle_d)%2==0){lcd.setCursor(z/2,1);lcd.write((uint8_t)1);}}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////// индикация тембр НЧ + управление кнопками -15...15 дБ ///////////////////
if(menu==3){
if (newPosition != oldPosition) {
oldPosition = newPosition;
bas=bas+newPosition;myEnc.write(0);newPosition=0;audio();lcd.clear();time=millis();w1=1;if(bas>15){bas=15;}if(bas<-15){bas=-15;}}
if(digitalRead(11)==HIGH){bas++;w=1;if(bas>15){bas=15;}}
if(digitalRead(10)==HIGH){bas--;w=1;if(bas<-15){bas=-15;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
lcd.setCursor(0,0);lcd.print("Bass ");
lcd.print(bas);lcd.setCursor(13,0);lcd.print("dB");bass_d=bas+15;
if(bass_d>=0){for(z=0;z<=bass_d;z++){lcd.setCursor(z/2,1);lcd.write((uint8_t)0);}}
if((bass_d)%2==0){lcd.setCursor(z/2,1);lcd.write((uint8_t)1);}}
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// вход 1...3 + предусилитель 0...15 дБ //////////////////////////////
if(menu==4){ //рег. предусилителя - нажать и удерживать кнопку set, рег. кнопки плюс и минус
if(digitalRead(11)==HIGH&&digitalRead(13)==LOW){in++;w=1;if(in>3){in=3;}}
if(digitalRead(10)==HIGH&&digitalRead(13)==LOW){in--;w=1;if(in<1){in=1;}}
if(in==1){gain0=gain1;}if(in==2){gain0=gain2;}if(in==3){gain0=gain3;}
if(digitalRead(13)==HIGH&&digitalRead(11)==HIGH){gain0++;w=1;if(gain0>15){gain0=15;}}
if(digitalRead(13)==HIGH&&digitalRead(10)==HIGH){gain0--;w=1;if(gain0<0){gain0=0;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
lcd.setCursor(0,0);lcd.print("Input: ");lcd.print(in);gain_d=gain0;
if(gain_d>=0){for(z=0;z<=gain_d;z++){lcd.setCursor(z/2,1);lcd.write((uint8_t)0);}}
if((gain_d)%2==0){lcd.setCursor(z/2,1);lcd.write((uint8_t)1);}
if(in==1){gain1=gain0;}if(in==2){gain2=gain0;}if(in==3){gain3=gain0;}
lcd.setCursor(11,1);lcd.print(gain0);lcd.print(" dB ");}
/////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// loudness ////////////////////////////////////////////////////
if(menu==5){
if(digitalRead(11)==HIGH&&digitalRead(13)==LOW){lon_f++;w=1;if(lon_f>3){lon_f=3;}}
if(digitalRead(10)==HIGH&&digitalRead(13)==LOW){lon_f--;w=1;if(lon_f<0){lon_f=0;}}
if(digitalRead(13)==HIGH&&digitalRead(11)==HIGH){lon++;w=1;if(lon>15){lon=15;}}
if(digitalRead(13)==HIGH&&digitalRead(10)==HIGH){lon--;w=1;if(lon<0){lon=0;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}lon_d=lon;
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;}
lcd.setCursor(0,1);
if(lon_d>=0){for(z=0;z<=lon_d;z++){lcd.setCursor(z/2,1);lcd.write((uint8_t)0);}}
if((lon_d)%2==0){lcd.setCursor(z/2,1);lcd.write((uint8_t)1);}
lcd.setCursor(9,1);lcd.print("-");lcd.print(lon);lcd.print(" dB ");}
////////////////////////////////////////// out /////////////////////////////////////////////////
if(menu==6){
if(digitalRead(11)==HIGH&&digitalRead(13)==LOW){out++;w=1;if(out>4){out=4;}}
if(digitalRead(10)==HIGH&&digitalRead(13)==LOW){out--;w=1;if(out<0){out=0;}}
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(digitalRead(13)==HIGH&&digitalRead(11)==HIGH){vol_out++;w=1;if(vol_out>15){vol_out=15;}}
if(digitalRead(13)==HIGH&&digitalRead(10)==HIGH){vol_out--;w=1;if(vol_out<0){vol_out=0;}}
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;}
lcd.print(" dB ");if(w==1){audio();cl();time=millis();w1=1;w=0;}}
/////////////////////////////////treble center//////////////////////////////////////////////////
if(menu==7){
if(digitalRead(11)==HIGH){treb_c++;w=1;if(treb_c>3){treb_c=3;}}
if(digitalRead(10)==HIGH){treb_c--;w=1;if(treb_c<0){treb_c=0;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
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==8){
if(digitalRead(11)==HIGH){mid_c++;w=1;if(mid_c>3){mid_c=3;}}
if(digitalRead(10)==HIGH){mid_c--;w=1;if(mid_c<0){mid_c=0;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
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==9){
if(digitalRead(11)==HIGH){bas_c++;w=1;if(bas_c>3){bas_c=3;}}
if(digitalRead(10)==HIGH){bas_c--;w=1;if(bas_c<0){bas_c=0;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
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==10){
if(digitalRead(11)==HIGH){sab_f++;w=1;if(sab_f>3){sab_f=3;}}
if(digitalRead(10)==HIGH){sab_f--;w=1;if(sab_f<0){sab_f=0;}}
if(w==1){audio();cl();time=millis();w1=1;w=0;}
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 && w1==1){
EEPROM.update(0,vol);EEPROM.update(1,treb+15);EEPROM.update(2,middle+15);EEPROM.update(3,bas+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;w1=0; myEnc.write(0);cl();}
}//loop
void cl(){delay(300);lcd.clear();}
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(bas,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);
}