#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 <BD3491FS.h>
#include <Encoder.h>
#include <EEPROM.h>
#include <MsTimer2.h>
#include <boarddefs.h>
#include <IRremote.h>
BD3491FS bd;
LiquidCrystal_I2C lcd(0x27,16,2); // Устанавливаем дисплей
IRrecv irrecv(12); // указываем вывод модуля IR приемника
Encoder myEnc(8, 9);// CLK, DT
decode_results ir;
byte v1[8] = {31,31,31,31,31,31,31,31};
byte v2[8] = {31,31,31, 0, 0, 0, 0, 0};
byte v3[8] = { 0, 0, 0, 0, 0,31,31,31};
byte v4[8] = {31,31, 0, 0, 0, 0,31,31};
byte v5[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
byte v7[8] = { 0, 0, 0, 0, 0, 0,31,31};
unsigned long time,oldPosition = -999,newPosition;
int menu,w,w2,vol,vol_d,gr1,gr2,z,z0,z1,treb,treb_d,bass,bass_d,in,www,gain1,gain2,gain3,gain4,gain5,gain6,gain0,gain_d,sur;
byte a[2];
byte i,d1,d2,d3,d4,d5,d6,e1,e2,e3;
void setup(){
irrecv.enableIRIn();lcd.init();lcd.backlight();
lcd.setCursor(0,0);lcd.print(" BD3491FS ");delay(2000);cl();
Serial.begin(9600); lcd.createChar(1, v1);lcd.createChar(2, v2);lcd.createChar(3, v3);lcd.createChar(4, v4);lcd.createChar(5, v5);lcd.createChar(7, v7);
pinMode(10,INPUT); // МЕНЮ КНОПКА SW энкодера
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)-87;treb = EEPROM.read(1)-7;bass = EEPROM.read(2)-7;
in = EEPROM.read(3);sur = EEPROM.read(10);
gain1 = EEPROM.read(4);gain2 = EEPROM.read(5);gain3 = EEPROM.read(6);
gain4 = EEPROM.read(7);gain5 = EEPROM.read(8);gain6 = EEPROM.read(9);
audio();
}
void loop(){
if(digitalRead(10)==LOW){menu++;cl();time=millis();w=1;w2=1;if(menu>4){menu=0;}}// меню
if(digitalRead(2)==HIGH){in++;;cl();time=millis();w=1;w2=1;www=1;if(in>5){in=0;};lcd.setCursor(0,0);lcd.print(" INPUT ");lcd.print(in+1);audio();delay(2000);cl();}// КНОПКА ВЫБОРА ВХОДА I
////////////////////// IR /////////////////////////////////////////////////
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();time=millis();w=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
if(ir.value==IR_1){menu++;gr1=0;gr2=0;cl();time=millis();w=1;w2=1;if(menu>4){menu=0;}}// меню кнопка вверх
if(ir.value==IR_2){menu--;gr1=0;gr2=0;cl();time=millis();w=1;w2=1;if(menu<0){menu=4;}}// меню кнопка вниз
if(ir.value==IR_5){in++;gr1=0;gr2=0;cl();time=millis();w=1;w2=1;www=1;if(in>5){in=0;};lcd.setCursor(0,0);lcd.print(" INPUT ");lcd.print(in+1);audio();delay(2000);cl();}// КНОПКА ВЫБОРА ВХОДА IN
/////////////////////////////// VOLUME -87 ... +0 дБ | step 1 dB////////////////////////////////////////////////
if(menu==0){
if(ir.value==IR_3){vol++;gr1=1;gr2=0;cl1();w2=1;vol_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl1();w2=1;vol_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){vol--;gr1=0;gr2=1;cl1();w2=1;vol_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl1();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();}
vol_d = 87-abs(vol);
a[0]= vol_d/10;a[1]=vol_d%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=9,e2=10,e3=11;break;
case 1: e1=13,e2=14,e3=15;break;}
switch(a[i]){
case 0: d1=1,d2=2,d3=1,d4=1,d5=3,d6=1;break;
case 1: d1=2,d2=1,d3=5,d4=7,d5=1,d6=7;break;
case 2: d1=2,d2=4,d3=1,d4=1,d5=3,d6=3;break;
case 3: d1=2,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 4: d1=1,d2=3,d3=1,d4=5,d5=5,d6=1;break;
case 5: d1=1,d2=4,d3=2,d4=3,d5=3,d6=1;break;
case 6: d1=1,d2=4,d3=2,d4=1,d5=3,d6=1;break;
case 7: d1=2,d2=2,d3=1,d4=5,d5=5,d6=1;break;
case 8: d1=1,d2=4,d3=1,d4=1,d5=3,d6=1;break;
case 9: d1=1,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 11: d1=5,d2=5,d3=5,d4=5,d5=5,d6=5;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);}
// if(vol<0){lcd.setCursor(7,0);lcd.write((uint8_t)7);}else{lcd.setCursor(7,0);lcd.write((uint8_t)5);}
lcd.setCursor(0,0);lcd.print("VOLUME");
lcd.setCursor(0,1);lcd.print("IN ");lcd.print(in+1);
}
///////////////////////// TREBLE +/-14 dB | step 2 dB/////////////////////////////////////////////////////////
if(menu==1){
if(ir.value==IR_3){treb++;gr1=1;gr2=0;cl1();w2=1;treb_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl1();w2=1;treb_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){treb--;gr1=0;gr2=1;cl1();w2=1;treb_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl1();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();}
treb_d = abs(treb*2);
a[0]= treb_d/10;a[1]=treb_d%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=9,e2=10,e3=11;break;
case 1: e1=13,e2=14,e3=15;break;}
switch(a[i]){
case 0: d1=1,d2=2,d3=1,d4=1,d5=3,d6=1;break;
case 1: d1=2,d2=1,d3=5,d4=7,d5=1,d6=7;break;
case 2: d1=2,d2=4,d3=1,d4=1,d5=3,d6=3;break;
case 3: d1=2,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 4: d1=1,d2=3,d3=1,d4=5,d5=5,d6=1;break;
case 5: d1=1,d2=4,d3=2,d4=3,d5=3,d6=1;break;
case 6: d1=1,d2=4,d3=2,d4=1,d5=3,d6=1;break;
case 7: d1=2,d2=2,d3=1,d4=5,d5=5,d6=1;break;
case 8: d1=1,d2=4,d3=1,d4=1,d5=3,d6=1;break;
case 9: d1=1,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 11: d1=5,d2=5,d3=5,d4=5,d5=5,d6=5;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);}
if(treb<0){lcd.setCursor(7,0);lcd.write((uint8_t)7);}else{lcd.setCursor(7,0);lcd.write((uint8_t)5);}
lcd.setCursor(0,0);lcd.print("TREBLE");
lcd.setCursor(0,1);lcd.print("10 kHz");
}
/////////////////////// BASS +/-14 dB | step 2 dB///////////////////////////////////////////////////////////
if(menu==2){
if(ir.value==IR_3){bass++;gr1=1;gr2=0;cl1();w2=1;bass_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl1();w2=1;bass_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){bass--;gr1=0;gr2=1;cl1();w2=1;bass_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl1();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();}
bass_d = abs(bass*2);
a[0]= bass_d/10;a[1]=bass_d%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=9,e2=10,e3=11;break;
case 1: e1=13,e2=14,e3=15;break;}
switch(a[i]){
case 0: d1=1,d2=2,d3=1,d4=1,d5=3,d6=1;break;
case 1: d1=2,d2=1,d3=5,d4=7,d5=1,d6=7;break;
case 2: d1=2,d2=4,d3=1,d4=1,d5=3,d6=3;break;
case 3: d1=2,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 4: d1=1,d2=3,d3=1,d4=5,d5=5,d6=1;break;
case 5: d1=1,d2=4,d3=2,d4=3,d5=3,d6=1;break;
case 6: d1=1,d2=4,d3=2,d4=1,d5=3,d6=1;break;
case 7: d1=2,d2=2,d3=1,d4=5,d5=5,d6=1;break;
case 8: d1=1,d2=4,d3=1,d4=1,d5=3,d6=1;break;
case 9: d1=1,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 11: d1=5,d2=5,d3=5,d4=5,d5=5,d6=5;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);}
if(bass<0){lcd.setCursor(7,0);lcd.write((uint8_t)7);}else{lcd.setCursor(7,0);lcd.write((uint8_t)5);}
lcd.setCursor(0,0);lcd.print("BASS ");
lcd.setCursor(0,1);lcd.print("100 Hz");
}
//////////////// IN + GAIN /////////////////////////////////////////////////////////////
if(menu==3){
switch(in){
case 0: gain0 = gain1;break;
case 1: gain0 = gain2;break;
case 2: gain0 = gain3;break;
case 3: gain0 = gain4;break;
case 4: gain0 = gain5;break;
case 5: gain0 = gain6;break;
}
if(ir.value==IR_3){gain0++;gr1=1;gr2=0;cl1();w2=1;gain_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){gain0++;gr2=0;cl1();w2=1;gain_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){gain0--;gr1=0;gr2=1;cl1();w2=1;gain_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){gain0--;gr1=0;cl1();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 0: gain1 = gain0;break;
case 1: gain2 = gain0;break;
case 2: gain3 = gain0;break;
case 3: gain4 = gain0;break;
case 4: gain5 = gain0;break;
case 5: gain6 = gain0;break;
}
if(www==1){audio();www=0;}
lcd.setCursor(0,0);lcd.print("Gain");
lcd.setCursor(0,1);lcd.print("IN ");lcd.print(in+1);
gain0 = abs(gain0*2);
a[0]= gain0/10;a[1]=gain0%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=9,e2=10,e3=11;break;
case 1: e1=13,e2=14,e3=15;break;}
switch(a[i]){
case 0: d1=1,d2=2,d3=1,d4=1,d5=3,d6=1;break;
case 1: d1=2,d2=1,d3=5,d4=7,d5=1,d6=7;break;
case 2: d1=2,d2=4,d3=1,d4=1,d5=3,d6=3;break;
case 3: d1=2,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 4: d1=1,d2=3,d3=1,d4=5,d5=5,d6=1;break;
case 5: d1=1,d2=4,d3=2,d4=3,d5=3,d6=1;break;
case 6: d1=1,d2=4,d3=2,d4=1,d5=3,d6=1;break;
case 7: d1=2,d2=2,d3=1,d4=5,d5=5,d6=1;break;
case 8: d1=1,d2=4,d3=1,d4=1,d5=3,d6=1;break;
case 9: d1=1,d2=4,d3=1,d4=3,d5=3,d6=1;break;
case 11: d1=5,d2=5,d3=5,d4=5,d5=5,d6=5;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);}
}
////////////////////////////// Surround ////////////////////////////////////////////////////
if(menu==4){
if(ir.value==IR_3){sur++;gr1=1;gr2=0;cl1();w2=1;sur_func();audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){sur++;gr2=0;cl1();w2=1;sur_func();audio();}// кнопка >>>>>>
if(ir.value==IR_4){sur--;gr1=0;gr2=1;cl1();w2=1;sur_func();audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){sur--;gr1=0;cl1();w2=1;sur_func();audio();}// кнопка <<<<<<
if (newPosition != oldPosition) {
oldPosition = newPosition;
sur=sur+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w2=1;sur_func();audio();}
lcd.setCursor(0,0);lcd.print("Surround Gain");
lcd.setCursor(0,1);
switch(sur){
case 0: lcd.print("OFF ");break;
case 1: lcd.print("LOW ");break;
case 2: lcd.print("MIDDLE");break;
case 3: lcd.print("HIGH ");break;}}
///////////////////// EEPROM ///////////////////////////////////////////////////////////////
if(millis()-time>10000 && w==1){
EEPROM.update(0,vol+87);EEPROM.update(1,treb+7);EEPROM.update(2,bass+7);
EEPROM.update(3,in);EEPROM.update(10,sur);
EEPROM.update(4,gain1);EEPROM.update(5,gain2);EEPROM.update(6,gain3);
EEPROM.update(7,gain4);EEPROM.update(8,gain5);EEPROM.update(9,gain6);
menu=0;w=0;w2=1;cl();}
}//loop
void sur_func(){if(sur>3){sur=0;}if(sur<0){sur=3;}}
void gain_func(){if(gain0<0){gain0=0;}if(gain0>7){gain0=7;}}
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 vol_func(){if(vol<-87){vol=-87;}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(){
bd.setSelect(in);// int 0...7 === A B C D E F INPUT_SHORT INPUT_MUTE
bd.setIn_gain(gain0); // int 0...7 === 0...14 dB
bd.setVol_1(abs(vol)); // int 0...87 === 0...-87 dB
bd.setVol_2(abs(vol)); // int 0...87 === 0...-87 dB
bd.setBass(bass); // int -7...0...+7 === -7...+7 dB
bd.setTreb(treb); // int -7...0...+7 === -7...+7 dB
bd.setSurround(sur); // int 0...3 === OFF LOW MIDDLE HIGH
}