Advertisement
Szerelo

TDA7439 encoder LCD 16*2

Mar 27th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.56 KB | None | 0 0
  1. // v1.0
  2. // v1.1 added input channel name
  3.  
  4. #define ENCODER_DO_NOT_USE_INTERRUPTS
  5. #define encA 2
  6. #define encB 3
  7. #define menu_key 12 // encoder pushbutton
  8. #define mute_key 11
  9. #include <Encoder.h>
  10. #include <Wire.h>
  11. #include <TDA7439.h>
  12. #include <LiquidCrystal_I2C.h>
  13. #include <EEPROM.h>
  14.  
  15. TDA7439 tda;
  16. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
  17. //LiquidCrystal_I2C lcd(0x27, 16, 2);
  18. byte a1[8] = {0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b00000};
  19. byte a2[8] = {0b00000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b00000};
  20. int vol, vol_d, volTmp, z, bass, mids, treb, balans, in, gain, gain1, gain2, gain3, gain4;
  21. byte menu ;
  22. float encPos  = 0 ;
  23. int16_t oldPos=0, tmp;
  24. int8_t dir=0;
  25. unsigned long times;
  26. bool saveOk=true, waitOk=true, muteOk=true;
  27. char inputName[4][7]= {"RADIO","DVD","TV","MIC"};
  28.  
  29. Encoder myEnc(encA, encB);
  30.  
  31. void setup() {
  32.   Serial.begin(115200);   // Only for debug
  33.   //Serial.println("TDA7439");
  34.   lcd.begin(16,2);
  35.   lcd.backlight();
  36.   //lcd.init();
  37.   lcd.clear();
  38.   lcd.print("    TDA7439");
  39.   delay(1000);
  40.   pinMode(menu_key, INPUT_PULLUP); // menu
  41.   pinMode(mute_key, INPUT_PULLUP); // mute
  42.   lcd.createChar(0, a1);
  43.   lcd.createChar(1, a2);
  44.   vol = EEPROM.read(0);
  45.   bass = EEPROM.read(1) - 7;
  46.   mids = EEPROM.read(2) - 7;
  47.   treb = EEPROM.read(3) - 7;
  48.   balans = EEPROM.read(4) - 4;
  49.   in = EEPROM.read(5);
  50.   gain1 = EEPROM.read(6);
  51.   gain2 = EEPROM.read(7);
  52.   gain3 = EEPROM.read(8);
  53.   gain4 = EEPROM.read(9);
  54.   if (vol>48) { // if eeprom empty
  55.     vol = 0;
  56.     bass = 0;
  57.     mids = 0;
  58.     treb = 0;
  59.     balans = 4;
  60.     in = 1;
  61.     gain1 = 0;
  62.     gain2 = 0;
  63.     gain3 = 0;
  64.     gain4 = 0;
  65.   }
  66.   audio();
  67.   displayWrite(0);
  68. }
  69.  
  70. void loop() {
  71.   if (digitalRead(mute_key) == LOW and muteOk==true) {
  72.     muteOk=false;
  73.     tda.setVolume(0);
  74.     lcd.clear();
  75.     lcd.print("      MUTE");
  76.     delay(100);
  77.     while (digitalRead(mute_key) == LOW) {}
  78.   }
  79.   if (digitalRead(mute_key) == LOW) {
  80.     muteOk=true;
  81.     menu=0;
  82.     volTmp=vol;
  83.     for (vol=0;vol<volTmp;vol++) {
  84.       tda.setVolume(vol);
  85.       displayWrite(menu);
  86.       delay(100);
  87.     }
  88.     dir=0;
  89.     displayWrite(menu);
  90.     while (digitalRead(mute_key) == LOW) {}
  91.   }
  92.   if (times+10000<millis() and saveOk==false) {   // if nothing happens for 10 sec, then save data
  93.     saveData();
  94.   }
  95.   float newPos = myEnc.read();
  96.   if (newPos != encPos) {
  97.     encPos = newPos;
  98.     tmp=encPos;
  99.     if (tmp/4-encPos/4==0) {  // the value depends on the encoder type, typically 4 is appropriate
  100.       if (oldPos<encPos) {
  101.         dir=1;
  102.       } else {
  103.         dir=-1;
  104.       }
  105.       modValue(menu,dir);
  106.       displayWrite(menu);
  107.       audio();
  108.       oldPos=encPos;
  109.     }
  110.   }
  111.   if (digitalRead(menu_key) == LOW) {
  112.     times=millis();
  113.     delay(100);
  114.     muteOk=true;
  115.     waitOk=true;
  116.     while (digitalRead(menu_key) == LOW){
  117.       if (times+2000<millis() and waitOk==true) {  // After 2 seconds of hold, the main menu changes
  118.         waitOk=false;
  119.         lcd.clear();
  120.         lcd.print("   waiting ...");
  121.       }
  122.     }
  123.     if (times+2000<millis()) {
  124.       if (menu==10) {
  125.         menu=4;
  126.       } else {
  127.         menu=10;
  128.         in=0;
  129.       }
  130.     }
  131.     if (menu != 10) menu++;
  132.     if (menu == 5) menu = 0;
  133.     if (menu == 10) in++;
  134.     if (in==5) in=1;
  135.     audio();
  136.     displayWrite(menu);
  137.     delay(200);
  138.   }
  139. }
  140.  
  141. void modValue(uint8_t sz, int8_t ir) {
  142.   times=millis();
  143.   muteOk=true;
  144.   saveOk=false;
  145.   switch (sz) {
  146.     case 0:
  147.       vol=vol+ir;
  148.       if (vol > 48) vol = 48;
  149.       if (vol < 0) vol = 0;
  150.       break;
  151.     case 1:
  152.       bass=bass+ir;
  153.       if (bass >  7) bass =  7;
  154.       if (bass < -7) bass = -7;
  155.       break;
  156.     case 2:
  157.       mids=mids+ir;
  158.       if (mids >  7) mids =  7;
  159.       if (mids < -7) mids = -7;
  160.       break;
  161.     case 3:
  162.       treb=treb+ir;
  163.       if (treb >  7) treb =  7;
  164.       if (treb < -7) treb = -7;
  165.       break;
  166.     case 4:
  167.       balans=balans+ir;
  168.       if (balans >  4) balans =  4;
  169.       if (balans < -4) balans = -4;
  170.       break;
  171.     case 10:
  172.       g1();
  173.       gain=gain+ir;
  174.       if (gain >  15) gain =  15;
  175.       if (gain < 0) gain = 0;
  176.       g2();
  177.       break;
  178.   }  
  179.   audio();
  180. }
  181.  
  182. void saveData() {
  183.   saveOk=true;
  184.   //Serial.println("SAVE");
  185.   EEPROM.update(0, vol);
  186.   EEPROM.update(4, balans + 4);
  187.   EEPROM.update(1, bass + 7);
  188.   EEPROM.update(2, mids + 7);
  189.   EEPROM.update(3, treb + 7);
  190.   EEPROM.update(5, in);
  191.   EEPROM.update(6, gain1);
  192.   EEPROM.update(7, gain2);
  193.   EEPROM.update(8, gain3);
  194.   EEPROM.update(9, gain4);
  195.   menu=0;
  196.   displayWrite(menu);
  197. }
  198.  
  199. void displayWrite (int sz) {
  200.   lcd.clear();
  201.   switch (sz) {
  202.     case 0:
  203.       lcd.setCursor(0, 0);
  204.       lcd.print("Volume ");
  205.       lcd.print(-48 + vol);
  206.       lcd.setCursor(13, 0);
  207.       lcd.print("dB");
  208.       vol_d = vol / 2;
  209.       if (vol_d >= 0) {
  210.         for (z = 0; z <= vol_d; z++) {
  211.           lcd.setCursor(z / 2, 1);
  212.           lcd.write((uint8_t)0);
  213.         }
  214.       }
  215.       if (vol_d % 2 == 0) {
  216.         lcd.setCursor(z / 2, 1);
  217.         lcd.write((uint8_t)1);
  218.       }
  219.       lcd.setCursor(13, 1);
  220.       lcd.print(vol);
  221.       break;
  222.     case 1:
  223.       lcd.setCursor(0, 0);
  224.       lcd.print("Bass ");
  225.       lcd.print(bass * 2);
  226.       lcd.setCursor(13, 0);
  227.       lcd.print("dB");
  228.       for (z = -7; z <= bass; z++) {
  229.         lcd.setCursor(z + 7, 1);
  230.         lcd.write((uint8_t)0);
  231.       }
  232.       break;
  233.     case 2:
  234.       lcd.setCursor(0, 0);
  235.       lcd.print("Mids ");
  236.       lcd.print(mids * 2);
  237.       lcd.setCursor(13, 0);
  238.       lcd.print("dB");
  239.       for (z = -7; z <= mids; z++) {
  240.         lcd.setCursor(z + 7, 1);
  241.         lcd.write((uint8_t)0);
  242.       }
  243.       break;
  244.     case 3:
  245.       lcd.setCursor(0, 0);
  246.       lcd.print("Treble ");
  247.       lcd.print(treb * 2);
  248.       lcd.setCursor(13, 0);
  249.       lcd.print("dB");
  250.       for (z = -7; z <= treb; z++) {
  251.         lcd.setCursor(z + 7, 1);
  252.         lcd.write((uint8_t)0);
  253.       }
  254.       break;
  255.     case 4:
  256.       lcd.setCursor(0, 0);
  257.       if (balans >= 0) {
  258.         lcd.print("-");
  259.       } else {
  260.         lcd.print("+");
  261.       }
  262.       lcd.print(abs(balans));
  263.       lcd.print(" dB ");
  264.       lcd.print(" <> ");
  265.       if (balans >= 0) {
  266.         lcd.print("+");
  267.       } else {
  268.         lcd.print("-");
  269.       }
  270.       lcd.print(abs(balans));
  271.       lcd.print(" dB ");
  272.       lcd.setCursor(0, 1);
  273.       lcd.print("R");
  274.       lcd.setCursor(15, 1);
  275.       lcd.print("L");
  276.       if (balans < 0) {
  277.         lcd.setCursor(balans + 7, 1);
  278.         lcd.write((uint8_t)0);
  279.       }
  280.       if (balans > 0) {
  281.         lcd.setCursor(balans + 8, 1);
  282.         lcd.write((uint8_t)0);
  283.       }
  284.       if (balans == 0) {
  285.         lcd.setCursor(7, 1);
  286.         lcd.write((uint8_t)0);
  287.         lcd.setCursor(8, 1);
  288.         lcd.write((uint8_t)0);
  289.       }
  290.       break;
  291.     case 10:
  292.       g1();
  293.       lcd.print("Input: ");
  294.       lcd.print(inputName[in-1]);
  295.       lcd.setCursor(0, 1);
  296.       lcd.print("Input Gain: ");
  297.       lcd.print(gain);
  298.       break;
  299.   }  
  300. }
  301.  
  302. void g1() {
  303.   if (in == 1) gain = gain1;
  304.   if (in == 2) gain = gain2;
  305.   if (in == 3) gain = gain3;
  306.   if (in == 4) gain = gain4;
  307. }
  308.  
  309. void g2() {
  310.   if (in == 1) gain1 = gain;
  311.   if (in == 2) gain2 = gain;
  312.   if (in == 3) gain3 = gain;
  313.   if (in == 4) gain4 = gain;
  314. }
  315.  
  316.  
  317. void audio() {
  318.   tda.setInput(in);
  319.   tda.inputGain(gain); // 0 to 15
  320.   tda.setVolume(vol); // 0 to 48 ( 0 is mute)
  321.   tda.setSnd(bass, 1); //-7 to +7 , 1 - Bass | 2 - Mids | 3 - Trebble
  322.   tda.setSnd(mids, 2);
  323.   tda.setSnd(treb, 3);
  324.   tda.spkAtt(balans);
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement