// 5.1 Surround System
// DaacWaves <https://https://daacwaves.blogspot.com>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

LiquidCrystal_I2C lcd(0x27,16,2);
#define PT2323_address 0b1001010
#define PT2322_address 0b1000100


#define sw01 8    // menu
#define sw02 11   // +
#define sw03 12   // -
#define sw04 9    // <
#define sw05 10   // >
#define sw06 7   // Power
#define sw07 13   // remot
#define sw08 3    // vela
#define sw09 4    // motorA
#define sw10 5    // motorB
#define sw11 2    // remot subwoofer


byte custom_num[8][8] = {
  { B00111,B01111,B11111,B11111,B11111,B11111,B11111,B11111 },
  { B11111,B11111,B11111,B00000,B00000,B00000,B00000,B00000 },
  { B11100,B11110,B11111,B11111,B11111,B11111,B11111,B11111 },
  { B11111,B11111,B11111,B11111,B11111,B11111,B01111,B00111 },
  { B00000,B00000,B00000,B00000,B00000,B11111,B11111,B11111 },
  { B11111,B11111,B11111,B11111,B11111,B11111,B11110,B11100 },
  { B11111,B11111,B11111,B00000,B00000,B00000,B11111,B11111 },
  { B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111 }};
    
const int digit_width = 3;
                                           //  0      1       2      3      4        5      6      7        8      9
const char custom_num_top[10][digit_width]={ 0,1,2, 1,2,32, 6,6,2, 6,6,2, 3,4,7,   7,6,6, 0,6,6, 1,1,2,   0,6,2, 0,6,2};
const char custom_num_bot[10][digit_width]={ 3,4,5, 4,7,4,  7,4,4, 4,4,5, 32,32,7, 4,4,5, 3,4,5, 32,32,7, 3,4,5, 4,4,5};

byte arrow_left[8] = {B00100,B01100,B11100,B01100,B00100,B00000,B00000,B00000};
byte arrow_right[8] = {B00100,B00110,B00111,B00110,B00100,B00000,B00000,B00000};
byte arrow_bottom[8] = {B00100,B00100,B00100,B11111,B01110,B00100,B00000,B00000};


unsigned long time;
int in, vol, bass, mid, treb, sub, fl, cn, fr, sl, sr, mute, ch_mute, eeprom, surr, mix, vol_10, vol_1, a, b, mute_sel, effect_sel, tone_sel, sound_mode, speaker_mode, sub_menu, menu_sel, power, vol_on;
int menu, sub_menu_sel, sub_menu_item, sub_menu_item_sel, esc, fogo;
int sub_v, sub_10, sub_1;

int lastClk = LOW;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  
  pinMode(sw01, INPUT); // menu
  pinMode(sw02, INPUT); // +
  pinMode(sw03, INPUT); // -
  pinMode(sw04, INPUT); // Input
  pinMode(sw05, INPUT); // Mute
  pinMode(sw06, INPUT); // Power
  pinMode(sw07, OUTPUT); // remot
  pinMode(sw08, OUTPUT); // vela
  pinMode(sw09, OUTPUT); // motorA
  pinMode(sw10, OUTPUT); // motorB
  pinMode(sw11, OUTPUT); // remot subwoofer

  lcd.begin(16, 2);
 
  power = 0;
  eeprom_read();
  power_up();
}



void loop() {
  lcd_update();
  eeprom_update();
  
  if(menu == 0 || menu == 2) {
    custom_num_shape();
  } else {
    custom_shape();
  }

  //power -------------------------------------------------//
  if (digitalRead(sw06) == HIGH) {
    power++;
    if (power > 1) {
      power = 0;
    }
    power_up();
    delay(300);
  }
  
  if(power == 1) {
    
    if (digitalRead(sw01) == HIGH && menu_sel == 0) {
      menu++;
      sub_menu = 0;
      if (menu > 5) {
        menu = 0;
        
      }
      btn_cl();
    }
    
    //mute -------------------------------------------------//

  }

  //volume -------------------------------------------------//
  if (menu == 0) {
    
    if (digitalRead(sw03) == HIGH && vol_on == 0) {
      vol++;
      set_vol();
  
    }
    if (digitalRead(sw02) == HIGH && vol_on == 0) {
      vol--;
      set_vol();
  
    }
    menu_sel = 0;
    sub_menu = 0;
    sub_menu_item = 0;
    sub_menu_item_sel = 0;
  }

  //menu 1 -------------------------------------------------//
  if (menu == 1) {
    set_sub_menu();
    switch(sub_menu){
      //bass -------------------------------------------------//
      case 1:
        if (digitalRead(sw02) == HIGH ) {
          bass++;
          set_bass();
          sound_mode = 0;
          btn_cl();
        }
        if (digitalRead(sw03) == HIGH ) {
          bass--;
          set_bass();
          sound_mode = 0;
          btn_cl();
        }
        break;

      //mid -------------------------------------------------//  
      case 2:
        if (digitalRead(sw02) == HIGH ) {
          mid++;
          set_mid();
          sound_mode = 0;
          btn_cl();
        }
        if (digitalRead(sw03) == HIGH ) {
          mid--;
          set_mid();
          sound_mode = 0;
          btn_cl();
        }
        break;

      //treb -------------------------------------------------//  
      case 3:
        if (digitalRead(sw02) == HIGH ) {
          treb++;
          set_treb();
          sound_mode = 0;
          btn_cl();
        }
        if (digitalRead(sw03) == HIGH ) {
          treb--;
          set_treb();
          sound_mode = 0;
          btn_cl();
        }
        break;
        
      case 4:
        sub_menu = 0;
        break;
    }
  }

  //menu 2 -------------------------------------------------//
  if (menu == 2) {
    if (digitalRead(sw03) == HIGH) {
      sub++; 
      set_sub();
      btn_cl();
    }
    if (digitalRead(sw02) == HIGH) {
      sub--;
      set_sub();
      btn_cl();
    }
    if (sub < (15) ){
    digitalWrite(sw11, HIGH);
    }
    if (sub == (15) ){
    digitalWrite(sw11, LOW);
    
   } 
  }
  
  //menu 3 -------------------------------------------------//
  if (menu == 3) {  
    set_sub_menu();  
    switch(sub_menu){
      //FL -------------------------------------------------//
      case 1:
        if (digitalRead(sw03) == HIGH ) {
          fl++; 
          set_fl();
          btn_cl();
        }
        if (digitalRead(sw02) == HIGH ) {
          fl--;
          set_fl();
          btn_cl();
        }
        break;

      //FR -------------------------------------------------//  
      case 2:
        if (digitalRead(sw03) == HIGH ) {
          fr++; 
          set_fr();
          btn_cl();
        }
        if (digitalRead(sw02) == HIGH ) {
          fr--;
          set_fr();
          btn_cl();
        }
        break;

      //CN -------------------------------------------------//
      case 3:
        if (digitalRead(sw03) == HIGH ) {
          cn++;
          set_cn();
          btn_cl();
        }
        if (digitalRead(sw02) == HIGH ) {
          cn--;
          set_cn();
          btn_cl();
        }
        break;

      //SL -------------------------------------------------//  
      case 4:
        if (digitalRead(sw03) == HIGH ) {
          sl++;
          set_sl();
          btn_cl();
        }
        if (digitalRead(sw02) == HIGH ) {
          sl--;
          set_sl();
          btn_cl();
        }
        break;

      //SR -------------------------------------------------//  
      case 5:
        if (digitalRead(sw03) == HIGH ) {
          sr++;
          set_sr();
          btn_cl();
        }
        if (digitalRead(sw02) == HIGH ) {
          sr--;
          set_sr();
          btn_cl();
        }
        break;
        
      case 6:
        sub_menu = 0;
        break;
    }
  }
  
  //menu 4 -------------------------------------------------//
  if (menu == 4) {
    set_sub_menu();
    switch(sub_menu){
      //surrond on off -------------------------------------------------//
      case 1:
        if (digitalRead(sw03) == HIGH ) {
          surr++;
          set_surr();
          btn_cl();
        }
        break;
        
      //speaker mode -------------------------------------------------//  
      case 2:
        if (digitalRead(sw03) == HIGH ) {
          speaker_mode++;
          set_speaker_mode();
          btn_cl();
        }
        break;
        
      //sound mode -------------------------------------------------//  
      case 3:
        if (digitalRead(sw03) == HIGH ) {
          sound_mode++;
          set_sound_mode();
          btn_cl();
        }
        break;
        
      case 4:
        sub_menu = 0;
        break;
    }
  }



//menu 5 -----------------------------------------------------------------------------------TESTE-----------------------------------------------------------------------------------------//
  if (menu == 5) {
    set_sub_menu();
    switch(sub_menu){
      //menu carro -------------------------------------------------//
      case 1:
        if (digitalRead(sw02) == HIGH ) {
          digitalWrite(sw09, HIGH);
          delay(1000);
          digitalWrite(sw09, LOW);
          esc = 1;  
        }
        if (digitalRead(sw03) == HIGH ) {
          digitalWrite(sw10, HIGH);
          delay(1000);
          digitalWrite(sw10, LOW);
          esc = 0;  
        }
        
        break;
        
      //speaker mode -------------------------------------------------//  
      case 2:
        if (digitalRead(sw02) == HIGH ) {
          digitalWrite(sw08, HIGH);
          fogo = 1;  
        }
        if (digitalRead(sw03) == HIGH ) {
          digitalWrite(sw08, LOW);
          fogo = 0;  
        }
        break;

        case 3:
        sub_menu = 0;
        break;
    }
  }



  }

//-----------------------------------------------------------------------------------------TESTE------------------------------------------------------------------------------------------//


//eeprom --------------------------------------------------------------------------------//

void eeprom_update(){
  if (millis() - time > 10000 && eeprom == 1 && mute == 0) {
    EEPROM.update(0, in);
    EEPROM.update(1, vol);
    EEPROM.update(2, bass);
    EEPROM.update(3, mid);
    EEPROM.update(4, treb);
    EEPROM.update(5, sub);
    EEPROM.update(6, fl);
    EEPROM.update(7, fr);
    EEPROM.update(8, cn);
    EEPROM.update(9, sl);
    EEPROM.update(10, sr);
    EEPROM.update(11, surr);
    EEPROM.update(12, mix);
    EEPROM.update(13, speaker_mode);
    EEPROM.update(14, sound_mode);
    EEPROM.update(15, esc);
    EEPROM.update(16, fogo);
    menu = 0;
    eeprom = 0;
    lcd.clear();
  }
}

void eeprom_read(){
  in = EEPROM.read(0);
  vol = EEPROM.read(1); 
  bass = EEPROM.read(2);
  mid = EEPROM.read(3);
  treb = EEPROM.read(4);
  sub = EEPROM.read(5);
  fl = EEPROM.read(6);
  fr = EEPROM.read(7);
  cn = EEPROM.read(8);
  sl = EEPROM.read(9);
  sr = EEPROM.read(10);
  surr = EEPROM.read(11);
  mix = EEPROM.read(12);
  speaker_mode = EEPROM.read(13);
  sound_mode = EEPROM.read(14);
  esc = EEPROM.read(15);
  fogo = EEPROM.read(16);
}

void btn_cl() {
  delay(200);
  lcd.clear();
  time = millis();
  eeprom = 1;
}

//power up --------------------------------------------------------------------------------//

void power_up() {
  if(power == 1) {
    lcd.setBacklight(HIGH);
    
    mute = 1;
    lcd.clear();
    delay(500);
    lcd.setCursor(0, 0);
    lcd.print("     SUBARU     ");
    delay(500);
    lcd.setCursor(0, 1);
    lcd.print("   5.1 System   ");
    delay(1000);
    lcd.clear();
    mute = 0;
    pt2322_input_sw();
    pt2322_function(0, 1, 0);
    set_speaker_mode();
    set_sound_mode();
    set_vol();
    set_sub();
    set_in();
    set_surr();
    set_mix();
    set_mute();
    set_fl();
    set_fr();
    set_cn();
    set_sl();
    set_sr();
    menu = 0;
    delay(500);
    digitalWrite(sw07, HIGH);
  } else {

    digitalWrite(sw07, LOW);
    digitalWrite(sw08, LOW);
    digitalWrite(sw09, LOW);
    digitalWrite(sw10, LOW);
    digitalWrite(sw11, LOW);
    delay(250);
    mute = 1;
    set_mute();
    delay(250);
    menu = 100;
    lcd.setBacklight(LOW);

  }
}

//lcd --------------------------------------------------------------------------------//

void lcd_update(){
  lcd.setCursor(0, 0);
  switch(menu){
    case 0:
      //input -------------------------------------------------//
      lcd.setCursor(0, 0);
      if(in == 0) {
        lcd.print("USB");
      }
      if(in == 1) {
        lcd.print("BLU");
      }
      if(in == 2) {
        lcd.print("FM ");
      }
      if(in == 3) {
        lcd.print("AUX");
      }

      //vol -------------------------------------------------//
      lcd.setCursor(0, 1);
      a = vol_10;
      b = vol_1;   
      lcd.setCursor(9, 0);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_top[7 - a][i]);
      
      lcd.setCursor(9, 1);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_bot[7 - a][i]);
      
      lcd.setCursor(13, 0);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_top[9 - b][i]);
      
      lcd.setCursor(13, 1);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_bot[9 - b][i]);

      //speaker mode -------------------------------------------------//
      lcd.setCursor(0, 1);
      if(sound_mode == 0) {
        lcd.print("NOR");
      }
      if(sound_mode == 1) {
        lcd.print("ROC");
      }
      if(sound_mode == 2) {
        lcd.print("POP");
      }
      if(sound_mode == 3) {
        lcd.print("CLA");
      }

      //sound mode -------------------------------------------------//
      //lcd.setCursor(13, 1);
      //if(speaker_mode == 0) {
      //  lcd.print("5.1");
      //}
      //if(speaker_mode == 1) {
      //  lcd.print("2.1");
      //}
      break;
      
    case 1:
      lcd.setCursor(0, 0);
      if(sub_menu == 0) {
        sub_menu = 1;
      }
      
      //bass -------------------------------------------------//
        if(sub_menu == 1) {
        lcd.setCursor(0, 0);
        lcd.write(2);  
      }
      lcd.setCursor(1, 0);
      lcd.print("Bass"); 
      lcd.setCursor(1, 1);
      lcd.print(bass);

      //mid -------------------------------------------------//

      if(sub_menu == 2) {
      lcd.setCursor(6, 0);
      lcd.write(2);  
 
      }
      lcd.setCursor(7, 0);
      lcd.print("Mid"); 
      lcd.setCursor(6, 1);
      lcd.print(mid);
      
      //treb -------------------------------------------------//
      
      if(sub_menu == 3) {
      lcd.setCursor(11, 0);
      lcd.write(2);  
  
      }
      lcd.setCursor(12, 0);
      lcd.print("Treb");
      lcd.setCursor(11, 1);
      lcd.print(treb);
      break;

    case 2:
      //SUB -------------------------------------------------//
      
      sub_v = 15 - sub;
      sub_10 = sub_v / 10;
      sub_1 = sub_v - sub_10 * 10;
      
      lcd.setCursor(0, 0);
      lcd.print("SUB");

      lcd.setCursor(9, 0);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_top[sub_10][i]);
      
      lcd.setCursor(9, 1);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_bot[sub_10][i]);
      
      lcd.setCursor(13, 0);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_top[sub_1][i]);
      
      lcd.setCursor(13, 1);
      for(int i=0; i < digit_width; i++)
      lcd.print(custom_num_bot[sub_1][i]);

      break;  
    case 3:
      if(sub_menu == 0) {
       sub_menu = 1;
      }
      //FL -------------------------------------------------//
     
      if(sub_menu == 1 ) {
      lcd.setCursor(0, 0);
      lcd.write(2);
      }
      lcd.setCursor(1, 0);
      lcd.print("FL");  
      lcd.setCursor(1, 1);
      lcd.print(15 - fl);

      //FR -------------------------------------------------//
      if(sub_menu == 2 ) {
      lcd.setCursor(3, 0);
      lcd.write(2);
      }
      lcd.setCursor(4, 0);
      lcd.print("FR");  
      lcd.setCursor(4, 1);
      lcd.print(15 - fr);

      //CN -------------------------------------------------//
      if(sub_menu == 3 ) {
      lcd.setCursor(6, 0); 
      lcd.write(2);
      }
      lcd.setCursor(7, 0);
      lcd.print("CN");  
      lcd.setCursor(7, 1);
      lcd.print(15 - cn);

      //SL -------------------------------------------------//
      if(sub_menu == 4 ) {
      lcd.setCursor(9, 0);
      lcd.write(2);
      }
      lcd.setCursor(10, 0);
      lcd.print("SL");  
      lcd.setCursor(10, 1);
      lcd.print(15 - sl);

      //SR -------------------------------------------------//
      if(sub_menu == 5 ) {
      lcd.setCursor(12, 0);
      lcd.write(2);
      }
      lcd.setCursor(13, 0);
      lcd.print("SR");  
      lcd.setCursor(13, 1);
      lcd.print(15 - sr);
      break;
      
    case 4:
      if(sub_menu == 0) {
        sub_menu = 1;
      }
      //surrond on off -------------------------------------------------//
      if(sub_menu == 1 ) {

        lcd.setCursor(0, 0);
        lcd.print("    SURROUND    ");
      
      if(surr == 0) {
        lcd.setCursor(0, 1);
        lcd.print("     LIGADO     ");
      }
      if(surr == 1) {
        lcd.setCursor(0, 1);
        lcd.print("    DESLIGADO   ");
       }
      }

      //speaker mode -------------------------------------------------//
      lcd.setCursor(6, 0);
      if(sub_menu == 2 ) {

        lcd.setCursor(1, 0); 
        lcd.print("");  
        lcd.setCursor(6, 1);
      if(speaker_mode == 0) {
        lcd.print("5.1");
       }
      if(speaker_mode == 1) {
        lcd.print("2.1");
       }
      }
      //sound mode -------------------------------------------------//
      lcd.setCursor(11, 0);
      if(sub_menu == 3 ) {

        lcd.setCursor(0, 0);
        lcd.print("  EQUALIZADOR   ");  
      
      lcd.setCursor(0, 1);
      if(sound_mode == 0) {
        lcd.print("     NORMAL     ");
      }
      if(sound_mode == 1) {
        lcd.print("      ROCK      ");
      }
      if(sound_mode == 2) {
        lcd.print("      POP       ");
      }
      if(sound_mode == 3) {
        lcd.print("    CLASICO     ");
       }
      }
      break;
      //-----------------------------------------------------------------------------------TESTE-----------------------------------------------------------------------------------------//


      case 5:
      if(sub_menu == 0) {
        sub_menu = 1;
      break;
      }
      
      //escape -------------------------------------------------//
    
      if(sub_menu == 1 ) {
     

      }

      //fogo -------------------------------------------------//
      
      if(sub_menu == 2 ) {
      

      }

      break;



      //-----------------------------------------------------------------------------------TESTE-----------------------------------------------------------------------------------------//


    case 100:
      lcd.setCursor(0, 0);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      lcd.print("   SUBARU  H6   ");
      break;
  
  





}
  
}

//custom shape --------------------------------------------------------------------------------//

void custom_num_shape(){
  for(int i=0; i < 8; i++)
  lcd.createChar(i, custom_num[i]);
}

void custom_shape(){
  lcd.createChar(1, arrow_left);
  lcd.createChar(3, arrow_right);
  lcd.createChar(2, arrow_bottom);
}


//sub menu --------------------------------------------------------------------------------//

void set_sub_menu(){
  if (digitalRead(sw05) == HIGH && sub_menu_sel == 0) {
    sub_menu++;
    btn_cl();
  }
  if (digitalRead(sw04) == HIGH && sub_menu_sel == 0) {
    sub_menu--;
    if (sub_menu < 0) {
      sub_menu = 0;
    }
    btn_cl();
  }

}


//speaker mode --------------------------------------------------------------------------------//

void set_speaker_mode() {
  if (speaker_mode > 1) {
    speaker_mode = 0;
  }
  switch(speaker_mode) {
    case 0:                     // 5.1 mode     
      ch_mute = 0;
      surr = 0;
      break;
    case 1:                     // 2.1 mode
      ch_mute = 1;
      surr = 1;
      break;
  }
  set_cn();
  set_sl();
  set_sr();
  set_surr();
}

//sound mode --------------------------------------------------------------------------------//

void set_sound_mode() {
  if (sound_mode > 3) {
    sound_mode = 0;
  }
  switch(sound_mode) {
    case 0:                     // Normal     
      bass = 7;
      mid = 7;
      treb = 7;
      break;
    case 1:                     // Rock
      bass = 11;
      mid = 5;
      treb = 12;
      break;
    case 2:                     // Pop
      bass = 4;
      mid = 10;
      treb = 5;
      break;
    case 3:                     // Classic
      bass = 7;
      mid = 7;
      treb = 5;
      break;
  }
  set_bass();
  set_mid();
  set_treb();
}

//pt2323 settings --------------------------------------------------------------------------------//

void set_in(){
  if (in > 3) {
    in = 0;
  }  
  if (in < 0) {
    in = 3;
  }  
  switch(in){
    case 0: a = 0b11001011; break; // 1 input
    case 1: a = 0b11001010; break; // 2 input
    case 2: a = 0b11001001; break; // 3 input
    case 3: a = 0b11001000; break; // 4 input
  }
  pt2323_send(a);
}
void set_surr(){
  if (surr > 1) {
    surr = 0;
  } 
  switch(surr){
    case 0: a = 0b11010000; break; // Surround ON
    case 1: a = 0b11010001; break; // Surround OFF
  }
  pt2323_send(a);
}
void set_mix(){
  if (mix > 1) {
    mix = 0;
  }
  switch(mix){
    case 0: a = 0b10010000; break; // 0dB setup
    case 1: a = 0b10010001; break; // +6dB setup
  }
  pt2323_send(a);
}
void set_mute(){
  if (mute > 1) {
    mute = 0;
  }
  switch(mute){
    case 0: a = 0b11111110; break; // All CH mute disabled
    case 1: a = 0b11111111; break; // All CH mute
  }
  pt2323_send(a);
}

//pt2322 settings --------------------------------------------------------------------------------//

void pt2322_function(int pt2322_mute, int effect, int tone_ctrl) {
  switch(pt2322_mute){
    case 0: mute_sel = 0b00000000; break;
    case 1: mute_sel = 0b00001000; break;
  }
  switch(effect){
    case 0: effect_sel = 0b00000000; break;
    case 1: effect_sel = 0b00000100; break;
  }
  switch(tone_ctrl){
    case 0: tone_sel = 0b00000000; break;
    case 1: tone_sel = 0b00000010; break;
  }
  pt2322_send(0b01110000 + mute_sel + effect_sel + tone_sel);
}

void set_vol(){
  if (vol > 79) {
    vol = 79;
  }
  if (vol < 0) {
    vol = 0;
  }
  vol_10 = vol / 10;
  vol_1 = vol - vol_10 * 10;
  a = vol_10 + 0b11100000;
  b = vol_1 + 0b11010000;
  Wire.beginTransmission(PT2322_address);
  Wire.write (a);
  Wire.write (b);
  Wire.endTransmission();
}

void set_bass(){
  if (bass > 15) {
    bass = 15;
  }
  if (bass < 0) {
    bass = 0;
  }
  a = bass; 
  if(bass > 7){
    a = 23 - bass;
  }
  pt2322_send(0b10010000 + a);
}

void set_mid(){
  if (mid > 15) {
    mid = 15;
  }
  if (mid < 0) {
    mid = 0;
  }
  a = mid; 
  if(mid > 7){
    a = 23 - mid;
  }
  pt2322_send(0b10100000 + a);
}

void set_treb(){
  if (treb > 15) {
    treb = 15;
  }
  if (treb < 0) {
    treb = 0;
  }
  a = treb; 
  if(treb > 7){
    a = 23 - treb;
  }
  pt2322_send(0b10110000 + a);
}

void set_sub(){
  if (sub > 15) {
    sub = 15;
  }
  if (sub < 0) {
    sub = 0;
  }
  pt2322_send(0b01100000 + sub);
}

void set_fl(){
  if (fl > 15) {
    fl = 15;
  }
  if (fl < 0) {
    fl = 0;
  }
  pt2322_send(0b00010000 + fl);
}

void set_fr(){
  if (fr > 15) {
    fr = 15;
  }
  if (fr < 0) {
    fr = 0;
  }
  pt2322_send(0b00100000 + fr);
}

void set_cn(){
  if (cn > 15) {
    cn = 15;
  }
  if (cn < 0) {
    cn = 0;
  }
  switch(ch_mute){
    case 0: a = 0b11110100; break; // CN mute disabled
    case 1: a = 0b11110101; break; // CN mute
  }
  pt2323_send(a);
  pt2322_send(0b00110000 + cn);
}

void set_sl(){
  if (sl > 15) {
    sl = 15;
  }
  if (sl < 0) {
    sl = 0;
  }
  switch(ch_mute){
    case 0: a = 0b11111000; break; // SL mute disabled
    case 1: a = 0b11111001; break; // SL mute
  }
  pt2323_send(a);
  pt2322_send(0b01000000 + sl);
}

void set_sr(){
  if (sr > 15) {
    sr = 15;
  }
  if (sr < 0) {
    sr = 0;
  }
  switch(ch_mute){
    case 0: a = 0b11111010; break; // SR mute disabled
    case 1: a = 0b11111011; break; // SR mute
  }
  pt2323_send(a);
  pt2322_send(0b01010000 + sr);
}



//pt2323 & pt2322 send --------------------------------------------------------------------------------//


void pt2323_send(char c){
  Wire.beginTransmission(PT2323_address);
  Wire.write (c);
  Wire.endTransmission();
}
void pt2322_input_sw() {
  Wire.beginTransmission(PT2322_address);
  Wire.write (0b11000111);
  Wire.endTransmission();
}
void pt2322_send(char c){
  Wire.beginTransmission(PT2322_address);
  Wire.write (c);
  Wire.endTransmission();
}

//end code