Advertisement
GaabMM88

Untitled

Mar 8th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.93 KB | None | 0 0
  1. // v1.0
  2. // v1.1 added input channel name
  3. // v1.2 added control buttons, leds and rtc
  4.  
  5. #define ENCODER_DO_NOT_USE_INTERRUPTS
  6. #define encA 2                            // encoder A
  7. #define encB 3                            // encoder B
  8. #define ok_button 4                       // encoder gomb
  9. #define MenuButton 5                      // menu gomb
  10. #define MuteButton 6                      // mute gomb
  11. #define MuteLed 7                         // mute led
  12. #define PCF1 0x26
  13. #define SelectInput1 in[0]
  14. #define SelectInput2 in[1]
  15. #define SelectInput3 in[2]
  16. #define SelectInput4 in[3]
  17. #define InputLed1 out[0]
  18. #define InputLed2 out[1]
  19. #define InputLed3 out[2]
  20. #define InputLed4 out[3]
  21. #define PCF2 0x27
  22. #define SelectAmp1 in[4]
  23. #define SelectAmp2 in[5]
  24. #define SelectAmp3 in[6]
  25. #define PowerOn in[7]
  26. #define Amp1Led out[4]
  27. #define Amp2Led out[5]
  28. #define Amp3Led out[6]
  29. #define PowerLed 8                          // If power ON then LED is GREEN
  30. #define PowerLed 9                          // If power OFF then LED is RED
  31. #define Amp1Relay A0                        // Amplifier 1 relay on ULN2003A IN1
  32. #define Amp2Relay A1                        // Amplifier 2 relay on ULN2003A IN2
  33. #define Amp3relay A2                        // Amplifier 3 relay on ULN2003A IN3
  34. #define MainRelay 13                        // Main relay on Nano D13
  35. #include <DS3231.h>
  36. #include <Encoder.h>
  37. #include <Wire.h>
  38. #include <TDA7439.h>
  39. #include <LiquidCrystal_I2C.h>
  40. #include <EEPROM.h>
  41.  
  42. TDA7439 tda;
  43. LiquidCrystal_I2C lcd(0x22, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Addres for I2C LCD
  44. byte a1[8] = {0b00000, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b11011, 0b00000};
  45. byte a2[8] = {0b00000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b11000, 0b00000};
  46. int vol, vol_d, volTmp, z, bass, mids, treb, balans, in, gain, gain1, gain2, gain3, gain4;
  47. byte menu ;
  48. float encPos  = 0 ;
  49. int16_t oldPos = 0, tmp;
  50. int8_t dir = 0;
  51. unsigned long times;
  52. bool saveOk = true, waitOk = true, muteOk = true, SelectInput1 = true, SelectInput2 = true, SelectInput3 = true, SelectInput4 = true, SelectAmp1 = true, SelectAmp2 = true, SelectAmp3 = true, PowerOn = true;
  53. bool in[8];
  54. bool out[8];
  55. char inputName[4][7] = {"IN1", "IN2", "IN3", "IN4"};
  56.  
  57. Encoder myEnc(encA, encB);
  58.  
  59. void setup() {
  60.   pinMode(MenuButton, INPUT_PULLUP);         // menu
  61.   pinMode(MuteButton, INPUT_PULLUP);         // mute
  62.   pinMode(MuteLed, OUTPUT);
  63.   pinMode(MainRelay, OUTPUT);
  64.   Serial.begin(115200);                      // Only for debug
  65.   lcd.begin(16, 2);
  66.   lcd.backlight();
  67.   lcd.clear();
  68.   lcd.print(" TDA7439 Preamp");
  69.   delay(1000);
  70.   lcd.createChar(0, a1);
  71.   lcd.createChar(1, a2);
  72.   vol = EEPROM.read(0);
  73.   bass = EEPROM.read(1) - 7;
  74.   mids = EEPROM.read(2) - 7;
  75.   treb = EEPROM.read(3) - 7;
  76.   balans = EEPROM.read(4) - 4;
  77.   in = EEPROM.read(5);
  78.   gain1 = EEPROM.read(6);
  79.   gain2 = EEPROM.read(7);
  80.   gain3 = EEPROM.read(8);
  81.   gain4 = EEPROM.read(9);
  82.   if (vol > 48) {                             // if eeprom empty
  83.     vol = 0;
  84.     bass = 0;
  85.     mids = 0;
  86.     treb = 0;
  87.     balans = 4;
  88.     in = 1;
  89.     gain1 = 0;
  90.     gain2 = 0;
  91.     gain3 = 0;
  92.     gain4 = 0;
  93.   }
  94.   audio();
  95.   displayWrite(0);
  96. }
  97.  
  98. void loop() {
  99.   if (SelectInput1 == true) {
  100.     SelectInput1 = false;
  101.     delay(200);                              //Ez a gombismétlés miatt
  102.     InputLed1 = HIGH;                        // Input 1 LED
  103.     InputLed2 = LOW;
  104.     InputLed3 = LOW;
  105.     InputLed4 = LOW;
  106.   }
  107.   if (SelectInput2 == true) {
  108.     SelectInput2 = false;
  109.     delay(200);                              //Ez a gombismétlés miatt
  110.     InputLed2 = LOW;                        // Input 2 LED
  111.     InputLed2 = HIGH;
  112.     InputLed3 = LOW;
  113.     InputLed4 = LOW;
  114.   }
  115.   if (SelectInput3 == true) {
  116.     SelectInput3 = false;
  117.     delay(200);                             //Ez a gombismétlés miatt
  118.     InputLed3 = LOW;                       // Input 3 LED
  119.     InputLed2 = LOW;
  120.     InputLed3 = HIGH;
  121.     InputLed4 = LOW;
  122.   }
  123.   if (SelectInput4 == true) {
  124.     SelectInput4 = false;
  125.     delay(200);                             //Ez a gombismétlés miatt
  126.     InputLed4 = LOW;                       // Input 4 LED
  127.     InputLed2 = LOW;
  128.     InputLed3 = LOW;
  129.     InputLed4 = HIGH;
  130.   }
  131.   if (SelectAmp1 == true) {
  132.     SelectAmp1 = false;
  133.     delay(200);                            //Ez a gombismétlés miatt
  134.     Amp1Led = HIGH;                        // Amplifier 1 LED
  135.   }
  136.   if (SelectAmp2 == true) {
  137.     SelectAmp2 = false;
  138.     delay(200);                            //Ez a gombismétlés miatt
  139.     Amp2Led = HIGH;                        // Amplifier 2 LED
  140.   }
  141.   if (SelectAmp3 == true) {
  142.     SelectAmp3 = false;
  143.     delay(200);                            //Ez a gombismétlés miatt
  144.     Amp3Led = HIGH;                        // Amplifier 3 LED
  145.   }
  146.   if (digitalRead(MuteButton) == LOW and muteOk == true) {
  147.     muteOk = false;
  148.     tda.setVolume(0);
  149.     lcd.clear();
  150.     lcd.print("      MUTE");
  151.     delay(100);
  152.     while (digitalRead(MuteButton) == LOW) {}
  153.   }
  154.   if (digitalRead(MuteButton) == LOW) {
  155.     muteOk = true;
  156.     menu = 0;
  157.     volTmp = vol;
  158.     for (vol = 0; vol < volTmp; vol++) {
  159.       tda.setVolume(vol);
  160.       displayWrite(menu);
  161.       delay(100);
  162.     }
  163.     dir = 0;
  164.     displayWrite(menu);
  165.     while (digitalRead(MuteButton) == LOW) {}
  166.   }
  167.   if (times + 10000 < millis() and saveOk == false) { // if nothing happens for 10 sec, then save data
  168.     saveData();
  169.   }
  170.   float newPos = myEnc.read();
  171.   if (newPos != encPos) {
  172.     encPos = newPos;
  173.     tmp = encPos;
  174.     if (tmp / 4 - encPos / 4 == 0) { // the value depends on the encoder type, typically 4 is appropriate
  175.       if (oldPos < encPos) {
  176.         dir = 1;
  177.       } else {
  178.         dir = -1;
  179.       }
  180.       modValue(menu, dir);
  181.       displayWrite(menu);
  182.       audio();
  183.       oldPos = encPos;
  184.     }
  185.   }
  186.   if (digitalRead(MenuButton) == LOW) {
  187.     times = millis();
  188.     delay(100);
  189.     muteOk = true;
  190.     waitOk = true;
  191.     while (digitalRead(MenuButton) == LOW) {
  192.       if (times + 2000 < millis() and waitOk == true) { // After 2 seconds of hold, the main menu changes
  193.         waitOk = false;
  194.         lcd.clear();
  195.         lcd.print("   waiting ...");
  196.       }
  197.     }
  198.     if (times + 2000 < millis()) {
  199.       if (menu == 10) {
  200.         menu = 4;
  201.       } else {
  202.         menu = 10;
  203.         in = 0;
  204.       }
  205.     }
  206.     if (menu != 10) menu++;
  207.     if (menu == 5) menu = 0;
  208.     if (menu == 10) in++;
  209.     if (in == 5) in = 1;
  210.     audio();
  211.     displayWrite(menu);
  212.     delay(200);
  213.   }
  214. }
  215. void PCFReadWrite() {
  216.   for (int i=0; i<8; ++i){
  217.     in[i]=0;
  218.   }
  219.   if (PCF1.digitalRead(P0)==LOW) in[0]=HIGH;
  220.   if (PCF1.digitalRead(P1)==LOW) in[1]=HIGH;
  221.   if (PCF1.digitalRead(P2)==LOW) in[2]=HIGH;
  222.   if (PCF1.digitalRead(P3)==LOW) in[3]=HIGH;
  223.   if (out[0]==LOW) {
  224.      PCF1.digitalWrite(P4,LOW);
  225.   } else {
  226.      PCF1.digitalWrite(P4,HIGH);
  227.   }
  228.   if (out[1]==LOW) {
  229.      PCF1.digitalWrite(P5,LOW);
  230.   } else {
  231.      PCF1.digitalWrite(P5,HIGH);
  232.   }
  233.   if (out[2]==LOW) {
  234.      PCF1.digitalWrite(P6,LOW);
  235.   } else {
  236.      PCF1.digitalWrite(P6,HIGH);
  237.   }
  238.   if (out[3]==LOW) {
  239.      PCF1.digitalWrite(P7,LOW)
  240.   } else {
  241.      PCF1.digitalWrite(P7,HIGH)
  242.   }
  243.   if (PCF2.digitalRead(P0)==LOW) in[4]=HIGH;
  244.   if (PCF2.digitalRead(P1)==LOW) in[5]=HIGH;
  245.   if (PCF2.digitalRead(P2)==LOW) in[6]=HIGH;
  246.   if (PCF2.digitalRead(P3)==LOW) in[7]=HIGH;
  247.   if (out[0]==LOW) {
  248.      PCF2.digitalWrite(P4,LOW);
  249.   } else {
  250.      PCF2.digitalWrite(P4,HIGH);
  251.   }
  252.   if (out[1]==LOW) {
  253.      PCF2.digitalWrite(P5,LOW);
  254.   } else {
  255.      PCF2.digitalWrite(P5,HIGH);
  256.   }
  257.   if (out[2]==LOW) {
  258.      PCF2.digitalWrite(P6,LOW);
  259.   } else {
  260.      PCF2.digitalWrite(P6,HIGH);
  261.   }
  262.   if (out[3]==LOW) {
  263.      PCF1.digitalWrite(P7,LOW);
  264.   } else {
  265.      PCF1.digitalWrite(P7,HIGH);
  266.   }
  267. }
  268. void modValue(uint8_t sz, int8_t ir) {
  269.   times = millis();
  270.   muteOk = true;
  271.   saveOk = false;
  272.   switch (sz) {
  273.     case 0:
  274.       vol = vol + ir;
  275.       if (vol > 48) vol = 48;
  276.       if (vol < 0) vol = 0;
  277.       break;
  278.     case 1:
  279.       bass = bass + ir;
  280.       if (bass >  7) bass =  7;
  281.       if (bass < -7) bass = -7;
  282.       break;
  283.     case 2:
  284.       mids = mids + ir;
  285.       if (mids >  7) mids =  7;
  286.       if (mids < -7) mids = -7;
  287.       break;
  288.     case 3:
  289.       treb = treb + ir;
  290.       if (treb >  7) treb =  7;
  291.       if (treb < -7) treb = -7;
  292.       break;
  293.     case 4:
  294.       balans = balans + ir;
  295.       if (balans >  4) balans =  4;
  296.       if (balans < -4) balans = -4;
  297.       break;
  298.     case 10:
  299.       g1();
  300.       gain = gain + ir;
  301.       if (gain >  15) gain =  15;
  302.       if (gain < 0) gain = 0;
  303.       g2();
  304.       break;
  305.   }
  306.   audio();
  307. }
  308.  
  309. void saveData() {
  310.   saveOk = true;
  311.   //Serial.println("SAVED");
  312.   EEPROM.update(0, vol);
  313.   EEPROM.update(4, balans + 4);
  314.   EEPROM.update(1, bass + 7);
  315.   EEPROM.update(2, mids + 7);
  316.   EEPROM.update(3, treb + 7);
  317.   EEPROM.update(5, in);
  318.   EEPROM.update(6, gain1);
  319.   EEPROM.update(7, gain2);
  320.   EEPROM.update(8, gain3);
  321.   EEPROM.update(9, gain4);
  322.   menu = 0;
  323.   displayWrite(menu);
  324. }
  325.  
  326. void displayWrite (int sz) {
  327.   lcd.clear();
  328.   switch (sz) {
  329.     case 0:
  330.       lcd.setCursor(0, 0);
  331.       lcd.print("Volume ");
  332.       lcd.print(-48 + vol);
  333.       lcd.setCursor(13, 0);
  334.       lcd.print("dB");
  335.       vol_d = vol / 2;
  336.       if (vol_d >= 0) {
  337.         for (z = 0; z <= vol_d; z++) {
  338.           lcd.setCursor(z / 2, 1);
  339.           lcd.write((uint8_t)0);
  340.         }
  341.       }
  342.       if (vol_d % 2 == 0) {
  343.         lcd.setCursor(z / 2, 1);
  344.         lcd.write((uint8_t)1);
  345.       }
  346.       lcd.setCursor(13, 1);
  347.       lcd.print(vol);
  348.       break;
  349.     case 1:
  350.       lcd.setCursor(0, 0);
  351.       lcd.print("Bass ");
  352.       lcd.print(bass * 2);
  353.       lcd.setCursor(13, 0);
  354.       lcd.print("dB");
  355.       for (z = -7; z <= bass; z++) {
  356.         lcd.setCursor(z + 7, 1);
  357.         lcd.write((uint8_t)0);
  358.       }
  359.       break;
  360.     case 2:
  361.       lcd.setCursor(0, 0);
  362.       lcd.print("Mids ");
  363.       lcd.print(mids * 2);
  364.       lcd.setCursor(13, 0);
  365.       lcd.print("dB");
  366.       for (z = -7; z <= mids; z++) {
  367.         lcd.setCursor(z + 7, 1);
  368.         lcd.write((uint8_t)0);
  369.       }
  370.       break;
  371.     case 3:
  372.       lcd.setCursor(0, 0);
  373.       lcd.print("Treble ");
  374.       lcd.print(treb * 2);
  375.       lcd.setCursor(13, 0);
  376.       lcd.print("dB");
  377.       for (z = -7; z <= treb; z++) {
  378.         lcd.setCursor(z + 7, 1);
  379.         lcd.write((uint8_t)0);
  380.       }
  381.       break;
  382.     case 4:
  383.       lcd.setCursor(0, 0);
  384.       if (balans >= 0) {
  385.         lcd.print("-");
  386.       } else {
  387.         lcd.print("+");
  388.       }
  389.       lcd.print(abs(balans));
  390.       lcd.print(" dB ");
  391.       lcd.print(" <> ");
  392.       if (balans >= 0) {
  393.         lcd.print("+");
  394.       } else {
  395.         lcd.print("-");
  396.       }
  397.       lcd.print(abs(balans));
  398.       lcd.print(" dB ");
  399.       lcd.setCursor(0, 1);
  400.       lcd.print("R");
  401.       lcd.setCursor(15, 1);
  402.       lcd.print("L");
  403.       if (balans < 0) {
  404.         lcd.setCursor(balans + 7, 1);
  405.         lcd.write((uint8_t)0);
  406.       }
  407.       if (balans > 0) {
  408.         lcd.setCursor(balans + 8, 1);
  409.         lcd.write((uint8_t)0);
  410.       }
  411.       if (balans == 0) {
  412.         lcd.setCursor(7, 1);
  413.         lcd.write((uint8_t)0);
  414.         lcd.setCursor(8, 1);
  415.         lcd.write((uint8_t)0);
  416.       }
  417.       break;
  418.     case 10:
  419.       g1();
  420.       lcd.print("Input: ");
  421.       lcd.print(inputName[in - 1]);
  422.       lcd.setCursor(0, 1);
  423.       lcd.print("Input Gain: ");
  424.       lcd.print(gain);
  425.       break;
  426.   }
  427. }
  428.  
  429. void g1() {
  430.   if (in == 1) gain = gain1;
  431.   if (in == 2) gain = gain2;
  432.   if (in == 3) gain = gain3;
  433.   if (in == 4) gain = gain4;
  434. }
  435.  
  436. void g2() {
  437.   if (in == 1) gain1 = gain;
  438.   if (in == 2) gain2 = gain;
  439.   if (in == 3) gain3 = gain;
  440.   if (in == 4) gain4 = gain;
  441. }
  442.  
  443.  
  444. void audio() {
  445.   tda.setInput(in);
  446.   tda.inputGain(gain); // 0 to 15
  447.   tda.setVolume(vol); // 0 to 48 ( 0 is mute)
  448.   tda.setSnd(bass, 1); //-7 to +7 , 1 - Bass | 2 - Mids | 3 - Trebble
  449.   tda.setSnd(mids, 2);
  450.   tda.setSnd(treb, 3);
  451.   tda.spkAtt(balans);
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement