Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v1.0
- // v1.1 added input channel name
- // v1.2 added control buttons, leds and rtc
- #define ENCODER_DO_NOT_USE_INTERRUPTS
- #define encA 2 // encoder A
- #define encB 3 // encoder B
- #define ok_button 4 // encoder gomb
- #define MenuButton 5 // menu gomb
- #define MuteButton 6 // mute gomb
- #define MuteLed 7 // mute led
- #define PCF1 0x26
- #define SelectInput1 in[0]
- #define SelectInput2 in[1]
- #define SelectInput3 in[2]
- #define SelectInput4 in[3]
- #define InputLed1 out[0]
- #define InputLed2 out[1]
- #define InputLed3 out[2]
- #define InputLed4 out[3]
- #define PCF2 0x27
- #define SelectAmp1 in[4]
- #define SelectAmp2 in[5]
- #define SelectAmp3 in[6]
- #define PowerOn in[7]
- #define Amp1Led out[4]
- #define Amp2Led out[5]
- #define Amp3Led out[6]
- #define PowerLed 8 // If power ON then LED is GREEN
- #define PowerLed 9 // If power OFF then LED is RED
- #define Amp1Relay A0 // Amplifier 1 relay on ULN2003A IN1
- #define Amp2Relay A1 // Amplifier 2 relay on ULN2003A IN2
- #define Amp3relay A2 // Amplifier 3 relay on ULN2003A IN3
- #define MainRelay 13 // Main relay on Nano D13
- #include <DS3231.h>
- #include <Encoder.h>
- #include <Wire.h>
- #include <TDA7439.h>
- #include <LiquidCrystal_I2C.h>
- #include <EEPROM.h>
- TDA7439 tda;
- LiquidCrystal_I2C lcd(0x22, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addres for I2C LCD
- 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, SelectInput1 = true, SelectInput2 = true, SelectInput3 = true, SelectInput4 = true, SelectAmp1 = true, SelectAmp2 = true, SelectAmp3 = true, PowerOn = true;
- bool in[8];
- bool out[8];
- char inputName[4][7] = {"IN1", "IN2", "IN3", "IN4"};
- Encoder myEnc(encA, encB);
- void setup() {
- pinMode(MenuButton, INPUT_PULLUP); // menu
- pinMode(MuteButton, INPUT_PULLUP); // mute
- pinMode(MuteLed, OUTPUT);
- pinMode(MainRelay, OUTPUT);
- Serial.begin(115200); // Only for debug
- lcd.begin(16, 2);
- lcd.backlight();
- lcd.clear();
- lcd.print(" TDA7439 Preamp");
- delay(1000);
- 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 (SelectInput1 == true) {
- SelectInput1 = false;
- delay(200); //Ez a gombismétlés miatt
- InputLed1 = HIGH; // Input 1 LED
- InputLed2 = LOW;
- InputLed3 = LOW;
- InputLed4 = LOW;
- }
- if (SelectInput2 == true) {
- SelectInput2 = false;
- delay(200); //Ez a gombismétlés miatt
- InputLed2 = LOW; // Input 2 LED
- InputLed2 = HIGH;
- InputLed3 = LOW;
- InputLed4 = LOW;
- }
- if (SelectInput3 == true) {
- SelectInput3 = false;
- delay(200); //Ez a gombismétlés miatt
- InputLed3 = LOW; // Input 3 LED
- InputLed2 = LOW;
- InputLed3 = HIGH;
- InputLed4 = LOW;
- }
- if (SelectInput4 == true) {
- SelectInput4 = false;
- delay(200); //Ez a gombismétlés miatt
- InputLed4 = LOW; // Input 4 LED
- InputLed2 = LOW;
- InputLed3 = LOW;
- InputLed4 = HIGH;
- }
- if (SelectAmp1 == true) {
- SelectAmp1 = false;
- delay(200); //Ez a gombismétlés miatt
- Amp1Led = HIGH; // Amplifier 1 LED
- }
- if (SelectAmp2 == true) {
- SelectAmp2 = false;
- delay(200); //Ez a gombismétlés miatt
- Amp2Led = HIGH; // Amplifier 2 LED
- }
- if (SelectAmp3 == true) {
- SelectAmp3 = false;
- delay(200); //Ez a gombismétlés miatt
- Amp3Led = HIGH; // Amplifier 3 LED
- }
- if (digitalRead(MuteButton) == LOW and muteOk == true) {
- muteOk = false;
- tda.setVolume(0);
- lcd.clear();
- lcd.print(" MUTE");
- delay(100);
- while (digitalRead(MuteButton) == LOW) {}
- }
- if (digitalRead(MuteButton) == 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(MuteButton) == 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(MenuButton) == LOW) {
- times = millis();
- delay(100);
- muteOk = true;
- waitOk = true;
- while (digitalRead(MenuButton) == 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 PCFReadWrite() {
- for (int i=0; i<8; ++i){
- in[i]=0;
- }
- if (PCF1.digitalRead(P0)==LOW) in[0]=HIGH;
- if (PCF1.digitalRead(P1)==LOW) in[1]=HIGH;
- if (PCF1.digitalRead(P2)==LOW) in[2]=HIGH;
- if (PCF1.digitalRead(P3)==LOW) in[3]=HIGH;
- if (out[0]==LOW) {
- PCF1.digitalWrite(P4,LOW);
- } else {
- PCF1.digitalWrite(P4,HIGH);
- }
- if (out[1]==LOW) {
- PCF1.digitalWrite(P5,LOW);
- } else {
- PCF1.digitalWrite(P5,HIGH);
- }
- if (out[2]==LOW) {
- PCF1.digitalWrite(P6,LOW);
- } else {
- PCF1.digitalWrite(P6,HIGH);
- }
- if (out[3]==LOW) {
- PCF1.digitalWrite(P7,LOW)
- } else {
- PCF1.digitalWrite(P7,HIGH)
- }
- if (PCF2.digitalRead(P0)==LOW) in[4]=HIGH;
- if (PCF2.digitalRead(P1)==LOW) in[5]=HIGH;
- if (PCF2.digitalRead(P2)==LOW) in[6]=HIGH;
- if (PCF2.digitalRead(P3)==LOW) in[7]=HIGH;
- if (out[0]==LOW) {
- PCF2.digitalWrite(P4,LOW);
- } else {
- PCF2.digitalWrite(P4,HIGH);
- }
- if (out[1]==LOW) {
- PCF2.digitalWrite(P5,LOW);
- } else {
- PCF2.digitalWrite(P5,HIGH);
- }
- if (out[2]==LOW) {
- PCF2.digitalWrite(P6,LOW);
- } else {
- PCF2.digitalWrite(P6,HIGH);
- }
- if (out[3]==LOW) {
- PCF1.digitalWrite(P7,LOW);
- } else {
- PCF1.digitalWrite(P7,HIGH);
- }
- }
- 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("SAVED");
- 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