#include <Wire.h>
//#include <TDA8425.h>
#include <Encoder.h>
#include <LiquidCrystal.h>
#include <EEPROM.h> //
// #include <EEPROMex.h>
//#include <MsTimer2.h>
Encoder myEnc ( 11, 10 ) ; //CLK, DT
TDA8425 tda ;
LiquidCrystal lcd ( 7, 6, 2, 3, 4, 5 ) ; // RS,E,D4,D5,D6,D7
byte a1 [ 8 ] = { 0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b00000 } ;
byte a2 [ 8 ] = { 0b00000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b00000 } ;
int z, vol, vol_d, balans, bass, bass_d, treb, treb_d, in, mute, sss, db ;
byte menu, w1 ;
long oldPosition = - 999, newPosition ;
unsigned long time ;
void to_Timer ( ) { //timer function
newPosition = myEnc . read ( ) / 4 ;
}
void setup() {
Serial . begin ( 9600 ) ;
lcd . begin ( 16, 2 ) ;
pinMode ( 12, INPUT ) ; //menu encoder button SW
lcd . createChar ( 0, a1 ) ; lcd . createChar ( 1, a2 ) ;
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 ; // balance eeprom
audio ( ) ;
lcd . setCursor ( 4, 0 ) ; lcd . print ( "TDA8425" ) ; delay ( 500 ) ;
MsTimer2 :: set ( 1, to_Timer ) ; MsTimer2 :: start ( ) ;
}
void loop() {
if ( digitalRead ( 12 ) == LOW && menu < 3 ) {
sss ++ ; // menu
menu ++ ;
myEnc . write ( 0 ) ;
oldPosition = - 999 ;
delay ( 300 ) ;
time = millis ( ) ;
w1 = 1 ;
if ( menu > 2 ) {
menu = 0 ;
} if ( sss > 2 ) {
menu = 3 ;
delay ( 300 ) ;
}
}
if ( digitalRead ( 12 ) == HIGH ) {
sss = 0 ;
}
if ( menu == 3 && digitalRead ( 12 ) == LOW && sss == 0 ) {
menu = 4 ;
oldPosition = - 999 ;
delay ( 300 ) ;
time = millis ( ) ;
w1 = 1 ;
lcd . clear ( ) ;
}
if ( menu == 4 && digitalRead ( 12 ) == LOW ) {
menu = 0 ;
delay ( 300 ) ;
lcd . clear ( ) ;
}
//////////////////////// volume /////////////////////// //////
if ( menu == 0 ) {
if ( newPosition != oldPosition ) {
oldPosition = newPosition ;
vol = vol + newPosition ; myEnc . write ( 0 ) ; newPosition = 0 ; lcd . clear ( ) ; time = millis ( ) ; w1 = 1 ; if ( vol > 63 ) {
vol = 63 ;
} if ( vol < 16 ) {
vol = 16 ;
} audio ( ) ;
}
lcd . setCursor ( 0, 0 ) ; lcd . print ( "Volume" ) ;
vol_d = vol * 2 - 120 ;
if ( vol_d >= 0 ) {
lcd . print ( "+" ) ;
} else {
lcd . print ( "-" ) ;
}
if ( vol_d > - 10 ) {
{
lcd . print ( "" ) ;
}
}
lcd . print ( abs ( vol_d ) ) ; lcd . print ( "dB" ) ;
// volume slider starts at -56 dB
if ( vol - 32 > 0 ) {
for ( z = 0 ; z <= vol - 33 ; z ++ ) {
lcd . setCursor ( z / 2, 1 ) ;
lcd . write ( ( uint8_t ) 0 ) ;
}
}
if ( ( vol - 32 ) % 2 == 0 && vol - 32 >= 0 ) {
lcd . setCursor ( z / 2, 1 ) ;
lcd . write ( ( uint8_t ) 1 ) ;
}
}
///////////////////////// balance ////////////////////// ///////
if ( menu == 3 ) {
if ( newPosition != oldPosition ) {
oldPosition = newPosition ;
balans = balans + newPosition ; myEnc . write ( 0 ) ; newPosition = 0 ; lcd . clear ( ) ; time = millis ( ) ; w1 = 1 ; if ( balans > 4 ) {
balans = 4 ;
} if ( balans < - 4 ) {
balans = - 4 ;
} 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 ( balances > 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 ) ;
}
}
//////////////////////// bass timbre ////////////////////// ////////
if ( menu == 1 ) {
if ( newPosition != oldPosition ) {
oldPosition = newPosition ;
bass = bass + newPosition ; myEnc . write ( 0 ) ; newPosition = 0 ; lcd . clear ( ) ; time = millis ( ) ; w1 = 1 ; if ( bass > 11 ) {
bass = 11 ;
} if ( bass < 2 ) {
bass = 2 ;
} audio ( ) ;
}
lcd . setCursor ( 0, 0 ) ; lcd . print ( "Bass" ) ;
bass_d = bass * 3 - 18 ;
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 ( bass > 1 ) {
for ( z = 3 ; z <= bass + 1 ; z ++ ) {
lcd . setCursor ( z, 1 ) ;
lcd . write ( ( uint8_t ) 0 ) ;
}
}
}
//////////////////////// treble tone ///////////////////// /////////
if ( menu == 2 ) {
if ( newPosition != oldPosition ) {
oldPosition = newPosition ;
treb = treb + newPosition ; myEnc . write ( 0 ) ; newPosition = 0 ; lcd . clear ( ) ; time = millis ( ) ; w1 = 1 ; if ( treb > 11 ) {
treb = 11 ;
} if ( treb < 2 ) {
treb = 2 ;
} audio ( ) ;
}
if ( treb > 10 ) {
treb = 10 ;
} if ( treb < 2 || treb == 255 ) {
treb = 2 ;
}
lcd . setCursor ( 0, 0 ) ; lcd . print ( "Treble" ) ;
treb_d = treb * 3 - 18 ;
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 ( treb > 1 ) {
for ( z = 3 ; z <= treb + 1 ; z ++ ) {
lcd . setCursor ( z, 1 ) ;
lcd . write ( ( uint8_t ) 0 ) ;
}
}
}
//////////////////////// input ///////////////////// ////////
if ( menu == 4 ) {
if ( newPosition != oldPosition ) {
oldPosition = newPosition ;
in = in + newPosition; myEnc . write ( 0 ) ; newPosition = 0 ; lcd . clear ( ) ; time = millis ( ) ; w1 = 1 ; if ( in > 1 ) {
in = 0 ;
} if ( in < 0 ) {
in = 1 ;
} audio ( ) ;
}
lcd . setCursor ( 0, 0 ) ; lcd . print ( "Source selector" ) ;
lcd . setCursor ( 0, 1 ) ; lcd . print ( "Input: " ) ; lcd . print ( in ) ;
}
////////////////// mute ///////////////////////////// ////
if ( digitalRead ( 9 ) == HIGH ) {
mute ++ ; if ( mute > 1 ) {
mute = 0 ;
}
if ( mute == 1 ) {
menu = - 1 ;
} else {
oldPosition = - 999 ;
menu = 0 ;
}
audio ( ) ; delay ( 300 ) ; lcd . clear ( ) ;
lcd . setCursor ( 6, 0 ) ; lcd . print ( "MUTE" ) ;
}
//////////////////////////////////////////////// //////////
/////////////////// -20 dB //////////////////////////// ///
if ( digitalRead ( 8 ) == HIGH && db == 0 ) {
vol = vol - 10 ;
db = 1 ;
delay ( 500 ) ;
audio ( ) ;
myEnc . write ( 0 ) ;
menu = 0 ;
lcd . clear ( ) ;
}
if ( digitalRead ( 8 ) == HIGH && db == 1 ) {
vol = vol + 10 ;
db = 0 ;
delay ( 500 ) ;
audio ( ) ;
myEnc . write ( 0 ) ;
menu = 0 ;
lcd . clear ( ) ;
}
//////////////////////////////////////////////// /////////
if ( millis ( ) - time > 10000 && w1 == 1 ) { // saving all settings in the eeprom after 60 seconds of inactivity
myEnc . write ( 0 ) ;
EEPROM update ( 0, vol ) ;
EEPROM update ( 4, balances + 4 ) ;
EEPROM update ( 1, bass ) ;
EEPROM update ( 2, treb ) ;
EEPROM update ( 3, in ) ;
w1 = 0 ; menu = 0 ; lcd . clear ( ) ;
}
} // loop
void audio ( ) {
tda . setVolumeL ( vol + balans ) ;
tda . setVolumeR ( vol - balans ) ;
tda . setBass ( bass ) ;
tda . setTreble ( treb ) ;
tda . setMute ( mute ) ;
tda . setSource ( in ) ;
}