Тема: TDA7318 (доработка 2)
Основная статья - http://rcl-radio.ru/?p=108423
#define UP 0x33B8E01F
#define DOWN 0x33B810EF
#define MENU 0x33B820DF
#define MUTE 0x33B8946B
#define IN 0x33B8C03F
#define LOG_OUT 1
#define AUTO_GAIN 1 // автонастройка по громкости
#define LOW_PASS 30 // нижний порог чувствительности шумов (нет скачков при отсутствии звука)
#define DEF_GAIN 120 // максимальный порог по умолчанию
#define FHT_N 128 // ширина спектра х2
#define LOG_OUT 1
#include <Wire.h> // Входит в состав Arduino IDE
#include <MsTimer2.h> // http://rcl-radio.ru/wp-content/uploads/2018/11/MsTimer2.zip
#include <LiquidCrystal.h> // Входит в состав Arduino IDE
#include <Encoder.h> // http://rcl-radio.ru/wp-content/uploads/2019/05/Encoder.zip
#include <EEPROM.h> // Входит в состав Arduino IDE
#include <TDA7318.h> // http://rcl-radio.ru/wp-content/uploads/2021/08/TDA7318.zip
#include <FHT.h> // http://forum.rcl-radio.ru/misc.php?action=pan_download&item=297&download=1
#include <boarddefs.h> // Входит в состав библиотеки IRremote
#include <IRremote.h> // http://rcl-radio.ru/wp-content/uploads/2019/06/IRremote.zip
TDA7318 tda;
Encoder myEnc(9, 8);//CLK, DT подключение энкодера
decode_results ir;
IRrecv irrecv(12); // указываем вывод модуля IR приемника
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);// RS,E,D4,D5,D6,D7 подключение LCD
byte menu_set=100,menu,w2=1,z,z0,z1,w,w1,www,spek,mute,gr1,gr2;
int vol=0,fun_d,in=0,bass,treb,gain1,gain2,gain3,gain4,gain0,lf,rf,lr,rr,lf_old,rf_old,lr_old,rr_old,graf;
unsigned long newPosition,time,time1,oldPosition = -999;
//byte posOffset[16] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};// узкий спектр
byte posOffset[16] = {2,3,4,6,8,9,11,13,15,17,19,21,23,25,27,29};// средний спектр
//byte posOffset[16] = {2,3,4,6,8,10,12,14,16,20,25,30,35,60,80,100};// широкий спектр
byte gain_sp = DEF_GAIN;
unsigned long gainTimer;
byte maxValue, maxValue_f;
float k = 0.05;
int ur[32],urr[32],i1;
byte i,d1,d2,d3,d4,d5,d6,e1,e2,e3;
int a[3];
void setup(){
ADMUX = 0b11000000; ADCSRA = 0b11010110;
/*
ADCSRA [2:0] скорость аналогового входа
000 - CLK/2
001 - CLK/2
010 - CLK/4
011 - CLK/8
100 - CLK/16
101 - CLK/32
110 - CLK/64 '
111 - CLK/128
*/
irrecv.enableIRIn();Wire.begin();Serial.begin(9600);lcd.begin(16, 2);
lcd.setCursor(0,0);lcd.print(F(" TDA7318 "));delay(1000);cl();// ЗАСТАВКА
pinMode(10,INPUT); // КНОПКА ЭНКОДЕРA
pinMode(11,INPUT_PULLUP); // INPUT
pinMode(16,INPUT_PULLUP); // SET
pinMode(17,INPUT_PULLUP); // MUTE
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;
gain1 = EEPROM.read(3);gain2 = EEPROM.read(4);gain3 = EEPROM.read(5);gain4 = EEPROM.read(6);
lr = EEPROM.read(7);rr = EEPROM.read(8);lf = EEPROM.read(9);rf = EEPROM.read(10);graf = EEPROM.read(11);
switch(in){
case 0: gain0 = gain1;break;
case 1: gain0 = gain2;break;
case 2: gain0 = gain3;break;
case 3: gain0 = gain4;break;}
audio();
}
void loop(){
if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();time=millis();w=1;if(spek==1){cl();spek=0;menu=0;}delay(50);}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта
if (newPosition != oldPosition) {
if(spek==1){cl();spek=0;menu=0;}}
if(spek==0){
if(menu_set==100){
byte v1[8] = {0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07};
byte v2[8] = {0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00};
byte v3[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v4[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v5[8] = {0x1C,0x1C,0x00,0x00,0x00,0x00,0x1C,0x1C};
byte v6[8] = {0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C};
byte v7[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07};
byte v8[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00};
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);
}
if(menu==100){
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};
lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);
}}
if(spek==1||menu_set==5){
if(graf==0){
byte v1[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11011};
byte v2[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11011, 0b11011};
byte v3[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11011, 0b11011, 0b11011};
byte v4[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b11011, 0b11011, 0b11011, 0b11011};
byte v5[8] = {0b00000, 0b00000, 0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011};
byte v6[8] = {0b00000, 0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011};
byte v7[8] = {0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011};
byte v8[8] = {0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011};
lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);
lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);
}
if(graf==1){
byte v1[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01110};
byte v2[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110};
byte v3[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b01110};
byte v4[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b01110, 0b01110};
byte v5[8] = {0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110};
byte v6[8] = {0b00000, 0b00000, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110};
byte v7[8] = {0b00000, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110};
byte v8[8] = {0b01110, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110, 0b01110};
lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);
lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);}
if(graf==2){
byte v1[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01010};
byte v2[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01010, 0b01010};
byte v3[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01010, 0b01010, 0b01010};
byte v4[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b01010, 0b01010, 0b01010, 0b01010};
byte v5[8] = {0b00000, 0b00000, 0b00000, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010};
byte v6[8] = {0b00000, 0b00000, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010};
byte v7[8] = {0b00000, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010};
byte v8[8] = {0b01010, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010, 0b01010};
lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);
lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);}
if(graf==3){
byte v1[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b10101};
byte v2[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b10101, 0b10101};
byte v3[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b10101, 0b10101, 0b10101};
byte v4[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b10101, 0b10101, 0b10101, 0b10101};
byte v5[8] = {0b00000, 0b00000, 0b00000, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101};
byte v6[8] = {0b00000, 0b00000, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101};
byte v7[8] = {0b00000, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101};
byte v8[8] = {0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101, 0b10101};
lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);
lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);}
if(graf==4){
byte v1[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111};
byte v2[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111};
byte v3[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111};
byte v4[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111};
byte v5[8] = {0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
byte v6[8] = {0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
byte v7[8] = {0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
byte v8[8] = {0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
lcd.createChar(0, v1);lcd.createChar(1, v2);lcd.createChar(2, v3);lcd.createChar(3, v4);
lcd.createChar(4, v5);lcd.createChar(5, v6);lcd.createChar(6, v7);lcd.createChar(7, v8);}
}
if(mute==0){
if(((digitalRead(10)==LOW||ir.value==MENU)&&menu_set==100)){spek=0;menu++;cl();myEnc.write(0);time=millis();w=1;w1=1;if(menu>2){menu=0;}}// menu
if((digitalRead(10)==LOW&&menu==100)){spek=0;menu_set++;cl();myEnc.write(0);time=millis();w=1;w1=1;if(menu_set>5){menu_set=0;}}// menu
if(digitalRead(11)==LOW||ir.value==IN){in++;cl();myEnc.write(0);time=millis();w=1;w1=1;if(in>3){in=0;}lcd.setCursor(4,0);lcd.print("INPUT ");lcd.print(in+1);audio();delay(1500);cl();}// input
if(digitalRead(16)==LOW&&menu_set==100){menu_set=0;menu=100;spek=0;cl();myEnc.write(0);time=millis();w=1;w1=1;lcd.setCursor(4,0);lcd.print("SET MENU");delay(1500);cl();}// menu
if(digitalRead(16)==LOW&&menu_set<100){menu_set=100;menu=0;spek=0;cl();myEnc.write(0);time=millis();w=1;w1=1;lcd.setCursor(3,0);lcd.print("MAIN MENU");delay(1500);cl();}// menu
}
if((digitalRead(17)==LOW||ir.value==MUTE)&&mute==0){mute=1;spek=0;cl();lf_old=lf,rf_old=rf,lr_old=lr,rr_old=rr; lf=31,rf=31,lr=31,rr=31;menu=100;menu_set=100;myEnc.write(0);audio();lcd.setCursor(6,0);lcd.print("MUTE");}
if((digitalRead(17)==LOW||ir.value==MUTE)&&mute==1){mute=0;spek=0;cl();lf=lf_old,rf=rf_old,lr=lr_old,rr=rr_old;menu=0;w=1;myEnc.write(0);time=millis();audio();cl();}
//////// VOLUME ////////////////////////////////////////////////////////////////
if(menu==0){
if(ir.value==UP){vol++;gr1=1;gr2=0;cl1();time=millis();w=1;w1=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl1();time=millis();w=1;w1=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка >>>>>>
if(ir.value==DOWN){vol--;gr1=0;gr2=1;cl1();time=millis();w=1;w1=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl1();time=millis();w=1;w1=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
vol=vol+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(vol>63){vol=63;}if(vol<0){vol=0;}audio();}
lcd.setCursor(0,0);lcd.print(F("VOLUME "));
lcd.setCursor(0,1);lcd.print(F("INPUT "));lcd.print(in+1);
fun_d = 63-vol;
if(w==1){w=0;
a[0]=(fun_d)/10;a[1]=(fun_d)%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=10,e2=11,e3=12;break;
case 1: e1=13,e2=14,e3=15;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);
}}
}
/////// BASS ///////////////////////////////////////////////////////////////////
if(menu==1){
if(ir.value==DOWN){bass++;gr1=1;gr2=0;cl1();time=millis();w=1;w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl1();time=millis();w=1;w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка >>>>>>
if(ir.value==UP){bass--;gr1=0;gr2=1;cl1();time=millis();w=1;w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl1();time=millis();w=1;w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
bass=bass-newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(bass>7){bass=7;}if(bass<-7){bass=-7;}audio();}
lcd.setCursor(0,0);lcd.print(F("BASS "));
lcd.setCursor(14,1);lcd.print(F("dB"));
fun_d=bass*2;
if(fun_d<0){fun_d=abs(fun_d);lcd.setCursor(6,0);lcd.write((uint8_t)3);}else{lcd.setCursor(6,0);lcd.print(" ");}
if(w==1){w=0;
a[0]=(fun_d)/10;a[1]=(fun_d)%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=7,e2=8,e3=9;break;
case 1: e1=10,e2=11,e3=12;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);
}}}
/////// TREB ///////////////////////////////////////////////////////////////////
if(menu==2){
if(ir.value==DOWN){treb++;gr1=1;gr2=0;cl1();time=millis();w=1;w1=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка >
if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl1();time=millis();w=1;w1=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка >>>>>>
if(ir.value==UP){treb--;gr1=0;gr2=1;cl1();time=millis();w=1;w1=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка <
if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl1();time=millis();w=1;w1=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}// кнопка <<<<<<
if (newPosition != oldPosition){oldPosition = newPosition;
treb=treb-newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(treb>7){treb=7;}if(treb<-7){treb=-7;}audio();}
lcd.setCursor(0,0);lcd.print(F("TREB "));
lcd.setCursor(14,1);lcd.print(F("dB"));
fun_d=treb*2;
if(fun_d<0){fun_d=abs(fun_d);lcd.setCursor(6,0);lcd.write((uint8_t)3);}else{lcd.setCursor(6,0);lcd.print(" ");}
if(w==1){w=0;
a[0]=(fun_d)/10;a[1]=(fun_d)%10;
for(i=0;i<2;i++){
switch(i){
case 0: e1=7,e2=8,e3=9;break;
case 1: e1=10,e2=11,e3=12;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);
}}}
//////////////// IN + GAIN /////////////////////////////////////////////////////////////
if(menu_set==0){
switch(in){
case 0: gain0 = gain1;break;
case 1: gain0 = gain2;break;
case 2: gain0 = gain3;break;
case 3: gain0 = gain4;break;}
if (newPosition != oldPosition) {
oldPosition = newPosition;
gain0=gain0-newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(gain0>3){gain0=3;}if(gain0<0){gain0=0;}audio();}
switch(in){
case 0: gain1 = gain0;break;
case 1: gain2 = gain0;break;
case 2: gain3 = gain0;break;
case 3: gain4 = gain0;break;
}
if(www==1){audio();www=0;}
lcd.setCursor(0,0);lcd.print(F("Gain IN"));lcd.print(in+1);
lcd.setCursor(8,0);lcd.print(" ");lcd.print(gain0*6.25);lcd.print(" ");lcd.setCursor(14,0);lcd.print("dB");fun_d=(gain0+1)*3;
if(w==1){
for(z=0,z0=0,z1=0;z<=fun_d;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0+6,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1+6,1);lcd.print(" ");}}
if(z1==3){lcd.setCursor(z0+6,1);lcd.write((uint8_t)1);}
if(z1==2){lcd.setCursor(z0+6,1);lcd.write((uint8_t)2);}}w=0;}
//////// LF ////////////////////////////////////////////////////////////////
if(menu_set==1){
if (newPosition != oldPosition){oldPosition = newPosition;
lf=lf+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(lf>31){lf=31;}if(lf<0){lf=0;}audio();}
lcd.setCursor(0,0);lcd.print(F("ATT LF "));
lcd.setCursor(9,0);lcd.print(lf);lcd.print(" ");lcd.setCursor(12,0);lcd.print("dB ");
fun_d = 31-lf;
if(w==1){
for(z=0,z0=0,z1=0;z<=fun_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);}w=0;}}
//////// LF ////////////////////////////////////////////////////////////////
if(menu_set==2){
if (newPosition != oldPosition){oldPosition = newPosition;
rf=rf+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(rf>31){rf=31;}if(rf<0){rf=0;}audio();}
lcd.setCursor(0,0);lcd.print(F("ATT RF "));
lcd.setCursor(9,0);lcd.print(rf);lcd.print(" ");lcd.setCursor(12,0);lcd.print("dB ");
fun_d = 31-rf;
if(w==1){
for(z=0,z0=0,z1=0;z<=fun_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);}w=0;}}
//////// LR ////////////////////////////////////////////////////////////////
if(menu_set==3){
if (newPosition != oldPosition){oldPosition = newPosition;
lr=lr+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(lr>31){lr=31;}if(lr<0){lr=0;}audio();}
lcd.setCursor(0,0);lcd.print(F("ATT LR "));
lcd.setCursor(9,0);lcd.print(lr);lcd.print(" ");lcd.setCursor(12,0);lcd.print("dB ");
fun_d = 31-lr;
if(w==1){
for(z=0,z0=0,z1=0;z<=fun_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);}w=0;}}
//////// RR ////////////////////////////////////////////////////////////////
if(menu_set==4){
if (newPosition != oldPosition){oldPosition = newPosition;
rr=rr+newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(rr>31){rr=31;}if(rr<0){rr=0;}audio();}
lcd.setCursor(0,0);lcd.print(F("ATT RR "));
lcd.setCursor(9,0);lcd.print(rr);lcd.print(" ");lcd.setCursor(12,0);lcd.print("dB ");
fun_d = 31-rr;
if(w==1){
for(z=0,z0=0,z1=0;z<=fun_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);}w=0;}}
///// set graf /////////////////////////////
if(menu_set==5){
if (newPosition != oldPosition){oldPosition = newPosition;
graf=graf-newPosition;myEnc.write(0);newPosition=0;time=millis();w=1;w1=1;if(graf>4){graf=0;}if(graf<0){graf=4;}}
lcd.setCursor(0,0);lcd.print(F("Graphics "));
lcd.setCursor(9,0);lcd.print(graf);lcd.print(" ");
lcd.setCursor(5,1);lcd.write((uint8_t)7);lcd.write((uint8_t)7);lcd.write((uint8_t)7);lcd.write((uint8_t)7);lcd.write((uint8_t)7);lcd.write((uint8_t)7);
}
//////// EEPROM //////////////////////////////////////////////////
if(millis()-time>10000 && w1==1&&mute==0){
EEPROM.update(0,vol);EEPROM.update(1,bass+7);EEPROM.update(2,treb+7);EEPROM.update(3,gain1);
EEPROM.update(4,gain2);EEPROM.update(5,gain3);EEPROM.update(6,gain4);
EEPROM.update(7,lr);EEPROM.update(8,rr);EEPROM.update(9,lf);EEPROM.update(10,rf);EEPROM.update(11,graf);
menu_set=100;menu=100;lcd.clear();w=1;w1=0;spek=1;}
/////// SPECTR /////////////////////////////////////////////////////////
if(spek==1){
analyzeAudio();
for (int pos = 0; pos < 16; pos++) {
if (fht_log_out[posOffset[pos]] > maxValue) maxValue = fht_log_out[posOffset[pos]];
lcd.setCursor(pos, 0);
int posLevel = map(fht_log_out[posOffset[pos]], LOW_PASS, gain_sp*0.9, 0, 15);posLevel = constrain(posLevel, 0, 15);
urr[pos] = posLevel;
if(urr[pos]<ur[pos]){ur[pos]=ur[pos]-1;}
else{ur[pos] = posLevel;}
if (ur[pos] > 7){lcd.write((uint8_t)ur[pos] - 8);lcd.setCursor(pos, 1);lcd.write((uint8_t)7);}
else {lcd.print(" ");lcd.setCursor(pos, 1);lcd.write((uint8_t)ur[pos]);}
}
if (AUTO_GAIN) {
if (millis() - gainTimer > 10) {
maxValue_f = maxValue * k + maxValue_f * (1 - k);
if(maxValue_f > LOW_PASS) gain_sp = maxValue_f;else gain_sp = DEF_GAIN;gainTimer = millis();}}
}/// end spec
}// loop
void cl(){ir.value=0;delay(300);lcd.clear();}
void cl1(){ir.value=0;delay(200);}
void audio(){
tda.setVolume(vol); // громкость 0...63
tda.setAttLR(lr); // аттенюатор LR 0...31
tda.setAttRR(rr); // аттенюатор RR 0...31
tda.setAttLF(lf); // аттенюатор LF 0...31
tda.setAttRF(rf); // аттенюатор RF 0...31
tda.setSwitch(in,gain0); // вход 0...3, усиление 0...3
tda.setBass(bass); // тембр НЧ -7...+7
tda.setTreble(treb); // тембр ВЧ -7...+7
}
void to_Timer(){newPosition = myEnc.read()/4;}
void analyzeAudio() {
while(i1 < FHT_N){i1++;
do{ADCSRA |= (1 << ADSC);}
while((ADCSRA & (1 << ADIF)) == 0);fht_input[i1] = (ADCL|ADCH << 8);}i1=0;
fht_window(); // window the data for better frequency response
fht_reorder(); // reorder the data before doing the fht
fht_run(); // process the data in the fht
fht_mag_log(); // take the output of the fht
}