#define IR_1 0x2FDD02F // Кнопка вверх
#define IR_2 0x2FD32CD // Кнопка вниз
#define IR_3 0x2FD906F // Кнопка >
#define IR_4 0x2FDF20D // Кнопка <
#define IR_5 0x2FD708F // Кнопка IN
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PT2033.h>
#include <Encoder.h>
#include <EEPROM.h>
#include <MsTimer2.h>
#include <boarddefs.h>
#include <IRremote.h>
PT2033 pt;
LiquidCrystal_I2C lcd(0x27,16,2); // Устанавливаем дисплей
IRrecv irrecv(12); // указываем вывод модуля IR приемника
Encoder myEnc(9, 8);// DT, CLK
decode_results ir;
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};
unsigned long time,oldPosition = -999,newPosition;
int menu,vol,w2,w,gr1,gr2,vol_d,z,z0,z1,bass,bass_d,treb,treb_d,in,loud,loud_off,attl,attr,atts;
void setup(){
Wire.begin();lcd.init();lcd.backlight();
irrecv.enableIRIn();
lcd.setCursor(0,0);lcd.print(" PT2033 ");delay(2000);
Serial.begin(9600); lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);
pinMode(10,INPUT); // МЕНЮ КНОПКА SW энкодера
pinMode(2,INPUT); // ВЫБОР ВХОДА КНОПКА IN
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);bass = EEPROM.read(1)-7;treb = EEPROM.read(2)-7;in = EEPROM.read(3);loud = EEPROM.read(4);attl = EEPROM.read(5);attr = EEPROM.read(6);atts = EEPROM.read(7);
cl();audio();
}
void loop(){
if(digitalRead(10)==LOW){menu++;cl();eep();if(menu>6){menu=0;}}// меню
if(digitalRead(2)==HIGH){in++;;cl();eep();if(in>3){in=0;};lcd.setCursor(0,0);lcd.print(" INPUT ");lcd.print(in+1);audio();delay(2000);}// КНОПКА ВЫБОРА ВХОДА IN
////////////////////// IR /////////////////////////////////////////////////
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();eep();}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
if(ir.value==IR_1){menu++;gr1=0;gr2=0;cl();eep();if(menu>6){menu=0;}}// меню кнопка вверх
if(ir.value==IR_2){menu--;gr1=0;gr2=0;cl();eep();if(menu<0){menu=6;}}// меню кнопка вниз
if(ir.value==IR_5){in++;gr1=0;gr2=0;cl();eep();if(in>3){in=0;};lcd.setCursor(0,0);lcd.print(" INPUT ");lcd.print(in+1);audio();delay(2000);}// КНОПКА ВЫБОРА ВХОДА IN
/////////////////////////////// VOLUME 0...63 ////////////////////////////////////////////////
if(menu==0){
if(ir.value==IR_3){vol++;gr1=1;gr2=0;cl1();eep();vol_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl1();eep();vol_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){vol--;gr1=0;gr2=1;cl1();eep();vol_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl1();eep();vol_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
vol=vol+newPosition;myEnc.write(0);newPosition=0;eep();vol_func();audio();}
lcd.setCursor(0,0);lcd.print("VOLUME ");lcd.print(vol);vol_d=vol-15;
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 +/-14 dB ///////////////////////////////////////////////////////////
if(menu==1){
if(ir.value==IR_3){bass++;gr1=1;gr2=0;cl1();eep();bass_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl1();eep();bass_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){bass--;gr1=0;gr2=1;cl1();eep();bass_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl1();eep();bass_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
bass=bass+newPosition;myEnc.write(0);newPosition=0;eep();bass_func();audio();}
lcd.setCursor(0,0);lcd.print("BASS ");
lcd.print(" ");lcd.print(bass*2);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");bass_d=bass*2+14;
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;}}
/////////////////////// TREBLE +/-14 dB ///////////////////////////////////////////////////////////
if(menu==2){
if(ir.value==IR_3){treb++;gr1=1;gr2=0;cl1();eep();treb_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl1();eep();treb_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){treb--;gr1=0;gr2=1;cl1();eep();treb_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl1();eep();treb_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
treb=treb+newPosition;myEnc.write(0);newPosition=0;eep();treb_func();audio();}
lcd.setCursor(0,0);lcd.print("TREBLE ");
lcd.print(" ");lcd.print(treb*2);lcd.print(" ");lcd.setCursor(13,0);lcd.print("dB");treb_d=treb*2+14;
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;}}
///////////////////////// LOUDNESS ////////////////////////////////////////////////////////
if(menu==3){
if(ir.value==IR_3){loud++;gr1=1;gr2=0;cl1();eep();loud_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){loud++;gr2=0;cl1();eep();loud_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){loud--;gr1=0;gr2=1;cl1();eep();loud_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){loud--;gr1=0;cl1();eep();loud_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
loud=loud+newPosition;myEnc.write(0);newPosition=0;eep();loud_func();audio();}
lcd.setCursor(0,0);lcd.print("LOUDNESS ");lcd.print(loud);
if(loud>0){loud_off=1;lcd.setCursor(0,1);lcd.print("ON ");}else{loud_off=0;lcd.setCursor(0,1);lcd.print("OFF");}}
/////////////////////////////// ATT L 0...31 ////////////////////////////////////////////////
if(menu==4){
if(ir.value==IR_3){attl++;gr1=1;gr2=0;cl1();eep();attl_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){attl++;gr2=0;cl1();eep();attl_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){attl--;gr1=0;gr2=1;cl1();eep();attl_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){attl--;gr1=0;cl1();eep();attl_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
attl=attl+newPosition;myEnc.write(0);newPosition=0;eep();attl_func();audio();}
lcd.setCursor(0,0);lcd.print("ATT L ");lcd.print(attl);lcd.print(" ");
if(w2==1){
for(z=0,z0=0,z1=0;z<=attl;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;}}
/////////////////////////////// ATT R 0...31 ////////////////////////////////////////////////
if(menu==5){
if(ir.value==IR_3){attr++;gr1=1;gr2=0;cl1();eep();attr_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){attr++;gr2=0;cl1();eep();attr_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){attr--;gr1=0;gr2=1;cl1();eep();attr_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){attr--;gr1=0;cl1();eep();attr_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
attr=attr+newPosition;myEnc.write(0);newPosition=0;eep();attr_func();audio();}
lcd.setCursor(0,0);lcd.print("ATT R ");lcd.print(attr);lcd.print(" ");
if(w2==1){
for(z=0,z0=0,z1=0;z<=attr;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;}}
/////////////////////////////// ATT S 0...31 ////////////////////////////////////////////////
if(menu==6){
if(ir.value==IR_3){atts++;gr1=1;gr2=0;cl1();eep();atts_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){atts++;gr2=0;cl1();eep();atts_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){atts--;gr1=0;gr2=1;cl1();eep();atts_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){atts--;gr1=0;cl1();eep();atts_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
atts=atts+newPosition;myEnc.write(0);newPosition=0;eep();atts_func();audio();}
lcd.setCursor(0,0);lcd.print("ATT SUB ");lcd.print(atts);lcd.print(" ");
if(w2==1){
for(z=0,z0=0,z1=0;z<=atts;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;}}
///////////////////// EEPROM ///////////////////////////////////////////////////////////////
if(millis()-time>10000 && w==1){
EEPROM.update(0,vol);EEPROM.update(1,bass+7);EEPROM.update(2,treb+7);EEPROM.update(3,in);EEPROM.update(4,loud);EEPROM.update(5,attl);EEPROM.update(6,attr);EEPROM.update(7,atts);
menu=0;w=0;w2=1;cl();}
}//loop
void atts_func(){if(atts>30){atts=30;}if(atts<0){atts=0;}}
void attr_func(){if(attr>30){attr=30;}if(attr<0){attr=0;}}
void attl_func(){if(attl>30){attl=30;}if(attl<0){attl=0;}}
void loud_func(){if(loud>3){loud=3;}if(loud<0){loud=0;}};
void bass_func(){if(bass>7){bass=7;}if(bass<-7){bass=-7;}}
void treb_func(){if(treb>7){treb=7;}if(treb<-7){treb=-7;}}
void eep(){newPosition=0;time=millis();w=1;w2=1;}
void vol_func(){if(vol>63){vol=63;}if(vol<0){vol=0;}}
void cl(){ir.value=0;delay(300);lcd.clear();}
void cl1(){ir.value=0;delay(100);}
void to_Timer(){newPosition = myEnc.read()/4;}
void audio(){
pt.setVolume(vol); // громкость 0...63
pt.setAttL(attl); // аттенюатор L 0...31
pt.setAttR(attr); // аттенюатор R 0...31
pt.setAttS(atts); // аттенюатор S 0...31
pt.setIn(in,loud_off,loud); // вход 0...3, тонкомпенсация 1 вкл 0 выкл, усиление 0...3
pt.setBass(bass); // тембр НЧ -7...+7
pt.setTreble(treb); // тембр ВЧ -7...+7
}