Тема: PT2314 + LCD2004
Протестировано
#define IR_1 0x2FDD02F // Кнопка вверх
#define IR_2 0x2FD32CD // Кнопка вниз
#define IR_3 0x2FD906F // Кнопка >
#define IR_4 0x2FDF20D // Кнопка <
#define IR_5 0x2FD708F // Кнопка IN
#define IR_6 0x2FD6A95 // Кнопка SET
#define IR_7 0x2FD00FF // Кнопка MUTE
#define IR_8 0x2FDB24D // Кнопка STANDBY (POWER)
#include <Wire.h>
#include <PT2314.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
#include <EEPROM.h>
#include <MsTimer2.h>
#include <boarddefs.h>
#include <IRremote.h>
#include <DS3231.h>
DS3231 clock;RTCDateTime DateTime;
PT2314 pt;
LiquidCrystal_I2C lcd(0x27,20,4); // Устанавливаем дисплей
IRrecv irrecv(12); // указываем вывод модуля IR приемника
Encoder myEnc(9, 8);// DT, CLK
decode_results ir;
int menu,menu0,menu1,vol,bass,treb,in,balans,balans_d,vol_d,bass_d,treb_d,temp0,par,hour,minut,secon,z_old,gain0,loud;
int att_r,att_l,gain1,gain2,gain3,gain4,brig0,brig1;
byte w2[4],z,z0,z1,n,q,gr1,gr2,www,i,w,mute,power,in_x,save,i1;
unsigned long time0,oldPosition = -999,newPosition,times_in;
byte mesto2[5]={0,11,6,0,11};
byte mesto3[5]={0,0,1,3,3};
void setup() {
irrecv.enableIRIn();lcd.init();lcd.backlight();clock.begin();Serial.begin(9600);
pinMode(10,INPUT); // МЕНЮ КНОПКА SW энкодера
pinMode(2,INPUT); // КНОПКА SET
pinMode(3,INPUT); // КНОПКА IN
pinMode(4,INPUT); // КНОПКА MUTE
pinMode(5,INPUT); // КНОПКА STANDBY
pinMode(7,OUTPUT); // ВЫХОД УПРАВЛЕНИЯ STANDBY
pinMode(6,OUTPUT); // ВЫХОД УПРАВЛЕНИЯ ПОДСВЕТКОЙ
analogWrite(6, 200);// больше 200 не делать
lcd.setCursor(1,1);lcd.print("Sound Processor");lcd.setCursor(1,2);lcd.print("PT2314"); delay(2000);lcd.clear();
MsTimer2::set(3, to_Timer);MsTimer2::start();
// clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча
if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// очистка памяти при первом включении
vol = EEPROM.read(0);treb = EEPROM.read(1)-7;bass = EEPROM.read(2)-7;in = EEPROM.read(3);
att_l = EEPROM.read(4);att_r = EEPROM.read(5);gain1 = EEPROM.read(6);gain2 = EEPROM.read(7);
gain3 = EEPROM.read(8);gain4 = EEPROM.read(9);loud = EEPROM.read(10);brig0 = EEPROM.read(11);brig1 = EEPROM.read(12);
w2_arr();audio();cl();
analogWrite(6, 25*brig0);
}
void loop() {
DateTime = clock.getDateTime();hour = DateTime.hour;minut = DateTime.minute;secon = DateTime.second;
/////////////////////////////// УПРАВЛЕНИЕ //////////////////////////////////////////////
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();time0=millis();w=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
if(mute==0&&power==0){
if(ir.value==IR_2&&menu0==0){menu++;gr1=0;gr2=0;cl1();time0=millis();w=1;w2_arr();if(menu>2){menu=0;}}//меню 1
if(ir.value==IR_1&&menu0==0){menu--;gr1=0;gr2=0;cl1();time0=millis();w=1;w2_arr();if(menu<0){menu=2;}}//меню 1
if(ir.value==IR_2&&menu0==1){menu1++;gr1=0;gr2=0;cl1();time0=millis();w=1;if(menu1>4){menu1=0;}}//меню 2
if(ir.value==IR_1&&menu0==1){menu1--;gr1=0;gr2=0;cl1();time0=millis();w=1;if(menu1<0){menu1=4;}}//меню 2
if(digitalRead(10)==LOW&&menu0==0){menu++;delay(200);time0=millis();w=1;w2_arr();if(menu>2){menu=0;}}// меню 0
if(digitalRead(10)==LOW&&menu0==1){menu1++;delay(200);time0=millis();w=1;if(menu1>4){menu1=0;}}// меню 1
if((ir.value==IR_6||digitalRead(2)==HIGH)&&menu0==0){menu0=1;cl();time0=millis();w=1;lcd.setCursor(6,1);lcd.print("SETTING"); delay(2000);lcd.clear();}
if((ir.value==IR_6||digitalRead(2)==HIGH)&&menu0==1){menu0=0;menu=0;cl();w2_arr();time0=millis();w=1;lcd.setCursor(8,1);lcd.print("MENU"); delay(2000);lcd.clear();}
if(ir.value==IR_5||digitalRead(3)==HIGH){in++;cl();times_in=millis();in_x=1;www=1;menu0=100;if(in>3){in=0;}}// IN
}
if((ir.value==IR_7||digitalRead(4)==HIGH)&&mute==0&&power==0){mute=1;pt.setAttR(31);pt.setAttL(31);menu0=100;cl();w=1;w2_arr();lcd.setCursor(8,1);lcd.print("MUTE");delay(300);}// mute on
if((ir.value==IR_7||digitalRead(4)==HIGH)&&mute==1&&power==0){mute=0;cl();time0=millis();w=1;w2_arr();menu0=0;myEnc.write(0);audio();}// mute off
if((ir.value==IR_8||digitalRead(5)==HIGH)&&power==0){power=1;save=1;pt.setAttR(31);pt.setAttL(31);cl();lcd.setCursor(5,1);lcd.print("POWER OFF");menu0=100;delay(3000);analogWrite(6, brig1*25);}// power off
if((ir.value==IR_8||digitalRead(5)==HIGH)&&power==1){power=0;analogWrite(6, brig0*25);lcd.clear();lcd.setCursor(5,1);lcd.print("POWER ON ");w=1;w2_arr();menu0=0;myEnc.write(0);audio();delay(3000);cl();}// power on
if(power==0){digitalWrite(7,HIGH);
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};
byte a4[8] = {0b10000,0b11000,0b11100,0b11110,0b11100,0b11000,0b10000,0b00000}; //>
byte a5[8] = {0b00000,0b00000,0b00000,0b00000,0b00000,0b00000,0b00000,0b00000};
lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);lcd.createChar(3,a4);lcd.createChar(4,a5);
}
if(power==1){digitalWrite(7,LOW);
byte v1[8] = {7,7,7,7,7,7,7,7};
byte v2[8] = {7,7,0, 0, 0, 0, 0, 0};
byte v3[8] = { 0, 0, 0, 0, 0,0,31,31};
byte v4[8] = {31,31, 0, 0, 0, 0,31,31};
byte v5[8] = { 28, 28, 0, 0, 0, 0, 28, 28};
byte v6[8] = {28,28,28,28,28,28,28,28};
byte v7[8] = { 0, 0, 0, 0, 0, 0,7,7};
byte v8[8] = { 31, 31,0,0,0,0,0, 0};
byte a[6];
byte i0,d1,d2,d3,d4,d5,d6,e1,e2,e3;
lcd.createChar(1, v1);lcd.createChar(2, v2);lcd.createChar(3, v3);lcd.createChar(4, v4);lcd.createChar(5, v5);lcd.createChar(6, v6);lcd.createChar(7, v7);lcd.createChar(8, v8);
a[0]=DateTime.hour/10;
a[1]=DateTime.hour%10;
a[2]=DateTime.minute/10;
a[3]=DateTime.minute%10;
a[4]=DateTime.second/10;
a[5]=DateTime.second%10;
for(i=0;i<6;i++){
switch(i){
case 0: e1=0,e2=1,e3=2;break;
case 1: e1=3,e2=4,e3=5;break;
case 2: e1=7,e2=8,e3=9;break;
case 3: e1=10,e2=11,e3=12;break;
case 4: e1=14,e2=15,e3=16;break;
case 5: e1=17,e2=18,e3=19;break;
}
switch(a[i]){
case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;
case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;
case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;
case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;
case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;
case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
}
lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);
}
lcd.setCursor(6,0);lcd.print(".");lcd.setCursor(13,0);lcd.print(".");lcd.setCursor(6,1);lcd.print(".");lcd.setCursor(13,1);lcd.print(".");
lcd.setCursor(5,3);lcd.print("POWER OFF");
if(digitalRead(10)==LOW&&digitalRead(2)==HIGH){hour++;if(hour>23){hour=0;} clock.setDateTime(2020, 9, 15, hour, minut, secon);delay(100);}
if(digitalRead(10)==LOW&&digitalRead(3)==HIGH){minut++;if(minut>59){minut=0;} clock.setDateTime(2020, 9, 15, hour, minut, secon);delay(100); }
if(digitalRead(10)==LOW&&digitalRead(4)==HIGH){secon=0; clock.setDateTime(2020, 9, 15, hour, minut, secon);delay(100); }
}
/////////////////////////////// MENU0 = VOLUME TERBLE BASS BALANCE ///////////////////////////////////////////////////////////////////////
if(menu0==0){
switch(menu){
case 0: temp0 = vol;q=0;break;
case 1: temp0 = bass;q=1;break;
case 2: temp0 = treb;q=2;break;}
if(ir.value==IR_3){temp0++;gr1=1;gr2=0;cl1();time0=millis();w=1;w2[q]=1;www=1;}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){temp0++;gr2=0;cl1();time0=millis();;w=1;w2[q]=1;www=1;}// кнопка >>>>>>
if(ir.value==IR_4){temp0--;gr1=0;gr2=1;cl1();time0=millis();;w=1;w2[q]=1;www=1;}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){temp0--;gr1=0;cl1();time0=millis();w=1;w2[q]=1;www=1;}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
temp0=temp0+newPosition;myEnc.write(0);newPosition=0;time0=millis();w=1;w2[q]=1;www=1;}
switch(menu){
case 0: vol = temp0;vol_func();break;
case 1: bass = temp0;bass_func();break;
case 2: treb = temp0;treb_func();break;}
au();
for(i=0;i<3;i++){if(menu==i){lcd.setCursor(0,i);lcd.write((uint8_t)3);}else{lcd.setCursor(0,i);lcd.print(" ");}}
lcd.setCursor(1,0);lcd.print("VOLUME ");if(vol>=0){lcd.print(" ");}lcd.print(vol);lcd.print(" ");vol_d=map(vol,0,56,1,24);
lcd.setCursor(1,1);lcd.print("BASS ");if(bass>=0){lcd.print(" ");}lcd.print(bass);lcd.print(" ");bass_d=map(bass,-7,7,1,24);
lcd.setCursor(1,2);lcd.print("TREBLE ");if(treb>=0){lcd.print(" ");}lcd.print(treb);lcd.print(" ");treb_d=map(treb,-7,7,1,24);
lcd.setCursor(1,3);lcd.print("INPUT ");lcd.print(in+1);lcd.print(" ");
lcd.print(DateTime.hour/10);lcd.print(DateTime.hour%10);lcd.print(":");lcd.print(DateTime.minute/10);lcd.print(DateTime.minute%10);lcd.print(":");
lcd.print(DateTime.second/10);lcd.print(DateTime.second%10);
for(n=0;n<3;n++){if(w2[n]==1){
switch(n){
case 0: par = vol_d;break;
case 1: par = bass_d;break;
case 2: par = treb_d;break;}
for(z=0,z0=0,z1=0;z<=par;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0+12,n);lcd.write((uint8_t)0);lcd.setCursor(z0+1+12,n);Serial.println(z0);z_old=7-par/3;while(z_old>0){z_old=z_old-1;lcd.write((uint8_t)4);}}}
if(z1==3){lcd.setCursor(z0+12,n);lcd.write((uint8_t)1);}
if(z1==2){lcd.setCursor(z0+12,n);lcd.write((uint8_t)2);}w2[n]=0;}
}}// menu0 = 0
///////////////////////////////////////////////////////////////////////////////////////////////
if(menu0==1){
switch(menu1){
case 0: temp0 = att_l;break;
case 1: temp0 = att_r;break;
case 2: temp0 = loud;break;
case 3: temp0 = brig0;break;
case 4: temp0 = brig1;break;
}
if(ir.value==IR_3){temp0++;gr1=1;gr2=0;cl1();time0=millis();w=1;www=1;}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){temp0++;gr2=0;cl1();time0=millis();w=1;www=1;}// кнопка >>>>>>
if(ir.value==IR_4){temp0--;gr1=0;gr2=1;cl1();time0=millis();;w=1;www=1;}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){temp0--;gr1=0;cl1();time0=millis();w=1;www=1;}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
temp0=temp0+newPosition;myEnc.write(0);newPosition=0;time0=millis();w=1;www=1;}
switch(menu1){
case 0: att_l = temp0;att_l_func();break;
case 1: att_r = temp0;att_r_func();break;
case 2: loud = temp0;loud_func();break;
case 3: brig0 = temp0;brig0_func();break;
case 4: brig1 = temp0;brig1_func();break;
}
au();analogWrite(6, brig0*25);
for(i=0;i<5;i++){if(menu1==i){lcd.setCursor(mesto2[i],mesto3[i]);lcd.write((uint8_t)3);}else{lcd.setCursor(mesto2[i],mesto3[i]);lcd.print(" ");}}
lcd.setCursor(1,0);lcd.print("ATT_L ");lcd.print(att_l);lcd.print(" ");lcd.setCursor(12,0);lcd.print("ATT_R ");lcd.print(att_r);lcd.print(" ");
lcd.setCursor(7,1);lcd.print("LOUD ");if(loud==0){lcd.print("OFF");}else{lcd.print("ON ");}
lcd.setCursor(0,2);lcd.print("-------------------");
lcd.setCursor(1,3);lcd.print("BRIG_1 ");lcd.print(brig0);lcd.print(" ");lcd.setCursor(12,3);lcd.print("BRIG_2 ");lcd.print(brig1);
} // menu0 = 1
////////////////////////// GAIN IN /////////////////////////////////////////////////////////////////////////////////////
if(in_x==1){ menu0=100;
switch(in){
case 0: gain0 = gain1;break;
case 1: gain0 = gain2;break;
case 2: gain0 = gain3;break;
case 3: gain0 = gain4;break;}
if(ir.value==IR_3){gain0++;gr1=1;gr2=0;cl1();time0=millis();times_in=millis();www=1;w=1;gain_func();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){gain0++;gr2=0;cl1();time0=millis();times_in=millis();www=1;w=1;gain_func();}// кнопка >>>>>>
if(ir.value==IR_4){gain0--;gr1=0;gr2=1;cl1();time0=millis();times_in=millis();www=1;w=1;gain_func();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){gain0--;gr1=0;cl1();time0=millis();times_in=millis();www=1;w=1;gain_func();}// кнопка <<<<<<
if (newPosition != oldPosition) {oldPosition = newPosition;
gain0=gain0+newPosition;myEnc.write(0);newPosition=0;time0=millis();times_in=millis();www=1;w=1;gain_func();}
switch(in){
case 0: gain1 = gain0;break;
case 1: gain2 = gain0;break;
case 2: gain3 = gain0;break;
case 3: gain4 = gain0;break;}
au();
lcd.setCursor(5,1);lcd.print("Gain IN ");lcd.print(in+1);lcd.setCursor(6,2);lcd.print(float(gain0*3.75));lcd.print(" ");lcd.print("dB ");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(millis()-time0>10000&&w==1&&mute==0&&power==0){if(menu0==1){cl();}w=0;menu0=0;menu=0;w2_arr();}
if(millis()-times_in>3000&&w==1&&in_x==1&&mute==0&&power==0){cl();w=0;in_x=0;menu0=0;menu=0;w2_arr();}
if(save==1){save=0;
EEPROM.update(0,vol);EEPROM.update(1,treb+7);EEPROM.update(2,bass+7);EEPROM.update(3,in);
EEPROM.update(4,att_l);EEPROM.update(5,att_r);EEPROM.update(6,gain1);EEPROM.update(7,gain2);
EEPROM.update(8,gain3);EEPROM.update(9,gain4);EEPROM.update(10,loud);EEPROM.update(11,brig0);EEPROM.update(12,brig1);}
}// loop
void brig1_func(){if(brig1>8){brig1=8;}if(brig1<0){brig1=0;}}
void brig0_func(){if(brig0>8){brig0=8;}if(brig0<0){brig0=0;}}
void w2_arr(){w2[0]=1;w2[1]=1;w2[2]=1;w2[3]=1;}
void au(){if(www==1){audio();www=0;}}
void in_func(){if(in>3){in=3;}if(in<0){in=0;}}
void loud_func(){if(loud<0){loud=0;}if(loud>1){loud=1;}}
void gain_func(){if(gain0<0){gain0=0;}if(gain0>3){gain0=3;}}
void att_r_func(){if(att_r<0){att_r=0;}if(att_r>24){att_r=24;}}
void att_l_func(){if(att_l<0){att_l=0;}if(att_l>24){att_l=24;}}
void treb_func(){if(treb<-7){treb=-7;}if(treb>7){treb=7;}}
void bass_func(){if(bass<-7){bass=-7;}if(bass>7){bass=7;}}
void vol_func(){if(vol<0){vol=0;}if(vol>56){vol=56;}}
void cl(){ir.value=0;delay(300);lcd.clear();}
void cl1(){ir.value=0;delay(200);}
void audio(){
pt.setVolume(vol); // int 0...56 === -70...0 dB step 1.25 dB
pt.setAttL(att_l); // int 0...24 === 0...-30 db step 1.25 dB > int 31 === mute on
pt.setAttR(att_r); // int 0...24 === 0...-30 dB step 1.25 dB > int 31 === mute on
pt.setSwitch(in, loud, gain0);
// input int 0..3 === IN 1...4
// loudness int 0...1 === 1-on 0-off
// input gain int 0...3 === 0...11.25dB step 3.75 dB
pt.setBass(bass); // int -7...+7 === -14...+14 dB step 2 dB
pt.setTreble(treb); // int -7...+7 === -14...+14 dB step 2 dB
}
void to_Timer(){newPosition = myEnc.read()/4;}