Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v1.0
- // v1.1 added input channel name
- #define ENCODER_DO_NOT_USE_INTERRUPTS
- #define encA 2
- #define encB 3
- #define menu_key 12 // encoder pushbutton
- #define mute_key 11
- #include <Encoder.h>
- #include <Wire.h>
- #include <TDA7439.h>
- #include <LiquidCrystal_I2C.h>
- #include <EEPROM.h>
- TDA7439 tda;
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
- //LiquidCrystal_I2C lcd(0x27, 16, 2);
- byte a1[8] = {0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b00000};
- byte a2[8] = {0b00000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b00000};
- int vol, vol_d, volTmp, z, bass, mids, treb, balans, in, gain, gain1, gain2, gain3, gain4;
- byte menu ;
- float encPos = 0 ;
- int16_t oldPos=0, tmp;
- int8_t dir=0;
- unsigned long times;
- bool saveOk=true, waitOk=true, muteOk=true;
- char inputName[4][7]= {"RADIO","DVD","TV","MIC"};
- Encoder myEnc(encA, encB);
- void setup() {
- Serial.begin(115200); // Only for debug
- //Serial.println("TDA7439");
- lcd.begin(16,2);
- lcd.backlight();
- //lcd.init();
- lcd.clear();
- lcd.print(" TDA7439");
- delay(1000);
- pinMode(menu_key, INPUT_PULLUP); // menu
- pinMode(mute_key, INPUT_PULLUP); // mute
- lcd.createChar(0, a1);
- lcd.createChar(1, a2);
- vol = EEPROM.read(0);
- bass = EEPROM.read(1) - 7;
- mids = EEPROM.read(2) - 7;
- treb = EEPROM.read(3) - 7;
- balans = EEPROM.read(4) - 4;
- in = EEPROM.read(5);
- gain1 = EEPROM.read(6);
- gain2 = EEPROM.read(7);
- gain3 = EEPROM.read(8);
- gain4 = EEPROM.read(9);
- if (vol>48) { // if eeprom empty
- vol = 0;
- bass = 0;
- mids = 0;
- treb = 0;
- balans = 4;
- in = 1;
- gain1 = 0;
- gain2 = 0;
- gain3 = 0;
- gain4 = 0;
- }
- audio();
- displayWrite(0);
- }
- void loop() {
- if (digitalRead(mute_key) == LOW and muteOk==true) {
- muteOk=false;
- tda.setVolume(0);
- lcd.clear();
- lcd.print(" MUTE");
- delay(100);
- while (digitalRead(mute_key) == LOW) {}
- }
- if (digitalRead(mute_key) == LOW) {
- muteOk=true;
- menu=0;
- volTmp=vol;
- for (vol=0;vol<volTmp;vol++) {
- tda.setVolume(vol);
- displayWrite(menu);
- delay(100);
- }
- dir=0;
- displayWrite(menu);
- while (digitalRead(mute_key) == LOW) {}
- }
- if (times+10000<millis() and saveOk==false) { // if nothing happens for 10 sec, then save data
- saveData();
- }
- float newPos = myEnc.read();
- if (newPos != encPos) {
- encPos = newPos;
- tmp=encPos;
- if (tmp/4-encPos/4==0) { // the value depends on the encoder type, typically 4 is appropriate
- if (oldPos<encPos) {
- dir=1;
- } else {
- dir=-1;
- }
- modValue(menu,dir);
- displayWrite(menu);
- audio();
- oldPos=encPos;
- }
- }
- if (digitalRead(menu_key) == LOW) {
- times=millis();
- delay(100);
- muteOk=true;
- waitOk=true;
- while (digitalRead(menu_key) == LOW){
- if (times+2000<millis() and waitOk==true) { // After 2 seconds of hold, the main menu changes
- waitOk=false;
- lcd.clear();
- lcd.print(" waiting ...");
- }
- }
- if (times+2000<millis()) {
- if (menu==10) {
- menu=4;
- } else {
- menu=10;
- in=0;
- }
- }
- if (menu != 10) menu++;
- if (menu == 5) menu = 0;
- if (menu == 10) in++;
- if (in==5) in=1;
- audio();
- displayWrite(menu);
- delay(200);
- }
- }
- void modValue(uint8_t sz, int8_t ir) {
- times=millis();
- muteOk=true;
- saveOk=false;
- switch (sz) {
- case 0:
- vol=vol+ir;
- if (vol > 48) vol = 48;
- if (vol < 0) vol = 0;
- break;
- case 1:
- bass=bass+ir;
- if (bass > 7) bass = 7;
- if (bass < -7) bass = -7;
- break;
- case 2:
- mids=mids+ir;
- if (mids > 7) mids = 7;
- if (mids < -7) mids = -7;
- break;
- case 3:
- treb=treb+ir;
- if (treb > 7) treb = 7;
- if (treb < -7) treb = -7;
- break;
- case 4:
- balans=balans+ir;
- if (balans > 4) balans = 4;
- if (balans < -4) balans = -4;
- break;
- case 10:
- g1();
- gain=gain+ir;
- if (gain > 15) gain = 15;
- if (gain < 0) gain = 0;
- g2();
- break;
- }
- audio();
- }
- void saveData() {
- saveOk=true;
- //Serial.println("SAVE");
- EEPROM.update(0, vol);
- EEPROM.update(4, balans + 4);
- EEPROM.update(1, bass + 7);
- EEPROM.update(2, mids + 7);
- EEPROM.update(3, treb + 7);
- EEPROM.update(5, in);
- EEPROM.update(6, gain1);
- EEPROM.update(7, gain2);
- EEPROM.update(8, gain3);
- EEPROM.update(9, gain4);
- menu=0;
- displayWrite(menu);
- }
- void displayWrite (int sz) {
- lcd.clear();
- switch (sz) {
- case 0:
- lcd.setCursor(0, 0);
- lcd.print("Volume ");
- lcd.print(-48 + vol);
- lcd.setCursor(13, 0);
- lcd.print("dB");
- vol_d = vol / 2;
- if (vol_d >= 0) {
- for (z = 0; z <= vol_d; z++) {
- lcd.setCursor(z / 2, 1);
- lcd.write((uint8_t)0);
- }
- }
- if (vol_d % 2 == 0) {
- lcd.setCursor(z / 2, 1);
- lcd.write((uint8_t)1);
- }
- lcd.setCursor(13, 1);
- lcd.print(vol);
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print("Bass ");
- lcd.print(bass * 2);
- lcd.setCursor(13, 0);
- lcd.print("dB");
- for (z = -7; z <= bass; z++) {
- lcd.setCursor(z + 7, 1);
- lcd.write((uint8_t)0);
- }
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print("Mids ");
- lcd.print(mids * 2);
- lcd.setCursor(13, 0);
- lcd.print("dB");
- for (z = -7; z <= mids; z++) {
- lcd.setCursor(z + 7, 1);
- lcd.write((uint8_t)0);
- }
- break;
- case 3:
- lcd.setCursor(0, 0);
- lcd.print("Treble ");
- lcd.print(treb * 2);
- lcd.setCursor(13, 0);
- lcd.print("dB");
- for (z = -7; z <= treb; z++) {
- lcd.setCursor(z + 7, 1);
- lcd.write((uint8_t)0);
- }
- break;
- case 4:
- lcd.setCursor(0, 0);
- if (balans >= 0) {
- lcd.print("-");
- } else {
- lcd.print("+");
- }
- lcd.print(abs(balans));
- lcd.print(" dB ");
- lcd.print(" <> ");
- if (balans >= 0) {
- lcd.print("+");
- } else {
- lcd.print("-");
- }
- lcd.print(abs(balans));
- lcd.print(" dB ");
- lcd.setCursor(0, 1);
- lcd.print("R");
- lcd.setCursor(15, 1);
- lcd.print("L");
- if (balans < 0) {
- lcd.setCursor(balans + 7, 1);
- lcd.write((uint8_t)0);
- }
- if (balans > 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);
- }
- break;
- case 10:
- g1();
- lcd.print("Input: ");
- lcd.print(inputName[in-1]);
- lcd.setCursor(0, 1);
- lcd.print("Input Gain: ");
- lcd.print(gain);
- break;
- }
- }
- void g1() {
- if (in == 1) gain = gain1;
- if (in == 2) gain = gain2;
- if (in == 3) gain = gain3;
- if (in == 4) gain = gain4;
- }
- void g2() {
- if (in == 1) gain1 = gain;
- if (in == 2) gain2 = gain;
- if (in == 3) gain3 = gain;
- if (in == 4) gain4 = gain;
- }
- void audio() {
- tda.setInput(in);
- tda.inputGain(gain); // 0 to 15
- tda.setVolume(vol); // 0 to 48 ( 0 is mute)
- tda.setSnd(bass, 1); //-7 to +7 , 1 - Bass | 2 - Mids | 3 - Trebble
- tda.setSnd(mids, 2);
- tda.setSnd(treb, 3);
- tda.spkAtt(balans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement