/*************************************************************************************
DIPLOMA THESIS

Name: Software defined transmitter with SI4713 for FM band 76-108 MHz - Arduino Shield
Author: Bc. Marek Kováč
Supervisor: Ing. Ondřej Pavelka
Last modified: 28.5.2014
**************************************************************************************
Datasheet and documentation:
http://www.silabs.com/Support%20Documents/TechnicalDocs/Si4712-13-B30.pdf
http://www.silabs.com/Support%20Documents/TechnicalDocs/AN332.pdf
http://www.silabs.com/Support%20Documents/TechnicalDocs/AN383.pdf
**************************************************************************************/

//Libraries
#include <PCD8544.h>
#include <Wire.h>
#include <EEPROM.h>

//Variables
static PCD8544 lcd; 

const int resetPin = 2;
int buttonStateMENU = 0;
int buttonStateLEFT = 0;
int buttonStateRIGHT = 0;
const int buttonMENU = A0;
const int buttonLEFT = A1;
const int buttonRIGHT = A2;

int address_ = 7;
int c = 0;
int def = 0;
float display_value = 0.0;
int frequency_transmit = 0;
int i = 0;
byte j = 0x00;
int komp = 0;
int lim = 1;
int m = 0;
int push_number = 0;
int last_state_left = 1;
int last_state_right = 1;
int transmit = 1;


byte STATUS = 0x00;

int deviations = 0x00;
const int deviations_max = 0x2328;  //9000 
const int deviations_min = 0x00;
int frequency = 0x1DB0;  //7600
const int frequency_max = 0x2A30;  //10800
const int frequency_min = 0x1DB0;  //7600
int pilot_freq = 0x00;
const int pilot_freq_max = 0x4A38;  //19000
const int pilot_freq_min = 0x00;
int power = 0x58;  //88
const int power_max = 0x78;  //120
const int power_min = 0x58;  //88
int tx_line_in_lev = 0x96;  //150
const int tx_line_in_lev_max = 0x27C;  //636
const int tx_line_in_lev_min = 0x96;  //150

int l = 0;

int byte_number = 0;
byte tune_status[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte value = 0x00;

int a = 0;  
int z = 0;

/******************************************************************
Complete list of commands and arguments in programming guide SI4713
http://www.silabs.com/Support%20Documents/TechnicalDocs/AN332.pdf
******************************************************************/
//Commands
byte POWER_UP = 0x01;
byte POWER_DOWN = 0x11;
byte SET_PROPERTY = 0x12;
byte GET_PROPERTY = 0x13;
byte TX_TUNE_FREQ = 0x30;
byte TX_TUNE_FREQ_HIGHBYTE = 0x00;
byte TX_TUNE_FREQ_LOWBYTE = 0x00;
byte TX_TUNE_POWER = 0x31;
byte TX_TUNE_STATUS = 0x33;
//Values of properties
byte TX_PILOT_DEVIATION_LOWBYTE = 0x00;
byte TX_PILOT_DEVIATION_HIGHBYTE = 0x00;
byte TX_PILOT_FREQUENCY_LOWBYTE = 0x00;
byte TX_PILOT_FREQUENCY_HIGHBYTE = 0x00;
byte TX_AUDIO_DEVIATION_LOWBYTE = 0x00;
byte TX_AUDIO_DEVIATION_HIGHBYTE = 0x00;

byte TX_COMPONENT_ENABLE_HIGHBYTE_VALUE = 0x00;
int TX_COMPONENT_ENABLE_LOWBYTE_VALUE[] = {0x01, 0x02, 0x03};

int TX_LINE_INPUT_MUTE_HIGHBYTE_VALUE = 0x00;
int TX_LINE_INPUT_MUTE_LOWBYTE_VALUE[] = {0x00, 0x01, 0x02, 0x03};

byte TX_LINE_INPUT_LEVEL_HIGHBYTE = 0x00;
byte TX_LINE_INPUT_LEVEL_LOWBYTE = 0x00;

byte TX_PREEMPHASIS_HIGHBYTE_VALUE = 0x00;
byte TX_PREEMPHASIS_LOWBYTE_VALUE[] = {0x00, 0x01};

byte TX_ACOMP_ENABLE_HIGHBYTE_VALUE = 0x00;
byte TX_ACOMP_ENABLE_LOWBYTE_VALUE[] = {0x00, 0x01, 0x02, 0x03};

byte TX_ACOMP_ATTACK_TIME_HIGHBYTE_VALUE = 0x00;
byte TX_ACOMP_ATTACK_TIME_LOWBYTE_VALUE[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};

byte TX_ACOMP_RELEASE_TIME_HIGHBYTE_VALUE = 0x00;
byte TX_ACOMP_RELEASE_TIME_LOWBYTE_VALUE[] = {0x00, 0x01, 0x02, 0x03, 0x04};

byte TX_ACOMP_GAIN_HIGHBYTE_VALUE = 0x00;
byte TX_ACOMP_GAIN_LOWBYTE_VALUE[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14};

byte TX_LIMITER_RELEASE_TIME_HIGHBYTE_VALUE[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07};
byte TX_LIMITER_RELEASE_TIME_LOWBYTE_VALUE[] = {0x05, 0x06, 0x07, 0x08, 0x0A, 0x0C, 0x11, 0x19, 0x33, 0x39, 0x40, 0x49, 0x55, 0x66, 0x7F, 0xAA, 0xFF, 0xFE, 0xE8, 0xD0};


byte TX_ACOMP_THRESHOLD_HIGHBYTE_VALUE[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                                            0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00};
byte TX_ACOMP_THRESHOLD_LOWBYTE_VALUE[] = {0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB,
                                           0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x00};

void setup() 
{
   lcd.begin(84, 48);   
  
   pinMode(buttonMENU, INPUT);
   pinMode(buttonLEFT, INPUT);
   pinMode(buttonRIGHT, INPUT);
   pinMode(resetPin, OUTPUT);
  
   Wire.begin();
   //Initial screen
   lcd.setCursor(0, 0);
   lcd.print("Project:");
   lcd.setCursor(0, 1);
   lcd.print("FM transmitter");
   lcd.setCursor(0, 4);
   lcd.print("Author:");
   lcd.setCursor(0, 5);
   lcd.print("Marek Kovac");
   delay(5000);
   lcd.clear();
   lcd.setCursor(0, 2);
   lcd.print("Welcome...");
   
   delay(2000);
}
void loop() 
{
 /****************************************************LAST SETTINGS**************************************************
Description: Configuration of transmitter depends on values saved in EEPROM. 
             When the last settins is used, data are read from EEPROM and send to SI4713 and transmitter 
             go to transmitting state. When user do not use last settings, can use default settings or set properties.
 ********************************************************************************************************************/
START:             
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("LAST SETTINGS:");
                   lcd.setCursor(0, 5);
                   lcd.print("-> NEXT");
                   lcd.setCursor(0, 4);
                   lcd.print("<- BACK");
                   lcd.setCursor(0, 3);
                   lcd.print("o   SET");
                  
                   delay(100);
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      
                      if (buttonStateRIGHT == HIGH)
                      {
                         goto DEFAULT_SETTINGS;
                      }
                      
                      if (buttonStateLEFT == HIGH)
                      {
                         goto PROP;
                      }
                   }
                   while ( buttonStateMENU == LOW);
                   
                   c = 1;
                 
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("LAST SETTINGS:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-/-> CHOOSE");
                   lcd.setCursor(0, 5);
                   lcd.print("o     OK");
                  
                   delay(100);
                  
                   lcd.setCursor(0, 2);
                   lcd.print("YES");
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      
                      if (buttonStateRIGHT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("NO ");
                         
                         c = 0;
                      }
                      
                      if (buttonStateLEFT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("YES");
                           
                         c = 1;
                      }
                   }
                   while (buttonStateMENU == LOW);
                   //When last saved configuration is selected
                   if (c == 1)  
                   {
                      lcd.clear();
                      lcd.setCursor(0, 2);
                      lcd.print("UPLOADING DATA...");
                      //Reset SI4713
                      digitalWrite(resetPin, LOW); 
                      delay(100);
                      digitalWrite(resetPin, HIGH);
                     
                      I2C_SEND(POWER_UP, 0x12, 0x50, 0x25, 0x25, 0x25);  //Send command POWER_UP with arguments to SI4713
                      I2C_SEND(SET_PROPERTY, 0x00, 0x21, 0x03, 0x00, 0x00);  //Set TX_RDS_DEVIATION to 0x00
                      //Read all configuration from EEPROM and send it to SI4713
                      while (address_ < 113)  
                      {
                         EEPROM_READ(address_);
                      }
                      goto TRANSMITTING;  //Jump here when last settings is ready
                   }
/********************************DEFAULT SETTINGS***************************************************
Description: Transmitter use default settings and user set only transmit frequency and output power.
             Transmit frequency: 76-108 MHz
             Output power: 88-120 dBuV
***************************************************************************************************/
DEFAULT_SETTINGS:
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("DEF. SETTINGS:");
                   lcd.setCursor(0, 4);
                   lcd.print("<- BACK");
                   lcd.setCursor(0, 3);
                   lcd.print("o   SET");
                   lcd.setCursor(0, 5);
                   lcd.print("-> NEXT");
                    
                   delay(100);
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      
                      if (buttonStateLEFT == HIGH)
                      {
                         goto START;
                      }
                      
                      if (buttonStateRIGHT == HIGH)
                      { 
                         goto PROP;
                      }
                   }
                   while ( buttonStateMENU == LOW);

                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print("DEF. SETTINGS:"); 
                lcd.setCursor(0, 4);
                lcd.print("<-/-> CHOOSE");
                lcd.setCursor(0, 5);
                lcd.print("o     OK");
                
                def = 1;
                
                delay(100);
                
                lcd.setCursor(0, 2);
                lcd.print("YES");
                
                do 
                {  
                   buttonStateMENU = digitalRead(buttonMENU);
                   buttonStateRIGHT = digitalRead(buttonRIGHT);
                   buttonStateLEFT = digitalRead(buttonLEFT);
     
                   if (buttonStateRIGHT == HIGH)
                   {
                      lcd.setCursor(0, 2);
                      lcd.print("NO ");
                       
                      def = 0;
                   }
                   
                   if (buttonStateLEFT == HIGH)
                   {
                      lcd.setCursor(0, 2);
                      lcd.print("YES");
                   
                      def = 1;
                   }
                }
                while (buttonStateMENU == LOW);
                //When default settings is selected
                if (def == 1)
                {           
                   digitalWrite(resetPin, LOW); 
                   delay(100);
                   digitalWrite(resetPin, HIGH);
                
                   I2C_SEND(POWER_UP, 0x12, 0x50, 0x25, 0x25, 0x25);
                   I2C_SEND(SET_PROPERTY, 0x00, 0x21, 0x03, 0x00, 0x00);
//************************************COMMAND 0x31 TX_TUNE_POWER******************************                   
OUTPUT_POWER:            
                   lcd.clear(); 
                   lcd.setCursor(0, 0);
                   lcd.print("OUTPUT POWER:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-      BACK");
                   lcd.setCursor(0, 5);
                   lcd.print("<--/--> EXIT");
                   lcd.setCursor(0, 3);
                   lcd.print("o       SET");
         
                   delay(100);
         
                   a = 0;   
                   z = 0;
                  
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                     
                      if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                      {
                         LONG_PUSH();
                      } 
                      
                      if (z > 1 && z <= 100)
                      {
                         goto TRANSMIT_FREQUENCY;
                      }
                      
                      if (z > 100 || a > 100) 
                      {
                         goto DEFAULT_SETTINGS;
                      }
                   }
                   while (buttonStateMENU == LOW);
                   
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("SET POWER:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-/-> INC/DEC");
                   lcd.setCursor(0, 5);
                   lcd.print("o     OK");
      
                   delay(100);
                      
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      //When button is released, increment or decrement of value from the smallest step   
                      if(last_state_right != buttonStateRIGHT && last_state_left != buttonStateLEFT)
                      {
                         l = 0;
                      }
                      //Increment value of power when button is pressed
                      if( buttonStateRIGHT == HIGH)
                      {
                         SET_VALUE_INC(2);
                      }
                      //decrement value of power when button is pressed
                      if( buttonStateLEFT == HIGH)
                      {
                         SET_VALUE_DEC(2);
                      }
                      
                      lcd.setCursor(0, 2);
                      lcd.print(power, DEC);
                      lcd.print("dBuV ");     
                      
                      delay(100);
                   }
                   while (buttonStateMENU == LOW);
    
                   I2C_SEND(TX_TUNE_POWER, 0x00, 0x00, power, 0x00, 0x25);
                   // Waif for CTS=1, STCINT=1 (0x81)
                   do  
                   {
                   Wire.beginTransmission(99); //If SEN=1, 99 id address of SI4713
                   Wire.write(0x14);
   
                   Wire.endTransmission();  //Stop transmitting   
                   
                      Wire.requestFrom(99, 1);  //Request 1 byte from address 99   
                  
                      while(Wire.available())  //Slave may send less than requested    
                      { 
                      STATUS = Wire.read();
                      }
                   }
                   while(STATUS != 0x81);
                   
                   EEPROM_WRITE(112, TX_TUNE_POWER, 0x00, 0x00, power, 0x00, 0x25);
//*****************************COMMAND 0x30 TX_TUNE_FREQ***********************************                 
TRANSMIT_FREQUENCY:
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("OUTPUT FREQ.:");
                   lcd.setCursor(0, 4);
                   lcd.print("o       SET");
                   lcd.setCursor(0, 5);
                   lcd.print("<--/--> EXIT");
         
                   delay(100);
                  
                   a = 0; 
                   z = 0;
                   
                   do
                   { 
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      
                      if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                      {
                         LONG_PUSH();
                      }  
       
                      if (z > 1 && z <= 100)
                      {
                         goto OUTPUT_POWER;
                      }               
                      
                      if (z > 100 || a > 100)
                      {
                         goto DEFAULT_SETTINGS;
                      }   
                   }
                   while (buttonStateMENU == LOW);                  
                 
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("SET FREQUENCY:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-/-> INC/DEC");
                   lcd.setCursor(0, 5);
                   lcd.print("o     OK");
                    
                   delay(100);
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      
                      if(last_state_right != buttonStateRIGHT && last_state_left != buttonStateLEFT)
                      {
                         l = 0;
                      }
                     
                      if( buttonStateRIGHT == HIGH)
                      {
                         SET_VALUE_INC(1); 
                      }
                      
                      if( buttonStateLEFT == HIGH)
                      {
                         SET_VALUE_DEC(1);
                      }
                     
                      TX_TUNE_FREQ_HIGHBYTE = highByte(frequency);  //Create highbyte of transmit frequency
                      TX_TUNE_FREQ_LOWBYTE = lowByte(frequency);  //Create lowbyte of transmit frequency
       
                      lcd.setCursor(0, 2);
                      lcd.print((float)frequency/100);
                      lcd.print("MHz ");
                      
                      delay(100);
                   }
                   while (buttonStateMENU == LOW);
                
                   I2C_SEND(TX_TUNE_FREQ, 0x00, TX_TUNE_FREQ_HIGHBYTE, TX_TUNE_FREQ_LOWBYTE, 0x25, 0x25);
                   
                   do  
                   {
                   Wire.beginTransmission(99);
                   Wire.write(0x14);
   
                   Wire.endTransmission();   
                    
                      Wire.requestFrom(99, 1);  
                      
                      while(Wire.available())    
                      { 
                      STATUS = Wire.read();
                      }
                   }
                   while(STATUS != 0x81);
                  
                   EEPROM_WRITE(105, TX_TUNE_FREQ, 0x00, TX_TUNE_FREQ_HIGHBYTE, TX_TUNE_FREQ_LOWBYTE, 0x25, 0x25);
                }
                
                else
                {                 
/************************************************************************************************************************************
Description: User configure properties when last and default settings are not set.

             Mono/Stero: 0x2100 TX_COMPONENT_ENABLE: 0x0000 = Disable
                                                     0x0001 = Enable  pilot tone  
                                                     0x0002 = Enable  left-right  
                                                     0x0003 = Enable  all (default)
                         0x2102 TX_PILOT_DEVIATION: 0 - 90 kHz (0x0000 - 0x2328), default 6,75 kHz (0x02A3)
                         0x2107 TX_PILOT_FREQUENCY: 0 - 19 kHz (0x0000 - 0x4A38), default 19 kHz (0x4A38)
                         
             Audio deviation: 0x2101 TX_AUDIO DEVIATON: 0 - 90 kHz (0x0000 - 0x2328), default 68,25 kHz (0x1AA9) 
             
             Preemphasis: 0x2106 TX_PREEMPHASIS: 0x00 = 75 µs. Used in USA (default)
                                                 0x01 = 50 µs. Used in Europe, Australia, Japan
                                                 0x02 = Disabled
                                                 0x03 = Reserved
                                                 
             Compressor: 0x2200 TX_ACOMP_ENABLE: 0x00 = Audio limiter disable, audio dynamic range control disabled
                                                 0x01 = Audio limiter disable, audio dynamic range control enabled
                                                 0x02 = Audio limiter enable (default), audio dynamic range control disable (default)
                                                 0x03 = Audio limiter enable, audio dynamic range control enabled
                         0x2101 TX_ACOMP_THRESHOLD: -40-0 dBFS (0xFFD8–0x0), default 0 dBFS (0xFFD8)
                         0x2102 TX_ACOMP_ATTACK_TIME: 0 = 0.5 ms (default)
                                                      1 = 1.0 ms
                                                      2 = 1.5 ms
                                                      3 = 2.0 ms
                                                      4 = 2.5 ms
                                                      5 = 3.0 ms
                                                      6 = 3.5 ms
                                                      7 = 4.0 ms
                                                      8 = 4.5 ms
                                                      9 = 5.0 ms
                         0x2103 TX_ACOMP_RELEASE_TIME: 0 = 100 ms 
                                                       1 = 200 ms
                                                       2 = 350 ms
                                                       3 = 525 ms
                                                       4 = 1000 ms (default) 
                         0x2104 TX_ACOMP_GAIN: 0 - 20 db (0x00 - 0x14), default 15 dB (0x15)
                         
             Limiter: 0x2200 TX_ACOMP_ENABLE
                      0x2205 TX_LIMITER_RELEASE_TIME: 5 = 102.39 ms
                                                      6 = 85.33 ms
                                                      7 = 73.14 ms
                                                      8 = 63.99 ms
                                                      10 = 51.19 ms
                                                      13 = 39.38 ms
                                                      17 = 30.11 ms
                                                      25 = 20.47 ms
                                                      51 = 10.03 ms
                                                      57 = 8.97 ms
                                                      64 = 7.99 ms
                                                      73 = 7.01 ms
                                                      85 = 6.02 ms
                                                      102 = 5.01 ms (default)
                                                      127 = 4.02 ms
                                                      170 = 3.00 ms
                                                      255 = 2.00 ms
                                                      510 = 1.00 ms
                                                      1000 = 0.50 ms
                                                      2000 = 0.25 ms
                                                      
             Audio input level: 0x2104 TX_LINE_INPUT_LEVEL: Bit [13:12] 00: 190mV PK , in. resistance 396kΩ
                                                                        01: 301mV PK , in. resistance 100kΩ
                                                                        10: 416mV PK , in. resistance 74kΩ
                                                                        11: 636mV PK , in. resistance 60kΩ
                                                            Bit[9:0] Maximum input level, default 636mV PK (0x27C)
             
             Line input mute: 0x2105 TX_LINE_INPUT_MUTE: 0x00= No mute (default)
                                                         0x01= Right mute
                                                         0x02= Left mute
                                                         0x03= Mute
             
             Trasmit frequency: 0x30 TX_TUNE FREQ: 76 - 108 MHz (0x1DB0 - 2A30)  
             
             Output power: 0x30 TX_TUNE_POWER: 88 - 120 dBuV (0x58 - 0x78)                            
****************************************************************************************************************************************/
PROP:              digitalWrite(resetPin, LOW); 
                   delay(100);
                   digitalWrite(resetPin, HIGH);
                
                   I2C_SEND(POWER_UP, 0x12, 0x50, 0x25, 0x25, 0x25);
                   I2C_SEND(SET_PROPERTY, 0x00, 0x21, 0x03, 0x00, 0x00);
PROPERTIES:   
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("PROPERTIES:");
                   lcd.setCursor(0, 4);
                   lcd.print("<- BACK");
                   lcd.setCursor(0, 5);
                   lcd.print("-> NEXT");
                   lcd.setCursor(0, 3);
                   lcd.print("o   SET");
                    
                   delay(100);
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      
                      if (buttonStateRIGHT == HIGH)
                      {
                         goto START;
                      }
                      
                       if (buttonStateLEFT == HIGH)
                      {
                         goto DEFAULT_SETTINGS;
                      }
                   }
                   while ( buttonStateMENU == LOW);
 //************************************MONO/STEREO******************************      
//******************************************************************************* 
MONO_STEREO:
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("MONO/STEREO:");
                   lcd.setCursor(0, 2);
                   lcd.print("o       SET");
                   lcd.setCursor(0, 3);
                   lcd.print("<-      BACK");
                   lcd.setCursor(0, 4);
                   lcd.print("->      NEXT");
                   lcd.setCursor(0, 5);
                   lcd.print("<--/--> EXIT");
                   
                   delay(100);
                    
                   a = 0;
                   z = 0;
                    
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);

                      if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                      {
                         LONG_PUSH();
                      }    
                       
                      if (z > 1 && z <= 100)
                      {
                         goto TX_LINE_INPUT_MUTE;
                      }

                      if (a > 1 && a <= 100)
                      {
                         goto TX_AUDIO_DEVIATION;
                      }
                       
                      if (z > 100 || a > 100) 
                      {
                         goto PROPERTIES;
                      }
                   }
                   while ( buttonStateMENU == LOW);
      
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("CHOOSE TYPE:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-/-> CHOOSE");
                   lcd.setCursor(0, 5);
                   lcd.print("o     SET");
                    
                   delay(100); 
                   
                   m = 1;
                   
                   lcd.setCursor(0, 2);
                   lcd.print("STEREO *DEF.");
                    
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                      
                      if (buttonStateRIGHT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("MONO            ");
                          
                         m = 0;
                      }
                       
                      if (buttonStateLEFT == HIGH)
                      {
      
                         lcd.setCursor(0, 2);
                         lcd.print("STEREO *DEF.");
      
                         m = 1;
                      }
                   }
                   while ( buttonStateMENU == LOW); 
//************************************PROPERTIE 0x2100 TX_COMPONENT_ENABLE****************************** 
                   //When stereo is selected
                   if(m == 1)
                   {     
TX_COMPONENT_ENABLE:
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("COMPONENT EN:");
                      lcd.setCursor(0, 2);
                      lcd.print("o       SET");
                      lcd.setCursor(0, 3);
                      lcd.print("<-      BACK");
                      lcd.setCursor(0, 4);
                      lcd.print("->      NEXT");
                      lcd.setCursor(0, 5);
                      lcd.print("<--/--> EXIT");
                      
                      delay(100);
      
                      a = 0;
                      z = 0;
                      i = 0;
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);

                         if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                         {
                            LONG_PUSH();
                         }  
                         
                         if (z > 1 && z <= 100)
                         {
                            goto TX_PILOT_FREQUENCY;
                         }

                         if (a > 1 && a <= 100)
                         {
                            goto TX_PILOT_DEVIATION;
                         }
                         
                         if (z > 100 || a > 100)
                         {
                            goto MONO_STEREO;
                         }
                      }
                      while (buttonStateMENU == LOW);

                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("SET COMPONENTS");
                      lcd.setCursor(0, 4);
                      lcd.print("<-/-> CHOOSE");
                      lcd.setCursor(0, 5);
                      lcd.print("o     OK");
     
                      delay(100);
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
   
                         if (buttonStateRIGHT == HIGH) 
                         {
                            i++;
                            
                            if (i > 2)
                            {
                               i = 0;
                            }
                         }
                         
                         if (buttonStateLEFT == HIGH)
                         {
                            i--;
                            
                            if (i < 0)
                            {
                               i = 2;
                            }
                         }

                         j = TX_COMPONENT_ENABLE_LOWBYTE_VALUE[i]; //Write value from array of values to j
                         char* TX_COMP_EN_DISP[] = {"PILOT TONE   ","LEFT-RIGHT   ","ALL *DEFAULT"};  //Text to display
                         lcd.setCursor(0, 2);
                         lcd.print(TX_COMP_EN_DISP[i]);
                         
                         delay(100);
                      }
                      while (buttonStateMENU == LOW);
                      
                      I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x00, 0x00, j);
                      
                      EEPROM_WRITE (7, SET_PROPERTY, 0x00, 0x21, 0x00, 0x00, j);
                      
//************************************PROPERTIE 0x2102 TX_PILOT_DEVIATION******************************
                      
                      if(j == 0x03 || j == 0x01)  //TX_PILOT_DEVIATION and TX_PILOT_FREQUENCY can be set when the pilot tone is enabled 
                      {
TX_PILOT_DEVIATION:
                         lcd.clear();
                         lcd.setCursor(0, 0);
                         lcd.print("PILOT DEV.:");
                         lcd.setCursor(0, 2);
                         lcd.print("o       SET");
                         lcd.setCursor(0, 3);
                         lcd.print("<-      BACK");
                         lcd.setCursor(0, 4);
                         lcd.print("->      NEXT");
                         lcd.setCursor(0, 5);
                         lcd.print("<--/--> EXIT");
         
                         delay(100);
                         
                         a = 0;
                         z = 0;
                        
                         do
                         {
  
                            buttonStateMENU = digitalRead(buttonMENU);
                            buttonStateRIGHT = digitalRead(buttonRIGHT);
                            buttonStateLEFT = digitalRead(buttonLEFT);
          
                            if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                            {
                               LONG_PUSH();
                            } 
                           
                            if (z > 1 && z <= 100)
                            {
                               goto TX_COMPONENT_ENABLE ;
                            }
  
                            if (a > 1 && a <= 100)
                            {
                               goto TX_PILOT_FREQUENCY;
                            }
                           
                            if (z > 100 || a > 100)
                            {
                               goto MONO_STEREO;
                            }
                         }
                         while (buttonStateMENU == LOW);  
  
                         lcd.clear();
                         lcd.setCursor(0, 0);
                         lcd.print("SET DEVIATION:");
                         lcd.setCursor(0, 4);
                         lcd.print("<-/-> INC/DEC");
                         lcd.setCursor(0, 5);
                         lcd.print("o     OK");
    
                         delay(100);
                         
                         do
                         {
                            buttonStateMENU = digitalRead(buttonMENU);
                            buttonStateRIGHT = digitalRead(buttonRIGHT);
                            buttonStateLEFT = digitalRead(buttonLEFT);
                           
                            if(last_state_right != buttonStateRIGHT && last_state_left != buttonStateLEFT) 
                            {
                               l = 0;
                            }
                           
                            if( buttonStateRIGHT == HIGH)
                            {
                               SET_VALUE_INC(4);
                            }
                           
                            if( buttonStateLEFT == HIGH)
                            {
                               SET_VALUE_DEC(4);
                            }
  
                            TX_PILOT_DEVIATION_HIGHBYTE = highByte(deviations);
                            TX_PILOT_DEVIATION_LOWBYTE = lowByte(deviations);
                           
                            lcd.setCursor(0, 2);
                            lcd.print((float)deviations/100);
                            lcd.print("kHz *6.75 ");
                            
                            delay(100);
                         }
                         while (buttonStateMENU == LOW);
                        
                         I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x02, TX_PILOT_DEVIATION_HIGHBYTE, TX_PILOT_DEVIATION_LOWBYTE);
          
                         EEPROM_WRITE (14, SET_PROPERTY, 0x00, 0x21, 0x02, TX_PILOT_DEVIATION_HIGHBYTE, TX_PILOT_DEVIATION_LOWBYTE);
  
//****************************************PROPERTIE 0x2107 TX_PILOT_FREQUENCY***********************************
TX_PILOT_FREQUENCY:
                         lcd.clear();
                         lcd.setCursor(0, 0);
                         lcd.print("PILOT FREQ.:");
                         lcd.setCursor(0, 2);
                         lcd.print("o       SET");
                         lcd.setCursor(0, 3);
                         lcd.print("<-      BACK");
                         lcd.setCursor(0, 4);
                         lcd.print("->      NEXT");
                         lcd.setCursor(0, 5);
                         lcd.print("<--/--> EXIT");
         
                         delay(100);
                        
                         a = 0;
                         z = 0;
                         
                         do
                         {
                
                            buttonStateMENU = digitalRead(buttonMENU);
                            buttonStateRIGHT = digitalRead(buttonRIGHT);
                            buttonStateLEFT = digitalRead(buttonLEFT);
                
                           if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                           {
                              LONG_PUSH();
                           }   
                          
                           if (z > 1 && z <= 100)
                           {
                              goto TX_PILOT_DEVIATION;
                           }
                
                           if (a > 1 && a <= 100)
                           {
                              goto TX_COMPONENT_ENABLE;
                           }
                          
                           if (z > 100 || a > 100) 
                           {
                              goto MONO_STEREO;
                           }
                        }
                        while (buttonStateMENU == LOW);  
  
                        lcd.clear();
                        lcd.setCursor(0, 0);
                        lcd.print("SET FREQUENCY:");
                        lcd.setCursor(0, 4);
                        lcd.print("<-/-> INC/DEC");
                        lcd.setCursor(0, 5);
                        lcd.print("o     OK");
         
                        delay(100);
                       
                         do
                         {
                            buttonStateMENU = digitalRead(buttonMENU);
                            buttonStateRIGHT = digitalRead(buttonRIGHT);
                            buttonStateLEFT = digitalRead(buttonLEFT);
                           
                            if(last_state_right != buttonStateRIGHT && last_state_left != buttonStateLEFT)
                            {
                               l = 0;
                            }
                
                            if( buttonStateRIGHT == HIGH)
                            {
                               SET_VALUE_INC(3);
                            }
                          
                            if( buttonStateLEFT == HIGH)
                            {
                               SET_VALUE_DEC(3);
                            }
                
                            TX_PILOT_FREQUENCY_HIGHBYTE = highByte(pilot_freq);
                            TX_PILOT_FREQUENCY_LOWBYTE = lowByte(pilot_freq);
                            
                            lcd.setCursor(0, 2);
                            lcd.print((float)pilot_freq/1000, 3);
                            lcd.print("kHz *19 ");
                         
                            delay(100);
                           
                         }
                         while (buttonStateMENU == LOW); 
                     
                         I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x07, TX_PILOT_FREQUENCY_HIGHBYTE, TX_PILOT_FREQUENCY_LOWBYTE);
                       
                         EEPROM_WRITE (21, SET_PROPERTY, 0x00, 0x21, 0x07, TX_PILOT_FREQUENCY_HIGHBYTE, TX_PILOT_FREQUENCY_LOWBYTE);
                      }
                   }

                   else
                   {
                      I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x00, 0x00, 0x00);
                      
                      EEPROM_WRITE (21, SET_PROPERTY, 0x00, 0x21, 0x00, 0x00, 0x00);
                   } 
                   
/************************************PROPERTIE 0x2101 TX_AUDIO_DEVIATION******************************
*******************************************************************************************************/
TX_AUDIO_DEVIATION:
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("AUDIO DEV.:");
                   lcd.setCursor(0, 2);
                   lcd.print("o       SET");
                   lcd.setCursor(0, 3);
                   lcd.print("<-      BACK");
                   lcd.setCursor(0, 4);
                   lcd.print("->      NEXT");
                   lcd.setCursor(0, 5);
                   lcd.print("<--/--> EXIT");
   
                   delay(100);
                   
                   a = 0;
                   z = 0;
                   
                   do
                   {
   
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);

                      if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                      {
                         LONG_PUSH();
                      } 
                      
                      if (z > 1 && z <= 100)
                      {
                         goto MONO_STEREO;
                      }
                
                      if (a > 1 && a <= 100)
                      {
                         goto PREEMPHASIS;
                      }
                      
                      if (z > 100 || a > 100)
                      {
                         goto PROPERTIES;
                      }
                   }
                   while (buttonStateMENU == LOW);

                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("SET DEVIATION:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-/-> INC/DEC");
                   lcd.setCursor(0, 5);
                   lcd.print("o     OK");
   
                   delay(100);
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                        
                      if(last_state_right != buttonStateRIGHT && last_state_left != buttonStateLEFT)
                      {
                         l = 0;
                      }
                        
                      if( buttonStateRIGHT == HIGH)
                      {
                         SET_VALUE_INC(4);
                      }
                        
                      if( buttonStateLEFT == HIGH)
                      {
                         SET_VALUE_DEC(4);
                      }
              
                      TX_AUDIO_DEVIATION_HIGHBYTE = highByte(deviations);
                      TX_AUDIO_DEVIATION_LOWBYTE = lowByte(deviations);
                        
                      lcd.setCursor(0, 2);
                      lcd.print((float)deviations/100);
                      lcd.print("kHz *68.25 ");
              
                      delay(100);
                   }
                   while (buttonStateMENU == LOW);
                    
                   I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x01, TX_AUDIO_DEVIATION_HIGHBYTE, TX_AUDIO_DEVIATION_LOWBYTE);

                   EEPROM_WRITE (28, SET_PROPERTY, 0x00, 0x21, 0x01, TX_AUDIO_DEVIATION_HIGHBYTE, TX_AUDIO_DEVIATION_LOWBYTE);
                   
/************************************PROPERTIE 0x2106 TX_PREEMPHASIS******************************
**************************************************************************************************/
PREEMPHASIS:     
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("PREEMPHASIS:");
                   lcd.setCursor(0, 2);
                   lcd.print("o       SET");
                   lcd.setCursor(0, 3);
                   lcd.print("<-      BACK");
                   lcd.setCursor(0, 4);
                   lcd.print("->      NEXT");
                   lcd.setCursor(0, 5);
                   lcd.print("<--/--> EXIT");
 
                   delay(100);
                     
                   a = 0;
                   z = 0;
                   i = 0;

                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
     
                      if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                      {
                         LONG_PUSH();
                      }       
                        
                      if (z > 1 && z <= 100)
                      {
                         goto TX_AUDIO_DEVIATION;
                      }
                  
                      if (a > 1 && a <= 100)
                      {
                         goto COMPRESSOR;
                      }
                        
                      if (z > 100 || a > 100) 
                      {
                         goto PROPERTIES;
                      }
                   }
                   while ( buttonStateMENU==LOW);
USE_PREEMPHASIS:
                   lcd.clear();
                   lcd.setCursor(0,0);
                   lcd.print("CHOOSE STATE:"); 
                   lcd.setCursor(0,4);
                   lcd.print("<-/-> CHOOSE");
                   lcd.setCursor(0,5);
                   lcd.print("o     OK");
                     
                   delay(100);
                   
                   m = 1;
                   
                   lcd.setCursor(0, 2);
                   lcd.print("YES *DEFAULT");
                   
                   do
                   {    
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                        
                      if (buttonStateRIGHT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("NO           ");
                          
                         m = 0;
                      }
                  
                      if (buttonStateLEFT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("YES *DEFAULT");
                          
                         m = 1;
                      }
                   }
                   while (buttonStateMENU==LOW);
                   //When preemphasis is activated
                   if (m == 1)
                   {  
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("SET VALUE:");
                      lcd.setCursor(0, 4);
                      lcd.print("<-/-> INC/DEC");
                      lcd.setCursor(0, 5);
                      lcd.print("o     OK");
   
                      delay(100);
                      
                      lcd.setCursor(0, 2);
                      lcd.print("75us *DEFAULT");
      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
                       
                         if (buttonStateRIGHT == HIGH)
                         {
                            lcd.setCursor(0, 2);
                            lcd.print("50us         ");
                            i = 1;
                         }
                
                         if (buttonStateLEFT == HIGH)
                         {
                            lcd.setCursor(0, 2);
                            lcd.print("75us *DEFAULT");
                            i = 0;
                            
                         }
                          
                         j = TX_PREEMPHASIS_LOWBYTE_VALUE[i];
                       
                         delay(100);
                      }
                      while (buttonStateMENU == LOW);
                       
                      I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x06, 0x00, j);
                       
                      EEPROM_WRITE (35, SET_PROPERTY, 0x00, 0x21, 0x06, 0x00, j);
                   }     
                   else 
                   {    
                   I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x06, 0x00, 0x02); 
                     
                   EEPROM_WRITE (35, SET_PROPERTY, 0x00, 0x21, 0x06, 0x00, 0x02);
                   } 
                   
/*********************************** COMPRESSOR*****************************************
****************************************************************************************/
COMPRESSOR:
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("COMPRESSOR:");
                   lcd.setCursor(0, 2);
                   lcd.print("o       SET");
                   lcd.setCursor(0, 3);
                   lcd.print("<-      BACK");
                   lcd.setCursor(0, 4);
                   lcd.print("->      NEXT");
                   lcd.setCursor(0, 5);
                   lcd.print("<--/--> EXIT");
                     
                   delay(100);
                   
                   a = 0;
                   z = 0;
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
               
                      if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                      {
                         LONG_PUSH();
                      }
                     
                      if (z > 1 && z <= 100)
                      {
                         goto PREEMPHASIS;
                      }
              
                      if (a > 1 && a <= 100)
                      {
                         goto LIMITER;
                      }
                     
                      if (z > 100 || a > 100)
                      {
                         goto PROPERTIES;
                      }
                   }
                   while ( buttonStateMENU == LOW);
                    
                   lcd.clear();
                   lcd.setCursor(0, 0);
                   lcd.print("CHOOSE STATE:");
                   lcd.setCursor(0, 4);
                   lcd.print("<-/-> CHOOSE");
                   lcd.setCursor(0, 5);
                   lcd.print("o     OK");
                 
                   delay(100);
                   
                   m = 0;
                   komp = 0;
                   
                   lcd.setCursor(0, 2);
                   lcd.print("NO *DEFAULT ");
                   
                   do
                   {
                      buttonStateMENU = digitalRead(buttonMENU);
                      buttonStateRIGHT = digitalRead(buttonRIGHT);
                      buttonStateLEFT = digitalRead(buttonLEFT);
                
                      if (buttonStateRIGHT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("YES          "); 
                       
                         m = 1;
                         komp = 1;
                      }
                      
                      if (buttonStateLEFT == HIGH)
                      {
                         lcd.setCursor(0, 2);
                         lcd.print("NO *DEFAULT  ");
                      
                         m = 0;
                         komp = 0;
                      }
                   }
                   while ( buttonStateMENU == LOW); 
                   //Compressor is activated
                   if(m == 1)
                   {  
                      //When limiter was activated
                      if(lim == 1)
                      { 
                         I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x03);
                         EEPROM_WRITE (42, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x03);
                     
                      }
                      //When limiter was not activated
                      else
                      { 
                         EEPROM_WRITE (42, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x01);
                         I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x01);
      
                      }
               
                      
//************************************PROPERTIE 0x2200 TX_ACOMP_THRESHOLD******************************             
TX_ACOMP_THRESHOLD: 
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("THRESHOLD:");
                      lcd.setCursor(0, 2);
                      lcd.print("o:      SET");
                      lcd.setCursor(0, 3);
                      lcd.print("<-      BACK");
                      lcd.setCursor(0, 4);
                      lcd.print("->      NEXT");
                      lcd.setCursor(0, 5);
                      lcd.print("<--/--> EXIT");
                      
                      delay(100);
    
                      a = 0;
                      z = 0;
                      i = 0;
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);

                         if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                         {
                            LONG_PUSH();
                         }  
                         
                         if (z > 1 && z <= 100)
                         {
                            goto TX_ACOMP_GAIN;
                         }
                
                         if (a > 1 && a <= 100)
                         {
                            goto TX_ACOMP_ATTACK_TIME;
                         }
                         
                         if (z > 100 || a > 100)
                         {
                            goto COMPRESSOR;
                         }
                      }
                      while (buttonStateMENU == LOW);

                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("SET THRESHOLD:");
                      lcd.setCursor(0, 4);
                      lcd.print("<-/-> INC/DEC");
                      lcd.setCursor(0, 5);
                      lcd.print("o OK");
     
                      delay(100);
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);

                         if (buttonStateRIGHT == HIGH)
                         {
                            i++;
                            
                            if (i > 40)
                            {
                               i = 0;
                            }
                         }
                         
                         if (buttonStateLEFT == HIGH)
                         {
                            i--;
                            
                            if (i < 0)
                            {
                               i = 40;
                            }
                         }
                         
                         j = TX_ACOMP_THRESHOLD_LOWBYTE_VALUE[i];
                         //Create correct value to display
                         int highbyte = TX_ACOMP_THRESHOLD_HIGHBYTE_VALUE[i]<<8;
                         int end_value = highbyte + j;
                         lcd.setCursor(0, 2);
                         lcd.print(end_value, DEC);
                         lcd.print("dB *-40");
                        
                         delay(100);
                      }
                      while (buttonStateMENU == LOW);
                      
                      I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x01, TX_ACOMP_THRESHOLD_HIGHBYTE_VALUE[i], j);
                     
                      EEPROM_WRITE (49, SET_PROPERTY, 0x00, 0x22, 0x01, TX_ACOMP_THRESHOLD_HIGHBYTE_VALUE[i], j); 
                      
//************************************PROPERTIE 0x2202 TX_ACOMP_ATTACK_TIME****************************** 
TX_ACOMP_ATTACK_TIME:     
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("ATTACK TIME:");
                      lcd.setCursor(0, 2);
                      lcd.print("o       SET");
                      lcd.setCursor(0, 3);
                      lcd.print("<-      BACK");
                      lcd.setCursor(0, 4);
                      lcd.print("->      NEXT");
                      lcd.setCursor(0, 5);
                      lcd.print("<--/--> EXIT");
                      
                      delay(100);
                      
                      a = 0;
                      z = 0;
                      i = 0;
                      
                      do
                      {

                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
                         
                         if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                         {
                            LONG_PUSH();
                         } 
                         
                         if (z > 1 && z <= 100)
                         {
                            goto TX_ACOMP_THRESHOLD;
                         }

                         if (a > 1 && a <= 100)
                         {
                            goto TX_ACOMP_RELEASE_TIME;
                         }
                         
                         if (z > 100 || a > 100)
                         {
                            goto COMPRESSOR;
                         }
                      }
                      while (buttonStateMENU == LOW);  

                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("SET A. TIME:");
                      lcd.setCursor(0, 4);
                      lcd.print("<-/-> INC/DEC");
                      lcd.setCursor(0, 5);
                      lcd.print("o          OK");
     
                      delay(100);
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);

                         if (buttonStateRIGHT == HIGH)
                         {
                            i++;
                            display_value=display_value + 0.5;
                            if (i > 10)
                            {
                               i = 0;
                            }
                            if (display_value > 5.0)
                            {
                               display_value = 0.0;
                            }
                         }
                
                         if (buttonStateLEFT == HIGH)
                         {
                            i--;
                            display_value = display_value - 0.5;
                            if (i < 0)
                            {
                               i = 10;
                            }
                            
                            if (display_value < 0.0)
                            {
                               display_value = 5.0;
                            }
                         }
                         
                         j = TX_ACOMP_ATTACK_TIME_LOWBYTE_VALUE[i];
                         
                         lcd.setCursor(0, 2);
                         lcd.print(display_value, 1);
                         lcd.print("ms *0.5");
                         
                         delay(100);
                      }
                      while (buttonStateMENU == LOW);
                      
                      I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x02, 0x00, j); 
      
                      EEPROM_WRITE (56, SET_PROPERTY, 0x00, 0x22, 0x02, 0x00, j);
                      
//************************************PROPERTIE 0x2203 TX_ACOMP_RELEASE_TIME******************************
TX_ACOMP_RELEASE_TIME:   
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("RELEASE TIME:");
                      lcd.setCursor(0, 2);
                      lcd.print("o       SET");
                      lcd.setCursor(0, 3);
                      lcd.print("<-      BACK");
                      lcd.setCursor(0, 4);
                      lcd.print("->      NEXT");
                      lcd.setCursor(0, 5);
                      lcd.print("<--/--> EXIT");
                      
                      delay(100);
                      
                      a = 0;
                      z = 0;
                      i = 0;
                      
                      do
                      {

                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
       
                         if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                         {
                            LONG_PUSH();
                         }   
                         
                         if (z > 1 && z <= 100)
                         {
                            goto TX_ACOMP_ATTACK_TIME;
                         }
                
                         if (a > 1 && a <=100)
                         {
                            goto TX_ACOMP_GAIN;
                         }
                         
                         if (z > 100 || a > 100)
                         {
                            goto COMPRESSOR;
                         }
                      }
                      while (buttonStateMENU == LOW);  

                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("SET R. TIME:");
                      lcd.setCursor(0, 4);
                      lcd.print("<-/-> INC/DEC");
                      lcd.setCursor(0, 5);
                      lcd.print("o     OK");

                      delay(100);
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
       
                         if (buttonStateRIGHT == HIGH)
                         {
                            i++;
                            
                            if (i > 4)
                            {
                               i = 0;
                            }
                         }
                
                         if (buttonStateLEFT == HIGH)
                         {
                            i--;
                          
                            if (i < 0)
                            {
                               i = 4;
                            }
                         }
                         
                         j = TX_ACOMP_RELEASE_TIME_LOWBYTE_VALUE[i];
                         
                         int TX_ACOMP_RELEASE_TIME_DISP[] = {100, 200, 350, 525, 1000};
                         
                         lcd.setCursor(0, 2);
                         lcd.print(TX_ACOMP_RELEASE_TIME_DISP[i]);
                         lcd.print("ms *1000");
                         
                         delay(100);
                      }
                      while (buttonStateMENU == LOW); 
                      
                      I2C_SEND(SET_PROPERTY, 0x00, 0x22, 0x03, 0x00, j);
                      
                      EEPROM_WRITE(63, SET_PROPERTY, 0x00, 0x22, 0x03, 0x00, j); 
                      
//************************************PROPERTIE 0x2204 TX_ACOMP_GAIN******************************
TX_ACOMP_GAIN:  
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("GAIN:");
                      lcd.setCursor(0, 2);
                      lcd.print("o       SET");
                      lcd.setCursor(0, 3);
                      lcd.print("<-      BACK");
                      lcd.setCursor(0, 4);
                      lcd.print("->      NEXT");
                      lcd.setCursor(0, 5);
                      lcd.print("<--/--> EXIT");
                      
                      delay(100);
                      
                      a = 0;
                      z = 0;
                      i = 0;
                     
                      do
                      {
                
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
                
                         if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                         {
                            LONG_PUSH();
                         } 
                         
                         if (z > 1 && z <= 100)
                         {
                            goto TX_ACOMP_RELEASE_TIME;
                         }
                
                         if (a > 1 && a <= 100)
                         {
                            goto TX_ACOMP_ATTACK_TIME;
                         }
                        
                         if (z > 100 || a > 100)
                         {
                            goto COMPRESSOR;
                         }
                
                      }
                      while (buttonStateMENU == LOW);  
                
                      lcd.clear();
                      lcd.setCursor(0, 0);
                      lcd.print("SET GAIN:");
                      lcd.setCursor(0, 4);
                      lcd.print("<-/-> INC/DEC");
                      lcd.setCursor(0, 5);
                      lcd.print("o     OK");
                      
                      delay(100);
                      
                      do
                      {
                         buttonStateMENU = digitalRead(buttonMENU);
                         buttonStateRIGHT = digitalRead(buttonRIGHT);
                         buttonStateLEFT = digitalRead(buttonLEFT);
                      
                         lcd.setCursor(0, 2);
                         lcd.print(j, DEC);
                         lcd.print("dB *15 ");
                       
                         delay(100);
                
                         if (buttonStateRIGHT == HIGH)
                         {
                            i++;
                            
                            if (i > 20)
                            {
                               i = 0;
                            }
                         }
                
                         if (buttonStateLEFT == HIGH)
                         {
                            i--;
                            
                            if (i < 0)
                            {
                               i = 20;
                            }
                         }
                         
                         j = TX_ACOMP_GAIN_LOWBYTE_VALUE[i];
                      }
                      while (buttonStateMENU == LOW);
                      
                      I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x04, 0xFF, j);
                      
                      EEPROM_WRITE (70, SET_PROPERTY, 0x00, 0x22, 0x04, 0xFF, j);
                   }
                   else
                   {   //Block compressor when limiter is activated
                       if(lim==1)
                       { 
                          I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x02);
                          EEPROM_WRITE (42, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x02);
                       }
                       //Block compressor when limiter is not activated
                       else
                       { 
                          I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x00);
                          EEPROM_WRITE (42, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x00);
                       }
                    }

/************************************LIMITER********************************************
***************************************************************************************/
LIMITER:    
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print("LIMITER:");
                    lcd.setCursor(0, 2);
                    lcd.print("o       SET");
                    lcd.setCursor(0, 3);
                    lcd.print("<-      BACK");
                    lcd.setCursor(0, 4);
                    lcd.print("->      NEXT");
                    lcd.setCursor(0, 5);
                    lcd.print("<--/--> EXIT");
                     
                    delay(100);
                   
                    a = 0;
                    z = 0;
                   
                    do
                    {
                       buttonStateMENU = digitalRead(buttonMENU);
                       buttonStateRIGHT = digitalRead(buttonRIGHT);
                       buttonStateLEFT = digitalRead(buttonLEFT);
               
                       if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                       {
                          LONG_PUSH();
                       }
                     
                       if (z > 1 && z <= 100)
                       {
                          goto COMPRESSOR;
                       }
              
                       if (a > 1 && a <= 100)
                       {
                          goto TX_LINE_INPUT_LEVEL;
                       }
                     
                       if (z > 100 || a > 100)
                       {
                          goto PROPERTIES;
                       }
                    }
                    while ( buttonStateMENU == LOW);
                    
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print("CHOOSE STATE:");
                    lcd.setCursor(0, 4);
                    lcd.print("<-/-> CHOOSE");
                    lcd.setCursor(0, 5);
                    lcd.print("o     OK");
                 
                    delay(100);
                   
                    m = 1;
                    lim = 1;
                   
                    lcd.setCursor(0, 2);
                    lcd.print("YES *DEFAULT ");
                   
                    do
                    {
                       buttonStateMENU = digitalRead(buttonMENU);
                       buttonStateRIGHT = digitalRead(buttonRIGHT);
                       buttonStateLEFT = digitalRead(buttonLEFT);
                
                       if (buttonStateRIGHT == HIGH)
                       {
                          lcd.setCursor(0, 2);
                          lcd.print("NO          ");
                      
                          m = 0;
                          lim = 0;
                       }
                      
                       if (buttonStateLEFT == HIGH)
                       {
                          lcd.setCursor(0, 2);
                          lcd.print("YES *DEFAULT"); 
                       
                          m = 1;
                          lim = 1;
                       }
                    }
                    while ( buttonStateMENU == LOW); 

                    if(m == 1)
                    {  
                      //Set limiter when compressor is not activated
                       if(komp == 0)
                       { 
                          I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x02);
                          EEPROM_WRITE (77, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x02);
                       }
                       //Set limiter when compressor is activated
                       else
                       { 
                          I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x03);
                          EEPROM_WRITE (77, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x03);
                       }
//************************************TX_LIMITER_RELEASE_TIME****************************************  
                       lcd.clear();
                       lcd.setCursor(0, 0);
                       lcd.print("RELEASE TIME:");
                       lcd.setCursor(0, 2);
                       lcd.print("o       SET");
                       lcd.setCursor(0, 3);
                       lcd.print("<-      BACK");
                       lcd.setCursor(0, 4);
                       lcd.print("->      NEXT");
                       lcd.setCursor(0, 5);
                       lcd.print("<--/--> EXIT");
                      
                       delay(100);
                      
                       a = 0;
                       z = 0;
                       i = 0;
                     
                       do
                       {
                          buttonStateMENU = digitalRead(buttonMENU);
                          buttonStateRIGHT = digitalRead(buttonRIGHT);
                          buttonStateLEFT = digitalRead(buttonLEFT);
     
                          if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                          {
                             LONG_PUSH();
                          }                 
                       
                          if (z > 100 || a > 100)
                          {
                             goto LIMITER;
                          }  
                       }
                       while (buttonStateMENU == LOW);
                      
                       lcd.clear();
                       lcd.setCursor(0, 0);
                       lcd.print("SET TIME:");
                       lcd.setCursor(0, 4);
                       lcd.print("<-/-> INC/DEC");
                       lcd.setCursor(0, 5);
                       lcd.print("o     OK");
                      
                       delay(100);
                       
                       do
                       {  
                          buttonStateMENU = digitalRead(buttonMENU);
                          buttonStateRIGHT = digitalRead(buttonRIGHT);
                          buttonStateLEFT = digitalRead(buttonLEFT);
                   
                          int TX_LIM_REL_TIME_DISP[] = {25, 50, 100, 200, 300, 402, 501, 602, 701, 799, 897, 1003, 2047, 3011, 3938, 5119, 6399, 7314, 8533, 10239};  //Values to diplay
                          
                          if (buttonStateRIGHT == HIGH)
                          {
                             i++;
                         
                             if (i > 19)
                             {
                                i = 0;
                             }
                          }
                       
                          if (buttonStateLEFT == HIGH)
                          {
                             i--;
                             if (i < 0)
                             {
                                i = 19;
                             }
                  
                          }
                          j = TX_LIMITER_RELEASE_TIME_LOWBYTE_VALUE[i];
                     
                          lcd.setCursor(0, 2);
                          lcd.print((float)TX_LIM_REL_TIME_DISP[i]/100, 2);
                          lcd.print("ms *5.01");
                          
                          delay(100);
                        }
                        while (buttonStateMENU == LOW);
                       
                        I2C_SEND (SET_PROPERTY, 0x00, 0x22,0x05, TX_LIMITER_RELEASE_TIME_HIGHBYTE_VALUE[i], j); 
                        
                        EEPROM_WRITE (84, SET_PROPERTY, 0x00, 0x22,0x05, TX_LIMITER_RELEASE_TIME_HIGHBYTE_VALUE[i], j); 
                        }
                    else
                    {
                      //Block limiter when compressor is not activated
                       if(komp == 0)
                       { 
                          I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x00);
                          EEPROM_WRITE (77, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x00);
                       }
                       //Block limiter when compressor is activated
                       else
                       { 
                          I2C_SEND (SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x01);
                          EEPROM_WRITE (77, SET_PROPERTY, 0x00, 0x22, 0x00, 0x00, 0x01);
                       } 
                    } 
     
/************************************PROPERTIE 0x2104 TX_LINE_INPUT_LEVEL*****************************
******************************************************************************************************/
TX_LINE_INPUT_LEVEL:
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print("CHAN. LEVEL:");
                    lcd.setCursor(0, 2);
                    lcd.print("o       SET");
                    lcd.setCursor(0, 3);
                    lcd.print("<-      BACK");
                    lcd.setCursor(0, 4);
                    lcd.print("->      NEXT");
                    lcd.setCursor(0, 5);
                    lcd.print("<--/--> EXIT");
                    
                    delay(100);
                    
                    a = 0;
                    z = 0;
                    i = 0;
                    
                    do
                    {
                       buttonStateMENU = digitalRead(buttonMENU);
                       buttonStateRIGHT = digitalRead(buttonRIGHT);
                       buttonStateLEFT = digitalRead(buttonLEFT);
                
                       if(buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                       {
                          LONG_PUSH();
                       }      
                       
                       if (z > 1 && z<= 100)
                       {
                         goto COMPRESSOR;
                       }
                
                       if (a > 1 && a<= 100)
                       {
                          goto TX_LINE_INPUT_MUTE;
                       }
                       
                       if (z > 100 || a > 100)
                       {
                          goto PROPERTIES;
                       }
                    }
                    while (buttonStateMENU == LOW);
                    
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print("SET LEVEL:");
                    lcd.setCursor(0, 4);
                    lcd.print("<-/-> INC/DEC");
                    lcd.setCursor(0, 5);
                    lcd.print("o OK");
    
                    delay(100);
   
                    do
                    {
                       buttonStateMENU = digitalRead(buttonMENU);
                       buttonStateRIGHT = digitalRead(buttonRIGHT);
                       buttonStateLEFT = digitalRead(buttonLEFT);

                       if( buttonStateRIGHT == HIGH)
                       {
                          SET_VALUE_INC(5);
                       }
                       
                       if( buttonStateLEFT == HIGH)
                       {
                          SET_VALUE_DEC(5);
                       }
                       
                       TX_LINE_INPUT_LEVEL_HIGHBYTE = highByte(tx_line_in_lev);
                       TX_LINE_INPUT_LEVEL_LOWBYTE = lowByte(tx_line_in_lev);
                       //Clear or set bits 4 and 5 according to input level for settings maximum input level
                       if(tx_line_in_lev <= 190)
                       {
                          bitClear(TX_LINE_INPUT_LEVEL_HIGHBYTE, 4);
                          bitClear(TX_LINE_INPUT_LEVEL_HIGHBYTE, 5);
                       }
                
                       if(tx_line_in_lev > 190 && tx_line_in_lev <= 301)
                       {
                          bitSet(TX_LINE_INPUT_LEVEL_HIGHBYTE, 4);
                          bitClear(TX_LINE_INPUT_LEVEL_HIGHBYTE, 5);
                       }
                      
                       if(tx_line_in_lev > 301 && tx_line_in_lev <= 416)
                       {
                          bitClear(TX_LINE_INPUT_LEVEL_HIGHBYTE, 4);
                          bitSet(TX_LINE_INPUT_LEVEL_HIGHBYTE, 5);
                       }
                       
                       if(tx_line_in_lev > 416)
                       {
                          bitSet(TX_LINE_INPUT_LEVEL_HIGHBYTE, 4);
                          bitSet(TX_LINE_INPUT_LEVEL_HIGHBYTE, 5);
                       }
                       
                       lcd.setCursor(0, 2);
                       lcd.print(tx_line_in_lev, DEC);
                       lcd.print("mV(PK) *636");
                       
                       delay(100);
                   
                    }
                    while (buttonStateMENU == LOW);
                
                    I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x04, TX_LINE_INPUT_LEVEL_HIGHBYTE, TX_LINE_INPUT_LEVEL_LOWBYTE) ;
                    
                    EEPROM_WRITE (91, SET_PROPERTY, 0x00, 0x21, 0x04, TX_LINE_INPUT_LEVEL_HIGHBYTE, TX_LINE_INPUT_LEVEL_LOWBYTE);
                    
/************************************PROPERTIE 0x2105 TX_LINE_INPUT_MUTE*******************************
******************************************************************************************************/
TX_LINE_INPUT_MUTE:
                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print("CHANNELS MUTE:");
                    lcd.setCursor(0, 2);
                    lcd.print("o       SET");
                    lcd.setCursor(0, 3);
                    lcd.print("<-      BACK");
                    lcd.setCursor(0, 4);
                    lcd.print("->      NEXT");
                    lcd.setCursor(0, 5);
                    lcd.print("<--/--> EXIT");
                    
                    delay(100);
                  
                    a = 0;
                    z = 0;
                    i = 0;
                    
                    do
                    {
                       buttonStateMENU = digitalRead(buttonMENU);
                       buttonStateRIGHT = digitalRead(buttonRIGHT);
                       buttonStateLEFT = digitalRead(buttonLEFT);
                
                       if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                       {
                          LONG_PUSH();
                       }  
                       
                       if (z > 1 && z <= 100)
                       {
                          goto TX_LINE_INPUT_LEVEL;
                       }
                
                       if (a > 1 && a <= 100)
                       {
                          goto MONO_STEREO;
                       }
                       
                       if (z > 100 || a > 100)
                       {
                          goto PROPERTIES;
                       }
                    }
                    while (buttonStateMENU == LOW);


                    lcd.clear();
                    lcd.setCursor(0, 0);
                    lcd.print("CHOOSE CHAN.:");
                    lcd.setCursor(0, 4);
                    lcd.print("<-/-> INC/DEC");
                    lcd.setCursor(0, 5);
                    lcd.print("o     OK");
                   
                    delay(100);
                    
                    do
                    {
                       buttonStateMENU = digitalRead(buttonMENU);
                       buttonStateRIGHT = digitalRead(buttonRIGHT);
                       buttonStateLEFT = digitalRead(buttonLEFT);
                  
                       if (buttonStateRIGHT == HIGH)
                       {
                          i++;
                          if (i > 3)
                          {
                             i = 0;
                          }
                       }
                       
                       if (buttonStateLEFT == HIGH)
                       {
                          i--;
                          if (i < 0)
                          {
                             i = 3;
                          }
                
                       }
                       char* TX_LINE_INPUT_MUTE_DISP[] = {"NO MUTE *DEF.","RIGHT MUTE   ","LEFT MUTE    ", "MUTE ALL     "};  //Text to display
                       j = TX_LINE_INPUT_MUTE_LOWBYTE_VALUE[i];
                       
                       lcd.setCursor(0, 2);
                       lcd.print(TX_LINE_INPUT_MUTE_DISP[i]);
                       
                       delay(100);
                    }
                    while (buttonStateMENU == LOW);
                
                    I2C_SEND (SET_PROPERTY, 0x00, 0x21, 0x05, 0x00, j); 
                    
                    EEPROM_WRITE (98, SET_PROPERTY, 0x00, 0x21, 0x05, 0x00, j);
                                        
                    goto OUTPUT_POWER;  //After completely setting of properties go to set of transmit frequency and power
                 }
                 
/***********************************************TRANSMIT*********************************************  
Description: Chip is transmitting and informating about transmit frequency and output power
             until button "OK" is not press. When transmit state is stopped by "OK" button, 
             pressing of left button cause jump to main menu. When is "OK/MENU" button pressed again,
             transmitting will continue.    
****************************************************************************************************/
                    
TRANSMITTING:       
                    TUNE_STATUS();
                    frequency_transmit = (tune_status[2]<< 8) + tune_status[3]; //Get real value of transmit frequency
                    
                    push_number = 0;
                    transmit = 1;
                    
                    while(1){
                               buttonStateMENU = digitalRead(buttonMENU);
                               //Chip is transmitting
                               if(transmit == 1)
                               {
                                  lcd.clear();
                                  
                                  while(buttonStateMENU == 0)
                                  {
                                     buttonStateMENU = digitalRead(buttonMENU);
                                     //Information screen about transmit frequency and output power
                                     lcd.setCursor(0, 0);
                                     lcd.print("STATE:TRANSMIT");
                                     lcd.setCursor(0, 2);
                                     lcd.print("F: ");
                                     lcd.print((float)frequency_transmit/100, 2);
                                     lcd.print(" MHz");
                                     lcd.setCursor(0, 3);
                                     lcd.print("P: ");
                                     lcd.print(tune_status[5], DEC);
                                     //lcd.print(power, DEC);
                                     lcd.print(" dBuV");
                                     lcd.setCursor(0, 5);
                                     lcd.print("o STOP" );
                                  }
                               }
                               //Transmitting stopped when button is pressed
                               if (buttonStateMENU == HIGH)
                               {
                                  push_number++;
                               }
                               
                               if (push_number == 1)
                               {
                                  I2C_SEND(POWER_DOWN, 0x25, 0x25, 0x25, 0x25, 0x25);  //Command POWER_DOWN to stop transmit
                                  buttonStateRIGHT = digitalRead(buttonRIGHT);
                                  buttonStateLEFT = digitalRead(buttonLEFT);
                                  // Go to start when the one of the buttons is pressed during the transmit state stop
                                  if (buttonStateLEFT == HIGH || buttonStateRIGHT == HIGH)
                                  {
                                     goto START;  // Go to menu when the one of the buttons is pressed during the transmit state stop
                                  }
                                
                                  lcd.setCursor(0, 0);
                                  lcd.print("STATE:STOPPED  ");
                                  lcd.setCursor(0, 4);
                                  lcd.print("o     START" );
                                  lcd.setCursor(0, 5);
                                  lcd.print("<-/-> EXIT" );
                                  
                                  transmit = 0;   
                              }  
                              //Transmitting restart when button is pressed second time
                              if (push_number == 2)
                              {
                                 lcd.clear();
                                 lcd.setCursor(0, 2);
                                 lcd.print("UPLOADING DATA...");
                                 
                                 digitalWrite(resetPin, LOW);
                                 delay(100);
                                 digitalWrite(resetPin, HIGH);
                                 
                                 I2C_SEND(POWER_UP, 0x02, 0x50, 0x25, 0x25, 0x25);
                                 I2C_SEND(SET_PROPERTY, 0x00, 0x21, 0x03, 0x00, 0x00);
                                 // If SI4713 was set by default settings, from EEPROM will be read only transmit frequency and output power, else all data from EEPROM
                                 if(def == 1)
                                 {
                                    address_ = 105;
                                 }
                                 
                                 else
                                 {
                                    address_ = 7;
                                 }
                                 
                                while (address_ < 113)
                                {
                                   EEPROM_READ(address_);
                                } 
                                
                                transmit = 1;
                                push_number = 0;  
                          
                              } 
                              delay(100);
                           }
}
/**********************************FUNCTIONS**********************************                                 
*****************************************************************************/

void GETPROPERTY (byte comm, byte PROP_H, byte PROP_L)
{
   Wire.beginTransmission(99); // for SEN = 0 address 17
   Wire.write(comm);

   Wire.write(0x00);
   Wire.write(PROP_H);
   Wire.write(PROP_L);
   
   Wire.endTransmission();    // stop transmitting
 
   Wire.requestFrom(99, 4);    // request 1 bytes from slave device #2

   while(Wire.available())    // slave may send less than requested
   { 
     lcd.clear();
      byte hodnota_prop = Wire.read(); // receive a byte as character
      lcd.setCursor(0,1);
      lcd.println(hodnota_prop,BIN);
      delay(5000);
      
   }

}
/*************************************************************************************************************
Function: void TUNE_STATUS()

Description: Function gets value of actual value of transmit frequency and output power and writes it to array
                
Example: TUNE_STATUS();
*************************************************************************************************************/
void TUNE_STATUS() 
{
   Wire.beginTransmission(99); 
   
   Wire.write(0x33);       
   Wire.write(0x01);
   
   Wire.endTransmission();  
   Wire.requestFrom(99, 8); 
   
   byte_number = 0;
  
   while(Wire.available())
   { 
      value = Wire.read();
      tune_status[byte_number] = value;  
      byte_number++;
   }
}
/***************************************************************************************************************
Function: byte I2CSEND (byte command, byte arg1, byte arg2, byte arg3, byte arg4, byte arg5)

Description: Function writes data to SI4713 and read status. Parameter is send when value is different than 0x25
             This function ends when CTS is set (response from SI is 0x80).
Parametres: command - number of SI4713 command
            arg1 - values of command arguments
            arg2
            arg3
            arg4
            arg5
         
Example: I2CSEND(POWER_UP, 0x02, 0x50, 0x25, 0x25, 0x25);
***************************************************************************************************************/
byte I2C_SEND(byte command, byte arg1, byte arg2, byte arg3, byte arg4, byte arg5)  //Send commands and arguments to SI4713 and read status
{
   Wire.beginTransmission(99); //If SEN=1, 99 id address of SI4713
   Wire.write(command);
   
   if(arg1 != 0x25)
   {
      Wire.write(arg1);
   }
   
   if(arg2 != 0x25)
   {
      Wire.write(arg2);
   }
   
   if(arg3 != 0x25)
   {
      Wire.write(arg3);
   }

   if(arg4 != 0x25)
   {
      Wire.write(arg4);
   }

   if(arg5 != 0x25)
   {
      Wire.write(arg5);
   }
   
   Wire.endTransmission();  //Stop transmitting   
   do  
   {
      Wire.requestFrom(99, 1);  //Request 1 byte from address 99   
   
      while(Wire.available())  //Slave may send less than requested    
      { 
         STATUS = Wire.read(); 
      }
   }
   while(STATUS != 0x80);
  
   //delay(500);
}

/***************************************************************************************************************
Function: int SET_VALUE_INC(int func)

Description: Function Increments value of parameter which is choose by input number when right button is pressed.
             Increment step depends on the time of keep button pressed (value of l)
Parametres: func - number of setting parameter (0 - 5)
               1 - transmit frequency
               2 - output power
               3 - pilot frequency
               4 - audio and pilot deviation
               5 - line input level
               
Return: value of parameter
               
Example: SET_VALUE_INC(4);
****************************************************************************************************************/
int SET_VALUE_INC(int func) 
{
   l++;

   if(frequency >= frequency_max) 
   {
      frequency = frequency_min;
      
      TX_TUNE_FREQ_HIGHBYTE = 0x00; 
      TX_TUNE_FREQ_LOWBYTE = 0x00; 
      
      l = 0;
   }

   if(power >= power_max)
   {
       power = power_min;
      
       l = 0;
   }

   if(pilot_freq >= pilot_freq_max)
   {
      pilot_freq = pilot_freq_min;
      
      TX_PILOT_FREQUENCY_HIGHBYTE = 0x00;
      TX_PILOT_FREQUENCY_LOWBYTE = 0x00;
      
      l = 0;
   }

   if(deviations >= deviations_max)
   {
      deviations = deviations_min;
      
      TX_PILOT_DEVIATION_HIGHBYTE = 0x00;
      TX_PILOT_DEVIATION_LOWBYTE = 0x00;
      
      TX_AUDIO_DEVIATION_HIGHBYTE = 0x00;
      TX_AUDIO_DEVIATION_LOWBYTE = 0x00;
      
      l = 0;
   }

   if(tx_line_in_lev >= tx_line_in_lev_max)
   {
      tx_line_in_lev = tx_line_in_lev_min;
      
      l = 0;
   }
  
   if (l > 5 && l < 25)    
   {
      switch(func)
      {
     
         case 1:
         frequency = frequency + 100; 
         return frequency;
         break;
        
         case 2:
         power = power + 2;
         return power;
         break;
       
         case 3:
         pilot_freq = pilot_freq + 500;  
         return pilot_freq;
         break;
        
         case 4:
         deviations = deviations + 50;
         return deviations;
         break;
        
         case 5:
         tx_line_in_lev = tx_line_in_lev + 10;
         return tx_line_in_lev;
         break;
      }
   }

   if (l >= 25)   
   {
      switch(func)
      {
         case 1:
         frequency = frequency + 500;
         return frequency;
         break;
         
         case 2:
         power = power + 5;
         return power;
         break;

         case 3:
         pilot_freq = pilot_freq + 1000;
         return pilot_freq;
         break;

         case 4:
         deviations = deviations + 500;
         return deviations;
         break;

         case 5:
         tx_line_in_lev = tx_line_in_lev + 100;
         return tx_line_in_lev;
         break;
      }
   }

   else
   {
      switch(func)
      {
         case 1:
         frequency = frequency + 5;
         return frequency;
         break;

         case 2:
         power++;
         return power;
         break;

         case 3:
         pilot_freq = pilot_freq++;
         return pilot_freq;
         break;

         case 4:
         deviations = deviations++;
         return deviations;
         break;

         case 5:
         tx_line_in_lev = tx_line_in_lev++;
         return tx_line_in_lev;
         break;
      }
   }
   delay(100) ;
}
/**************************************************************************************************************
Function: int SET_VALUE_DEC(int func)

Description: Function decrements value of parameter which is choose by input number when left button is pressed
                Decrement step depends on the time of keep button pressed (value of l)
Parametres: func - number of setting parameter (0 - 5)
               1 - transmit frequency
               2 - output power
               3 - pilot frequency
               4 - audio and pilot deviation
               5 - line input level
               
Return: value of parameter
               
Example: SET_VALUE_DEC(4);
**************************************************************************************************************/
int SET_VALUE_DEC(int func)
{
   l++;

   if(frequency <= frequency_min)  
   {
      frequency = frequency_max;
      
      l = 0;
   }

   if(power <= power_min)  
   {
      power = power_max;
      
      l = 0;
   }

   if(pilot_freq <= pilot_freq_min) 
   {
      pilot_freq = pilot_freq_max;
      
      TX_PILOT_FREQUENCY_HIGHBYTE = 0x00;
      TX_PILOT_FREQUENCY_LOWBYTE = 0x00;
      
      l = 0;
   }
 
   if(deviations <= deviations_min)  
   {
      deviations = deviations_max;
      
      TX_PILOT_DEVIATION_HIGHBYTE = 0x00;
      TX_PILOT_DEVIATION_LOWBYTE = 0x00;
      
      TX_AUDIO_DEVIATION_HIGHBYTE = 0x00;
      TX_AUDIO_DEVIATION_LOWBYTE = 0x00;
   
      l = 0;            
   }
  
   if(tx_line_in_lev <= tx_line_in_lev_min)
   {
      tx_line_in_lev = tx_line_in_lev_max;//nahodit ako premennu 0x27C
      l = 0;            
   }
 
   if (l > 5 && l < 25)
   {
      switch(func)
      {
         
         case 1:
         frequency = frequency - 100;
         return frequency;
         break;
         
         case 2:
         power = power - 2;
         return power;
         break;
        
         case 3:
         pilot_freq = pilot_freq - 500;
         return pilot_freq;
         break;
        
         case 4:
         deviations = deviations - 50;
         return deviations;
         break;
         
         case 5:
         tx_line_in_lev = tx_line_in_lev - 10;
         return tx_line_in_lev;
         break;
      }
   }

   if (l >= 25)  
   {
      switch(func)
      {
         
         case 1:
         frequency = frequency - 500;
         return frequency;
         break;
         
         case 2:
         power = power - 5;
         return power;
         break;
         
         case 3:
         pilot_freq = pilot_freq - 1000;
         return pilot_freq;
         break;
         
         case 4:
         deviations = deviations - 500;
         return deviations;
         break;
         
         case 5:
         tx_line_in_lev = tx_line_in_lev - 100;
         return tx_line_in_lev;
         break;
      }
   }

   else
   {
      switch(func)
      {
         case 1:
         frequency = frequency - 5;
         return frequency;
         break;
  
         case 2:
         power--;
         return power;
         break;

         case 3:
         pilot_freq--;
         return pilot_freq;
         break;
        
         case 4:
         deviations--;
         return deviations;
         break;
       
         case 5:
         tx_line_in_lev--;
         return tx_line_in_lev;
         break;
      }
   }

   delay(100) ;
}
/**********************************************************************
Function: int LONG_PUSH()

Description: Function return values of two variables: a and z
Return: a - value, which indicate time of pressing "right arrow" button
        z - value, which indicate time of pressing "left arrow" button
         
Example: LONG_PUSH();
**********************************************************************/
int LONG_PUSH()
{

   if (buttonStateLEFT == HIGH)
   {
      while (buttonStateLEFT == HIGH)
      {
         buttonStateLEFT = digitalRead(buttonLEFT);
         
         delay(20);
         
         z++;

       }
       return z;
   }

   if (buttonStateRIGHT == HIGH)
   {
      while (buttonStateRIGHT == HIGH)
      {
         buttonStateRIGHT = digitalRead(buttonRIGHT);
        
         delay(20);
        
         a++;
      }
      return a;
   }
}  
/**************************************************************************************************************
Function: int EEPROM_ZAPIS(int addr, byte command_, byte arg_1, byte arg_2, byte arg_3, byte arg_4, byte arg_5)

Description: Function writes data to EEPROM memory
Parametres: addr - number, where function starts write data (0 - 2048)
               command_ - number of SI4713 command
               arg_1 - value of command arguments
               arg_2
               arg_3
               arg_4
               arg_5
         
Example: EEPROM_ZAPIS(0, SET_PROPERTY, 0x00, 0x21, 0x03, 0x00, 0x00);
**************************************************************************************************************/
void EEPROM_WRITE(int addr, byte command_, byte arg_1, byte arg_2, byte arg_3, byte arg_4, byte arg_5)  //Write commands and arguments to EEPROM
{  

   EEPROM.write(addr, command_);
   EEPROM.write(addr+1, arg_1);
   EEPROM.write(addr+2, arg_2);
   EEPROM.write(addr+3, arg_3);
   EEPROM.write(addr+4, arg_4);
   EEPROM.write(addr+5, arg_5);       
}

/*******************************************************************************
Function int EEPROM_READ(byte address)

Description: Function read data from EEPROM memory and send it to SI4713 via I2C
Parametres: address - number, where function starts read data (0 - 2048)           
Return: address_ - number, where the functions ends reading                          
     
Example: EEPROM_READ(101);
*******************************************************************************/

int EEPROM_READ(byte address)

{
   byte cmd = EEPROM.read(address);
   address++;

   byte argument1 = EEPROM.read(address);
   address++;
   
   byte argument2 = EEPROM.read(address);
   address++;
  
   byte argument3 = EEPROM.read(address);
   address++;
  
   byte argument4 = EEPROM.read(address);
   address++;
  
   byte argument5 = EEPROM.read(address);

   I2C_SEND(cmd, argument1, argument2, argument3,argument4, argument5);

   delay(500);
   
   address_ = address_ + 7;
  
   return address_;
}







