#define IR_1 0x2FDD02F // Кнопка вверх
#define IR_2 0x2FD32CD // Кнопка вниз
#define IR_3 0x2FD906F // Кнопка >
#define IR_4 0x2FDF20D // Кнопка <
#define IR_5_1 0x2FD629D // Кнопка IN1
#define IR_5_2 0x2FD22DD // Кнопка IN2
#define IR_6 0x2FDB24D // Кнопка MUTE
#define IR_7 0x2FD00FF // POWER
#include <Wire.h>
#include <TDA8425.h>
#include <Encoder.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <MsTimer2.h>
#include <boarddefs.h>
#include <IRremote.h>
#include <DS3231.h>
LiquidCrystal_I2C lcd(0x27,16,2);
DS3231 clock;// Связываем объект clock с библиотекой DS3231
RTCDateTime DateTime;// Определяем сущность структуры RTCDateTime (описанной в библиотеке DS3231) для хранения считанных с часов даты и времени
IRrecv irrecv(9); // указываем вывод модуля IR приемника
Encoder myEnc(11, 10);//CLK, DT
decode_results ir;
TDA8425 tda;
byte a1[8]={0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101};
byte a2[8]={0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100};
byte a3[8]={0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000};
int menu,vol,vol_d,balans,bass,bass_d,treb,treb_d,in,mute,sss;
byte w,w2,standby=1,www,z,z0,z1,gr1,gr2,t;
unsigned long time,oldPosition = -999,newPosition,tik;
int minut,hour,date,mont,corr,x,reg_time,vvv;
void setup() {
Serial.begin(9600);lcd.init();lcd.backlight();clock.begin();// Инициализируем работу с объектом библиотеки DS3231
irrecv.enableIRIn(); // запускаем модуль IR
pinMode(12,INPUT);// меню кнопка энкодера SW
pinMode(A0,INPUT);// кнопка POWER
pinMode(8,OUTPUT);// выход управления STANDBY
pinMode(7,OUTPUT);// выход управления MUTE
pinMode(A1,INPUT);// MUTE
pinMode(2,INPUT); // корр. перебор
pinMode(3,INPUT); // корр.
digitalWrite(8,LOW);
lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);
vol = EEPROM.read(0);// vol eeprom
bass = EEPROM.read(1);// bass eeprom
treb = EEPROM.read(2);// treb eeprom
in = EEPROM.read(3);// in eeprom
balans = EEPROM.read(4)-4;// balans eeprom
lcd.setCursor(4,0);lcd.print("TDA8425");delay(1000);
MsTimer2::set(3, to_Timer);MsTimer2::start();
// clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча
audio();
}
void loop() {delay(10);DateTime = clock.getDateTime(); // Считываем c часов текущие значения даты и времени в сущность DateTime
if(standby==1&®_time==0){menu=100;ds3231();lcd.setCursor(0,1);lcd.print(" POWER OFF ");delay(1000);lcd.noBacklight();}
if(analogRead(A0)>900&&standby==1){standby=0;lcd.clear();lcd.setCursor(0,0);lcd.backlight();lcd.print(" POWER ON ");digitalWrite(8,HIGH);delay(2000);cl();menu=0;w2=1;}
if(analogRead(A0)>900&&standby==0){standby=1;lcd.clear();digitalWrite(8,LOW);delay(500);menu=100;}
if(analogRead(A1)>900&&mute==0&&standby==0){mute=1;cl1();menu=100;w=0;lcd.setCursor(0,0);lcd.print(" MUTE ");delay(500);}
if(analogRead(A1)>900&&mute==1&&standby==0){mute=0;cl1();delay(100);menu=0;w2=1;}
if(mute==1){w=0;}
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);time=millis();delay(50);if(t==1){lcd.clear();}t=0;w=1;w2=1;irrecv.resume();}// IR приемник
if(digitalRead(12)==LOW&&standby==0){menu++;cl1();myEnc.write(0);time=millis();w=1;w2=1;if(menu>4){menu=0;}}// меню
if(ir.value==IR_1&&standby==0){menu++;gr1=0;gr2=0;cl1();time=millis();w=1;w2=1;if(menu>4){menu=0;}}// IR меню кнопка вверх
if(ir.value==IR_2&&standby==0){menu--;gr1=0;gr2=0;cl1();time=millis();w=1;w2=1;if(menu<0){menu=4;}}// IR меню кнопка вниз
if(ir.value==IR_7&&standby==1){standby=0;gr1=0;gr2=0;lcd.clear();cl();lcd.setCursor(0,0);lcd.backlight();lcd.print(" POWER ON ");digitalWrite(8,HIGH);delay(2000);cl();menu=0;w2=1;}//IR power on
if(ir.value==IR_7&&standby==0){standby=1;gr1=0;gr2=0;lcd.clear();cl();digitalWrite(8,LOW);delay(500);menu=100;}//IR power off
if(ir.value==IR_6&&mute==0&&standby==0){mute=1;cl1();menu=100;w=0;lcd.setCursor(0,0);lcd.print(" MUTE ");delay(500);}// IR MUTE
if(ir.value==IR_6&&mute==1&&standby==0){mute=0;cl1();menu=0;w2=1;}//IR MUTE
if(ir.value==IR_5_1&&standby==0){in=0;gr1=0;gr2=0;cl1();time=millis();w=1;w2=1;lcd.setCursor(0,0);lcd.print(" INPUT1 ");audio();delay(2000);}// IN 1
if(ir.value==IR_5_2&&standby==0){in=1;gr1=0;gr2=0;cl1();time=millis();w=1;w2=1;lcd.setCursor(0,0);lcd.print(" INPUT2 ");audio();delay(2000);}// IN 2
if(mute==1){
digitalWrite(7,LOW);
lcd.setCursor(1,1);
if(DateTime.hour<10){lcd.print("0");}lcd.print(DateTime.hour);lcd.print(":");
if(DateTime.minute<10){lcd.print("0");}lcd.print(DateTime.minute);lcd.print(":");
if(DateTime.second<10){lcd.print("0");}lcd.print(DateTime.second);lcd.print(" ");
if(DateTime.day<10){lcd.print("0");}lcd.print(DateTime.day);lcd.print("-");
if(DateTime.month<10){lcd.print("0");}lcd.print(DateTime.month);
}else{digitalWrite(7,HIGH);}
/////////////////////// VOLUME ///////////////////////////////////
if(menu==0){
if(ir.value==IR_3){vol++;gr1=1;gr2=0;cl();w2=1;ogr_vol();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl();w2=1;ogr_vol();audio();}// кнопка >>>>>>
if(ir.value==IR_4){vol--;gr1=0;gr2=1;cl();w2=1;ogr_vol();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl();w2=1;ogr_vol();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
vol=vol+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;ogr_vol();audio();}
lcd.setCursor(0,0);lcd.print("VOLUME ");
lcd.print(" ");lcd.print((-45+vol)*2);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");vol_d=vol;
if(t==1){
lcd.setCursor(4,1);
if(DateTime.hour<10){lcd.print("0");}lcd.print(DateTime.hour);lcd.print(":");
if(DateTime.minute<10){lcd.print("0");}lcd.print(DateTime.minute);lcd.print(":");
if(DateTime.second<10){lcd.print("0");}lcd.print(DateTime.second);
}
else{
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;}}}
//////////////// BASS /////////////////////////////////////
if(menu==1){
if(ir.value==IR_3){bass++;gr1=1;gr2=0;cl();w2=1;ogr_bass();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl();w2=1;ogr_bass();audio();}// кнопка >>>>>>
if(ir.value==IR_4){bass--;gr1=0;gr2=1;cl();w2=1;ogr_bass();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl();w2=1;ogr_bass();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
bass=bass+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;ogr_bass();audio();}
lcd.setCursor(0,0);lcd.print("BASS ");
switch(bass){
case 11: bass_d=15;break;
case 10: bass_d=12;break;
case 9: bass_d=9;break;
case 8: bass_d=6;break;
case 7: bass_d=3;break;
case 6: bass_d=0;break;
case 5: bass_d=-3;break;
case 4: bass_d=-6;break;
case 3: bass_d=-9;break;
case 2: bass_d=-12;break;
}
if(bass_d>=0){lcd.print("+");}else{lcd.print("-");}
if(bass_d>-10&&bass_d<=0||bass_d<10&&bass_d>0){{lcd.print(" ");}}
lcd.print(abs(bass_d));lcd.print(" dB ");
if(w2==1){bass_d=bass_d+12;
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+3+1,1);lcd.print(" ");}}
if(bass_d==0){lcd.setCursor(0,1);lcd.print(" ");}w2=0;}}
/////////////////////// TREBLE //////////////////////////////////////////////////////
if(menu==2){
if(ir.value==IR_3){treb++;gr1=1;gr2=0;cl();w2=1;ogr_treb();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl();w2=1;ogr_treb();audio();}// кнопка >>>>>>
if(ir.value==IR_4){treb--;gr1=0;gr2=1;cl();w2=1;ogr_treb();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl();w2=1;ogr_treb();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
treb=treb+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;ogr_treb();audio();}
lcd.setCursor(0,0);lcd.print("TREBLE ");
switch(treb){
case 10: treb_d=12;break;
case 9: treb_d=9;break;
case 8: treb_d=6;break;
case 7: treb_d=3;break;
case 6: treb_d=0;break;
case 5: treb_d=-3;break;
case 4: treb_d=-6;break;
case 3: treb_d=-9;break;
case 2: treb_d=-12;break;
}
if(treb_d>=0){lcd.print("+");}else{lcd.print("-");}
if(treb_d>-10&&treb_d<=0||treb_d<10&&treb_d>0){{lcd.print(" ");}}
lcd.print(abs(treb_d));lcd.print(" dB ");
if(w2==1){treb_d=treb_d+12;
for(z=0,z0=0,z1=0;z<=treb_d;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0+4,1);lcd.write((uint8_t)0);lcd.setCursor(z0+4+1,1);lcd.print(" ");}}
if(treb_d==0){lcd.setCursor(0,1);lcd.print(" ");}w2=0;}}
//////////////// BALANCE /////////////////////////////////////
if(menu==3){
if(ir.value==IR_3){balans++;gr1=1;gr2=0;cl1();w2=1;ogr_balans();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){balans++;gr2=0;cl();w2=1;ogr_balans();audio();}// кнопка >>>>>>
if(ir.value==IR_4){balans--;gr1=0;gr2=1;cl1();w2=1;ogr_balans();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){balans--;gr1=0;cl();w2=1;ogr_balans();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
balans=balans+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;lcd.clear();ogr_balans();audio();}
lcd.setCursor(0,0);
if(balans*2>=0){lcd.print("-");}else{lcd.print("+");}
lcd.print(abs(balans*2));lcd.print(" dB ");
lcd.print(" <> ");
if(balans*2>=0){lcd.print("+");}else{lcd.print("-");}
lcd.print(abs(balans*2));lcd.print(" dB ");
lcd.setCursor(0,1);lcd.print("L");
lcd.setCursor(15,1);lcd.print("R");
if(balans<0){lcd.setCursor(balans+7,1);lcd.write((uint8_t)0);}
if(balans>0){lcd.setCursor(balans+8,1);lcd.write((uint8_t)0);}
if(balans==0){lcd.setCursor(7,1);lcd.write((uint8_t)0);lcd.setCursor(8,1);lcd.write((uint8_t)0);}}
///////////////////////// input /////////////////////////////
if(menu==4){
if(ir.value==IR_3){in++;gr1=1;gr2=0;cl();w2=1;ogr_in();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){in++;gr2=0;cl1();w2=1;ogr_in();audio();}// кнопка >>>>>>
if(ir.value==IR_4){in--;gr1=0;gr2=1;cl();w2=1;ogr_in();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){in--;gr1=0;cl1();w2=1;ogr_in();audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
in=in+newPosition;myEnc.write(0);newPosition=0;audio();time=millis();w=1;ogr_in();audio();}
lcd.setCursor(0,0);lcd.print("Source selector");
lcd.setCursor(0,1);lcd.print("INPUT: ");lcd.print(in+1);
}
////////////////////////////////////////////////////////////////
if(millis()-time>10000 && w==1){// сохранение всех настроек в eeprom через 10 сек неактивности
EEPROM.update(0,vol);
EEPROM.update(4,balans+4);
EEPROM.update(1,bass);
EEPROM.update(2,treb);
EEPROM.update(3,in);
w=0;w2=1;menu=0;cl1();t=1; }
////////////////// reg time ///////////////////////////////////
if(digitalRead(2)==HIGH&&standby==1){menu=100;cl();reg_time=1;x++;if(x>4){corr=1;cl();reg_time=0;x=0;menu=0;}};
if(reg_time==1){
menu=100;standby=1;
hour = DateTime.hour;
minut = DateTime.minute;
date = DateTime.day;
mont = DateTime.month;
lcd.setCursor(2,0);lcd.print("Time setting");//lcd.print(x);
if(millis()-tik<200){vvv=0;}if(millis()-tik>200&&millis()-tik<1000){vvv=1;}if(millis()-tik>1000){tik=millis();}
if(digitalRead(3)==HIGH&&x==1){hour++;delay(200);if(hour>23){hour=0;}corr=1;}
if(digitalRead(3)==HIGH&&x==2){minut++;delay(200);if(minut>59){minut=0;}corr=1;}
if(digitalRead(3)==HIGH&&x==3){date++;delay(200);if(date>31){date=1;}corr=1;}
if(digitalRead(3)==HIGH&&x==4){mont++;delay(200);if(mont>12){mont=1;}corr=1;}
if(x==1&&vvv==0){lcd.setCursor(1,1);lcd.print(" ");}
else{lcd.setCursor(1,1);lcd.print(hour/10);lcd.print(hour%10);}
lcd.print(":");
if(x==2&&vvv==0){lcd.setCursor(4,1);lcd.print(" ");}
else{lcd.setCursor(4,1);lcd.print(minut/10);lcd.print(minut%10);}
lcd.print(" ");
if(x==3&&vvv==0){lcd.setCursor(9,1);lcd.print(" ");}
else{lcd.setCursor(9,1);lcd.print(date/10);lcd.print(date%10);}
lcd.print("-");
if(x==4&&vvv==0){lcd.setCursor(12,1);lcd.print(" ");}
else{lcd.setCursor(12,1);lcd.print(mont/10);lcd.print(mont%10);}
}
if(corr==1){clock.setDateTime(2019, mont, date, hour, minut, 0);corr=0;}
/////////////////////////////////////////////////////////////////////////////////////////
}//loop
void ds3231(){lcd.setCursor(1,0);
if(DateTime.hour<10){lcd.print("0");}lcd.print(DateTime.hour);lcd.print(":");
if(DateTime.minute<10){lcd.print("0");}lcd.print(DateTime.minute);lcd.print(":");
if(DateTime.second<10){lcd.print("0");}lcd.print(DateTime.second);lcd.print(" ");
if(DateTime.day<10){lcd.print("0");}lcd.print(DateTime.day);lcd.print("-");
if(DateTime.month<10){lcd.print("0");}lcd.print(DateTime.month);
}
void ogr_in(){if(in>1){in=0;}if(in<0){in=1;}}
void ogr_balans(){if(balans>4){balans=4;}if(balans<-4){balans=-4;}}
void ogr_treb(){if(treb>10){treb=10;}if(treb<2){treb=2;}}
void ogr_bass(){if(bass>11){bass=11;}if(bass<2){bass=2;}}
void ogr_vol(){if(t==1){lcd.clear();}t=0;if(vol<0){vol=0;}if(vol>48){vol=48;}}
void cl(){ir.value=0;delay(200);}
void cl1(){ir.value=0;delay(400);lcd.clear();}
void to_Timer(){newPosition = myEnc.read()/4;}
void audio(){
tda.setVolumeL(vol+balans);
tda.setVolumeR(vol-balans);
tda.setBass(bass);
tda.setTreble(treb);
tda.setMute(mute);
tda.setSource(in);
}
убрал это: lcd.noBacklight();
изменил: tda.setVolumeL(vol+16+balans);
tda.setVolumeR(vol-16-balans);