// TODO include senzor svetla a vzdalenosti, hot plug

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
#include <Encoder.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <APDS9930.h>

// -----------Enkoderove vstupy
Encoder encoder0(29, 30);
Encoder encoder1(25, 24);
Encoder encoder2(28, 27);
Encoder encoder3(2, 3);
Encoder encoder4(5, 4);
Bounce buttonEncoder0 = Bounce(31, 500);
Bounce buttonEncoder1 = Bounce(26, 1); 
Bounce buttonEncoder2 = Bounce(32, 1); 
Bounce buttonEncoder3 = Bounce(33, 1); 

#define outputA 29 //CLK
#define outputB 30 //DT
#define outputA1 25 //CLK
#define outputB1 24 //DT
#define outputA2 28 //CLK
#define outputB2 27 //DT
#define outputA3 3 //CLK
#define outputB3 2 //DT
#define outputA4 5 //CLK
#define outputB4 4 //DT

// -----------//Enkoderove vstupy

// -------------Promene a vstupy pro displej


#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
// -------------//Promene a vstupy pro displej

//---------------Promene a vstupy pro senzor vlhkosti a teploty BMP280

// nastavení adresy senzoru
#define BMP280_ADRESA (0x76)
// inicializace senzoru BMP z knihovny
Adafruit_BMP280 bmp;
// konstanta s korekcí měření v hPa
int korekce = 32;
//---------------///Promene a vstupy pro senzor vlhkosti a teploty 

//---------------Promene a vstupy pro senzor svetla a vzdalenosti APDS 9930

APDS9930 apds = APDS9930();
float osvetleni = 0;
uint16_t priblizeni = 0;
//---------------///Promene a vstupy pro senzor svetla a vzdalenosti APDS 9930


// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=387.66666412353516,361.6666660308838
AudioSynthWaveformSineModulated sine_fm1;       //xy=537.6667022705078,388.6666679382324
AudioSynthWaveformSine   sine1;          //xy=539.6667022705078,485.66666984558105
AudioSynthNoisePink      pink1;          //xy=577.6667022705078,528.6667022705078
AudioMixer4              mixer1;         //xy=706.6667098999023,383.6666679382324
AudioFilterStateVariable filter1;        //xy=899.6667137145996,636.6667060852051
AudioEffectReverb        reverb1;        //xy=908.6667098999023,449.66666984558105
AudioEffectDelay         delay1;         //xy=919.6667098999023,539.6667022705078
AudioMixer4              mixer4;         //xy=1021.6667175292969,642.6667060852051
AudioMixer4              mixer3;         //xy=1045.6666564941406,513.6667022705078
AudioMixer4              mixer2;         //xy=1231.6666641235352,471.6666679382324
AudioOutputI2S           i2s1;           //xy=1444.6666641235352,526.6666703224182
AudioConnection          patchCord1(waveform1, sine_fm1);
AudioConnection          patchCord2(waveform1, 0, mixer1, 0);
AudioConnection          patchCord3(sine_fm1, 0, mixer1, 1);
AudioConnection          patchCord4(sine1, 0, mixer1, 2);
AudioConnection          patchCord5(pink1, 0, mixer1, 3);
AudioConnection          patchCord6(mixer1, 0, mixer2, 0);
AudioConnection          patchCord7(mixer1, reverb1);
AudioConnection          patchCord8(mixer1, delay1);
AudioConnection          patchCord9(mixer1, 0, filter1, 0);
AudioConnection          patchCord10(filter1, 0, mixer4, 0);
AudioConnection          patchCord11(filter1, 1, mixer4, 1);
AudioConnection          patchCord12(filter1, 2, mixer4, 2);
AudioConnection          patchCord13(reverb1, 0, mixer2, 1);
AudioConnection          patchCord14(delay1, 0, mixer3, 0);
AudioConnection          patchCord15(delay1, 1, mixer3, 1);
AudioConnection          patchCord16(delay1, 2, mixer3, 2);
AudioConnection          patchCord17(delay1, 3, mixer3, 3);
AudioConnection          patchCord18(mixer4, 0, mixer2, 3);
AudioConnection          patchCord19(mixer3, 0, mixer2, 2);
AudioConnection          patchCord20(mixer2, 0, i2s1, 0);
AudioConnection          patchCord21(mixer2, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=1300.6666412353516,718.6667098999023
// GUItool: end automatically generated code





void setup() {
  Serial.begin(9600);
  
  // Inicailizace Audio parametru
  AudioMemory(20);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.32);
  mixer1.gain(0, 0.75);
  mixer1.gain(1, 0.0);
  mixer1.gain(2, 0.0);
  mixer1.gain(3, 0.0);
  mixer2.gain(0, 0.15);
  mixer2.gain(1, 0.0);
  mixer2.gain(2, 0.0);
  mixer2.gain(3, 0.0);
  waveform1.begin(WAVEFORM_SAWTOOTH);
  waveform1.amplitude(0.75);
  waveform1.frequency(50);
  waveform1.pulseWidth(0.15);
  sine_fm1.frequency(440);
  sine_fm1.amplitude(0.75);
  sine1.frequency(200);
  sine1.amplitude(0.75);
  pink1.amplitude(0.75);


 // ------------------- Nastaveni pro displej
  // Adresa displejove jednotky - 0x3C
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); 
  // init done
  
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  display.clearDisplay();


// ------------------- Inicalizace senzoru BMP 280
bmp.begin(BMP280_ADRESA);
// -------------------///Inicalizace senzoru BMP 280

// ------------------- Inicalizace senzoru APDS-9930
// apds.init();
// apds.enableLightSensor(false);
// apds.enableProximitySensor(false);
// -------------------///Inicalizace senzoru APDS-9930

// ------Promenne pro ovladaci prvky, tlacitka, potaky
// --------Enkodery tlacitka
  pinMode(26, INPUT_PULLUP);
  pinMode(31, INPUT_PULLUP);
  pinMode(32, INPUT_PULLUP);

}

int waveform_type = WAVEFORM_SAWTOOTH;
int mixer1_setting = 0;
int mixer2_setting = 0;
elapsedMillis timeout = 0;


//---------Menu promene
char *titles0[]  = {"1: Synthesis menu","2: Wave Menu","3: Sensor Menu", "4: Monitor input", "5: Map Menu", "6: Mixer Menu"};
int titles0Count = 6;

char *titles1[]  = {"Back","1: Modulator","2: FM synteza","3: Sin oscilator","4: Pink Noise"};
int titles1Count = 5;

char *titles2[]  = {"Back","1: Mod Sinus","2: Mod Obdelnik","3: Sin oscilator","4: Mod Pila","5: Mod Pulz"};
int titles2Count = 6;

char *titles3[]  = {"Back","1: BMP280", "2: APDS-9930", "3: TCS34725"};
int titles3Count = 4;

char *titles4[] = {"Back","1: Encoder 1", "2: Encoder 2", "3: Encoder 3", "4: Encoder 4", "5: Senzor1"};
int titles4Count = 6;

char *titles5[] = {"Back","1: Encoder 1", "2: Encoder 2", "3: Encoder 3", "4: Encoder 4", "5: Senzor", "6: Senzor 2"};
int titles5Count = 7;

char *titles6[] = {"Back","1: Mixer 1", "2: Mixer 2", "3: Mixer 3", "4: Mixer 4"};
int titles6Count = 5;

char *titles61[] = {"Back","1: OSC 1", "2: OSC 2", "3: OSC 3", "4: PNoise"};
int titles61Count = 5;

char *titles10[] = {"Back","Read Value: ", "Multiply: ", "Out value: "};
int titles10Count = 4;

char *titles11[] = {"Back","1: Car Freq ", "2: Mod Freq", "3: Reverb", "4: Filter" };
int titles11Count = 5;

char *titles12[] = {"Back","Max", "Min", "Multiply"};
int titles12Count = 4;




int stopRightMenu0 = titles0Count*2;
int stopRightMenu1 = titles1Count*2;
int stopRightMenu2 = titles2Count*2;
int stopRightMenu3 = titles3Count*2;
int stopRightMenu4 = titles4Count*2;
int stopRightMenu5 = titles5Count*2;
int stopRightMenu6 = titles6Count*2;
int stopRightMenu61 = titles61Count*2;
int stopRightMenu10 = titles10Count*2;
int stopRightMenu11 = titles11Count*2;
int stopRightMenu12 = titles12Count*2;
int menu = 0;
int inputNumber = 1;
float inputValue = 0; 

int senzorNumber;
int cursor = 0;
//---------//Menu promene

//---------Promene pro enkoder
long positionEncoder0 = 0;
long positionEncoder1  = 0;
long positionEncoder2 = 0;
long positionEncoder3 = 0;
long positionEncoder4 = 0;
int counterEncoder0 = 0;
int counterEncoder1 = 0;
int counterEncoder2 = 0;
int counterEncoder3 = 0;
int counterEncoder4 = 0;
int stopEncoder0 = stopRightMenu0;
int stopEncoder1 = 1000;
int stopEncoder2 = 1000;
int stopEncoder3 = 1000;
int stopEncoder4 = 1000;

int inMenu;
//----------Audio parametry
float frequency1 = 400;
float frequency2 = 100;
float reverbAmount = 0;

float mixer1level1 = 0;
float mixer1level2 = 0;
float mixer1level3 = 0;
float mixer1level4 = 0;
float mixer2level1 = 0;
float mixer2level2 = 0;
float mixer2level3 = 0;
float mixer2level4 = 0;
float mixer3level1 = 0;
float mixer3level2 = 0;
float mixer3level3 = 0;
float mixer3level4 = 0;



float multiplier1 = 2;
float multiplier2 = 2;
float multiplier3 = 2;
float multiplier4 = 2;
float multiplier5 = 5;
float multiplier6 = 5;
float inputMultiplier = 0;
float min1 = 0;
float min2 = 0;
float min3 = 0;
float min4 = 0;
float min5 = 0;
float min6 = 0;
float inputMin = 0;
float max1 = 10;
float max2 = 10;
float max3 = 10;
float max4 = 10;
float max5 = 10;
float max6 = 10;
float inputMax = 10;
int controlPar1 = 0; // Prvni cislo znaci parametr, druhe kontroler
int controlPar2 = 0; 
int controlPar3 = 0; 
int controlPar4 = 0; 
int controlPar5 = 0; 
int controlPar6 = 0; 
int mixerNum = 1;

//---------//Promene pro enkoder

// -------------------funkce displeje
void menupredloha(int cursor, char *itemName[], int noItems) {
  display.clearDisplay();
  display.setTextSize(1.3);
  display.setCursor(0,0);
  if (cursor == 0)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[0]);
  display.setCursor(0,10);
  if (cursor == 1)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[1]);
  display.setCursor(0,20);
  if (cursor == 2)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[2]);
  if (noItems > 3){
  display.setCursor(0,30);
  if (cursor == 3)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[3]);
  }
  if (noItems > 4){
  display.setCursor(0,40);
  if (cursor == 4)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[4]);
  }
  if (noItems > 5){
  display.setCursor(0,50);
  if (cursor == 5)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[5]);
  }
  if (noItems > 6 && cursor == 6){
  display.clearDisplay();
  display.setCursor(0,0);
  display.setTextColor(BLACK, WHITE);
  display.println(itemName[6]);
  }
  display.display();
}

void menuMonitoring(int cursor, char *itemName[], int noItems, float inputValue, float coef) {
  display.clearDisplay();
  display.setTextSize(1.3);
  display.setCursor(0,0);
  if (cursor == 0)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[0]);
  display.setCursor(0,10);
  if (cursor == 1)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.print(itemName[1]);
  display.setCursor(70,10);
  display.println(inputValue);
  if (cursor == 2)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.print(itemName[2]);
  display.setCursor(70,20);
  display.println(coef);

  display.setCursor(0,30);
  if (cursor == 3)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.print(itemName[3]);
  display.setCursor(70,30);
  display.println(coef*inputValue);
  if (noItems > 3){
  display.setCursor(0,40);
  if (cursor == 4)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[4]);
  }
  if (noItems > 4){
  display.setCursor(0,50);
  if (cursor == 5)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[5]);
  }
  display.display();
}

void menuMapping(int cursor, char *itemName[], int noItems, float maxInput, float minInput, float coef, char *parameterName) {
  display.clearDisplay();
  display.setTextSize(1.3);
  display.setCursor(0,0);
  if (cursor == 0)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[0]);
  display.setCursor(35,10);
  display.setTextColor(WHITE);
  display.println(parameterName);
  if (cursor == 1)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.setCursor(0,20);
  display.print(itemName[1]);
  display.setCursor(70,20);
  display.println(maxInput);

  display.setCursor(0,30);
  if (cursor == 2)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.print(itemName[2]);
  display.setCursor(70,30);
  display.println(minInput);
  if (noItems > 3){
  display.setCursor(0,40);
  if (cursor == 3)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[3]);
  display.setCursor(70,40);
  display.println(coef);
  }
  if (noItems > 4){
  display.setCursor(0,50);
  if (cursor == 4)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[4]);
  }
  display.display();
}


void menuSettings(int cursor, char *itemName[], int noItems, float value1, float value2, float value3, float value4, char *parameterName) {
  display.clearDisplay();
  display.setTextSize(1.3);
  display.setCursor(0,0);
  if (cursor == 0)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.println(itemName[0]);
  display.setCursor(35,10);
  display.setTextColor(WHITE);
  display.println(parameterName);
  if (cursor == 1)
  {
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.setCursor(0,20);
  display.print(itemName[1]);
  display.setCursor(70,20);
  display.println(value1);

  display.setCursor(0,30);
  if (cursor == 2){
  display.setTextColor(BLACK, WHITE);
  }
  else
  {
  display.setTextColor(WHITE);
  }
  display.print(itemName[2]);
  display.setCursor(70,30);
  display.println(value2);
  if (noItems > 3){
  display.setCursor(0,40);
    if (cursor == 3){
    display.setTextColor(BLACK, WHITE);
    }
    else{
    display.setTextColor(WHITE);
    }
    display.print(itemName[3]);
    display.setCursor(70,40);
    display.println(value3);
  }
  if (noItems > 4){
    display.setCursor(0,50);
    if (cursor == 4){
    display.setTextColor(BLACK, WHITE);
    }
    else{
    display.setTextColor(WHITE);
    }
    display.print(itemName[4]);
    display.setCursor(70,50);
    display.println(value4);
  }
  display.display();
}




void readValue(float inputValue,int cursor, int noItems, char *itemName[]) {
display.setTextSize(1.8);
display.setCursor(70,10);
display.setTextColor(WHITE);
display.println(inputValue);
display.display();
menupredloha(cursor, itemName, noItems);
}

// ------------------- Funkce senzoru vlhkosti a teploty BMP280
float readBMP280(){
  // načtení naměřené teploty ze senzoru
  float teplota = bmp.readTemperature();
  float tlak = (bmp.readPressure()/100.00) + korekce;
  // Serial.print("Teplota: ");
  // Serial.print(teplota);
  // Serial.println(" stupnu Celsia.");
  // Serial.print("Barometricky tlak: ");
  // Serial.print(tlak);
  // Serial.println(" hPa");
  // Serial.println();
  return teplota;
}
// ------------------- Funkce senzoru vlhkosti a teploty BMP280

// ------------------- Funkce senzoru vlhkosti a teploty ADPS9930
float readADPS9930(){
apds.readAmbientLightLux(osvetleni);
apds.readProximity(priblizeni);

return osvetleni;
}
// ------------------- //Funkce senzoru vlhkosti a teploty ADPS9930

// Pro Mapping menu
// Ovladani parametru
void chooseInput(int selectedInput){
  switch (selectedInput)
    {
    case 1:
      inputNumber = selectedInput;
      inputValue = counterEncoder1;
      inputMultiplier = multiplier1;
      inputMin = min1;
      inputMax = max1;
      break;
  
    case 2:
      inputNumber = selectedInput;
      inputValue = counterEncoder2;
      inputMultiplier = multiplier2;
      inputMin = min2;
      inputMax = max2;
      break;
    
    case 3:
      inputNumber = selectedInput;
      inputValue = counterEncoder3;
      inputMultiplier = multiplier3;
      inputMin = min3;
      inputMax = max3;
      break;

    case 4:
      inputNumber = selectedInput;
      inputValue = counterEncoder4;
      inputMultiplier = multiplier4;
      inputMin = min4;
      inputMax = max4;
      break;

    case 5:
      inputNumber = selectedInput;
      inputValue = readBMP280();
      inputMultiplier = multiplier5;
      inputMin = min5;
      inputMax = max5;
      break;

    case 6:
      inputNumber = selectedInput;
      inputValue =13;
      inputMultiplier = multiplier6;
      inputMin = min6;
      inputMax = max6;
      break;
    }
    Serial.println(inputValue);
}


void mapParameter1(int selectedInput){
  switch (selectedInput)
    {
    case 0:
      frequency1 = 192;
      min1 = 0;
      max1 = 10000;
      break;

    case 1:
      frequency1 = counterEncoder1*multiplier1;
      min1 = 0;
      max1 = 10000;
      break;
  
    case 2:
      frequency1 = counterEncoder2*multiplier2;
      min2 = 0;
      max2 = 10000;
      break;
    
    case 3:
      frequency1 = counterEncoder3*multiplier3;
      min3 = 0;
      max3 = 10000;
      break;

    case 4:
      frequency1 = counterEncoder4*multiplier4;
      min4 = 0;
      max4 = 10000;
      break;

    case 5:
      frequency1 = readBMP280()*multiplier5;
      min5 = 0;
      max5 = 10000;
      break;

    case 6:
      frequency1 = readADPS9930()*multiplier6;
      min6 = 0;
      max6 = 10000;
      break;
    }
}

void mapParameter2(int selectedInput){
  switch (selectedInput)
    {
    case 0:
      frequency2 = 192;
      min1 = 0;
      max1 = 10000;
      break;

    case 1:
      frequency2 = counterEncoder1*multiplier1;
      min1 = 0;
      max1 = 10000;
      break;
  
    case 2:
      frequency2 = counterEncoder2*multiplier2;
      min2 = 0;
      max2 = 10000;
      break;
    
    case 3:
      frequency2 = counterEncoder3*multiplier3;
      min3 = 0;
      max3 = 10000;
      break;

    case 4:
      frequency2 = counterEncoder4*multiplier4;
      min4 = 0;
      max4 = 10000;
      break;

    case 5:
      frequency2 = readBMP280()*multiplier5;
      min5 = 0;
      max5 = 10000;
      break;

    case 6:
      frequency1 = readADPS9930()*multiplier6;
      min6 = 0;
      max6 = 10000;
      break;      
    }
}

void mapParameter3(int selectedInput){
  switch (selectedInput)
    {
    case 0:
      reverbAmount = 0.6;
      min1 = 0;
      max1 = 10;
      break;

    case 1:
      min1 = 0;
      max1 = 10;
      stopEncoder1 = max1;
      multiplier1 = 1;
      reverbAmount = counterEncoder1*multiplier1;
      break;
  
    case 2:
      min2 = 0;
      max2 = 10;
      stopEncoder2 = max2;
      multiplier2 = 1;
      reverbAmount = counterEncoder2*multiplier2;
      break;
    
    case 3:
      min3 = 0;
      max3 = 10;
      stopEncoder3 = max3;
      multiplier2 = 1;
      reverbAmount = counterEncoder3*multiplier3;
      break;

    case 4:
      min4 = 0;
      max4 = 10;
      stopEncoder4 = max4;
      multiplier4 = 1;
      reverbAmount = counterEncoder4*multiplier4;
      break;

    case 5:
      reverbAmount = readBMP280()*multiplier5;
      min5 = 0;
      max5 = 10;
      break;
    
    case 6:
      reverbAmount = readBMP280()*multiplier6;
      min6 = 0;
      max6 = 10;
      break;
    }
}

void mapParameter4(int selectedInput){
  switch (selectedInput)
    {
    case 0:
      reverbAmount = 0.6;
      min1 = 0;
      max1 = 10;
      break;

    case 1:
      min1 = 0;
      max1 = 10;
      stopEncoder1 = max1;
      multiplier1 = 1;
      reverbAmount = counterEncoder1*multiplier1;
      break;
  
    case 2:
      min2 = 0;
      max2 = 10;
      stopEncoder2 = max2;
      multiplier2 = 1;
      reverbAmount = counterEncoder2*multiplier2;
      break;
    
    case 3:
      min3 = 0;
      max3 = 10;
      stopEncoder3 = max3;
      multiplier2 = 1;
      reverbAmount = counterEncoder3*multiplier3;
      break;

    case 4:
      min4 = 0;
      max4 = 10;
      stopEncoder4 = max4;
      multiplier4 = 1;
      reverbAmount = counterEncoder4*multiplier4;
      break;

    case 5:
      reverbAmount = readBMP280()*multiplier5;
      min5 = 0;
      max5 = 10;

      break;
    }
}

void setMixerGain(int mixerNum, int cursor, char *itemName[], int noItems, float value1, float value2, float value3, float value4, char *parameterName){
  // mixerNum OSC mixer <1 - 4>, Delay mixer <5 - 8>, Filter mixer <9 - 12>, Main Mixer <13 - 16>
  stopEncoder0 = 10;
  float gainLevel = counterEncoder0*0.1;
  switch (mixerNum)
    {
    case 1:
       mixer1.gain(0, gainLevel);
       menuSettings(cursor, itemName, noItems, gainLevel, value2, value3, value4, parameterName);
       mixer1level1 = gainLevel;
      break;

    case 2:
       mixer1.gain(1, gainLevel);
       menuSettings(cursor, itemName, noItems, value1, gainLevel, value3, value4, parameterName);
       mixer1level2 = gainLevel;
      break;
  
    case 3:
       mixer1.gain(2, gainLevel);
       menuSettings(cursor, itemName, noItems, value1, value2, gainLevel, value4, parameterName);
       mixer1level3 = gainLevel;
      break;
    
    case 4:
       mixer1.gain(3, gainLevel);
       menuSettings(cursor, itemName, noItems, value1, value2, value3, gainLevel, parameterName);
       mixer1level4 = gainLevel;
      break;
    }
}

void loop() {

// Volume 
float volumeKnob = analogRead(A1);
float volume = volumeKnob/1023.0;

//ENCODER
  long newEncoder0, newEncoder1, newEncoder2, newEncoder3, newEncoder4;

  newEncoder0 = encoder0.read();
  newEncoder1 = encoder1.read();
  newEncoder2 = encoder2.read();
  newEncoder3 = encoder3.read();
  newEncoder4 = encoder4.read();


  switch (inputNumber)
    {
    case 1:
      inputValue = counterEncoder1;
      inputMultiplier = multiplier1;
      inputMin = min1;
      inputMax = max1;
      break;
  
    case 2:
      inputValue = counterEncoder2;
      inputMultiplier = multiplier2;
      inputMin = min2;
      inputMax = max2;
      break;
    
    case 3:
      inputValue = counterEncoder3;
      inputMultiplier = multiplier3;
      inputMin = min3;
      inputMax = max3;
      break;

    case 4:
      inputValue = counterEncoder4;
      inputMultiplier = multiplier4;
      inputMin = min4;
      inputMax = max4;
      break;

    case 5:
      inputValue = readBMP280();
      inputMultiplier = multiplier5;
      inputMin = min5;
      inputMax = max5;
      break;

    case 6:
      inputValue =13;
      inputMultiplier = multiplier6;
      inputMin = min6;
      inputMax = max6;
      break;      
    }



//----ZVOLENI PROGRAMU TLACITKEM----
  buttonEncoder0.update();

  if (buttonEncoder0.fallingEdge() && menu == 0){ 
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("ZVOLEN Menu 1");
        menu = 1;
        stopEncoder0 = stopRightMenu1;
        counterEncoder0 = -1;
        menupredloha(0, titles1, titles1Count);
      } else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("ZVOLEN Menu 2");
        menu = 2;
        stopEncoder0 = stopRightMenu2;
        menupredloha(0, titles2, titles2Count);
        counterEncoder0 = -1;
      } else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("ZVOLEN Menu 3");
        menu = 3;
        stopEncoder0 = stopRightMenu3;
        menupredloha(0, titles3, titles3Count);
        counterEncoder0 = -1;
      }  
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("ZVOLEN Menu 4");
        menu = 4;
        stopEncoder0 = stopRightMenu4;
        menupredloha(0, titles4, titles4Count);
        counterEncoder0 = -1;
      } 
      else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
        Serial.println("ZVOLEN Menu 5");
        menu = 5;
        stopEncoder0 = stopRightMenu5;
        menupredloha(0, titles5, titles5Count);
        counterEncoder0 = -1;
      } 
      else if (counterEncoder0 >= 10 && counterEncoder0 < stopEncoder0) {
        Serial.println("ZVOLEN Menu 6");
        menu = 6;
        stopEncoder0 = stopRightMenu5;
        menupredloha(0,titles6, titles6Count);
        counterEncoder0 = -1;
      } 
      else{}
   }



// //Pro Menu = 10, cteni hodnoty
//   if (buttonEncoder0.fallingEdge() && menu == 10){ 
//       Serial.println("ZVOLEN Back");
//       menu = 0;
//       stopEncoder0 = stopRightMenu0;
//       menupredloha(0, titles0);
//   }


// Pro Menu = 1; metody syntezy
  if (buttonEncoder0.fallingEdge() && menu == 1){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    }
    if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      mixer1.gain(0, 0.75);
      mixer1.gain(1, 0.0);
      mixer1.gain(2, 0.0);
      mixer1.gain(3, 0.0);
      Serial.println("ZVOLEN Program 1: Ovladani modulátoru");
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      mixer1.gain(0, 0.0);
      mixer1.gain(1, 0.75);
      mixer1.gain(2, 0.0);
      mixer1.gain(3, 0.0);
      Serial.println("ZVOLEN Program 2: Ovladani FM syntezy");
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      mixer1.gain(0, 0.0);
      mixer1.gain(1, 0.0);
      mixer1.gain(2, 0.75);
      mixer1.gain(3, 0.0);
      Serial.println("ZVOLEN Program 3: Ovladani Sin oscilatoru");
    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < stopEncoder0) {
      mixer1.gain(0, 0.0);
      mixer1.gain(1, 0.0);
      mixer1.gain(2, 0.0);
      mixer1.gain(3, 0.75);
      Serial.println("ZVOLEN Program 4: Pink Noise");    
    } 
  }


// Pro Menu = 2; typ vlny
  if (buttonEncoder0.fallingEdge() && menu == 2) {
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    }
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.println("ZVOLEN Control waveform: Sine");
      waveform_type = WAVEFORM_SINE;
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.println("ZVOLEN Control waveform: Square");
      waveform_type = WAVEFORM_SQUARE;
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      Serial.println("ZVOLEN Control waveform: Triangle");
      waveform_type = WAVEFORM_TRIANGLE;
    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
      Serial.println("ZVOLEN Control waveform: Sawtooth");
      waveform_type = WAVEFORM_SAWTOOTH;
    } 
    else if (counterEncoder0 >= 10 && counterEncoder0 < stopEncoder0) {
      Serial.println("ZVOLEN Control waveform: Pulse");
      waveform_type = WAVEFORM_PULSE;
    } 
    waveform1.begin(waveform_type);
  }


// Pro Menu = 3; typ senzoru
  if (buttonEncoder0.fallingEdge() && menu == 3){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    } 
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.println("ZVOLEN init BMP280");
      bmp.begin(BMP280_ADRESA);
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 <6) {
      Serial.println("ZVOLEN init APDS-9930");
      apds.init();
      apds.enableLightSensor(false);
      apds.enableProximitySensor(false);
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      Serial.println("ZVOLEN reading Senzoru");
    } 
    else{}
  }
  
// Pro Menu = 4; monitorovani vstupnich zarizeni
  if (buttonEncoder0.fallingEdge() && menu == 4){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    } 
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.print("ZVOLEN encoder 1");
      Serial.println(titles4[1]);
      menu = 10;
      stopEncoder0 = stopRightMenu10;
      chooseInput(1);
      counterEncoder0 = -1;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.print("ZVOLEN encoder 2");
      Serial.println(titles4[2]);
      menu = 10;
      stopEncoder0 = stopRightMenu10;
      chooseInput(2);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      Serial.print("ZVOLEN encoder 3");
      Serial.println(titles4[2]);
      menu = 10;
      stopEncoder0 = stopRightMenu10;
      chooseInput(3);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
      Serial.print("ZVOLEN encoder 4");
      Serial.println(titles4[2]);
      menu = 10;
      stopEncoder0 = stopRightMenu10;
      chooseInput(4);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 10 && counterEncoder0 < stopEncoder0) {
      Serial.print("ZVOLEN senzor");
      Serial.println(titles4[3]);
      menu = 10;
      stopEncoder0 = stopRightMenu10;
      chooseInput(5);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280())
    } 
    else{}
  }

// Pro menu = 5 Mapovani vstupnich parametru
  if (buttonEncoder0.fallingEdge() && menu == 5){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    } 
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.print("ZVOLEN encoder 1");
      Serial.println(titles4[1]);
      menu = 11;
      stopEncoder0 = stopRightMenu11;
      chooseInput(1);
      counterEncoder0 = -1;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.print("ZVOLEN encoder 2");
      Serial.println(titles4[2]);
      menu = 11;
      stopEncoder0 = stopRightMenu11;
      chooseInput(2);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      Serial.print("ZVOLEN encoder 3");
      Serial.println(titles4[2]);
      menu = 11;
      stopEncoder0 = stopRightMenu11;
      chooseInput(3);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
      Serial.print("ZVOLEN encoder 4");
      Serial.println(titles4[2]);
      menu = 11;
      stopEncoder0 = stopRightMenu11;
      chooseInput(4);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 10 && counterEncoder0 < 12) {
      Serial.print("ZVOLEN senzor");
      Serial.println(titles4[3]);
      menu = 11;
      stopEncoder0 = stopRightMenu11;
      chooseInput(5);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280())
    } 
    else if (counterEncoder0 >= 12 && counterEncoder0 < stopEncoder0) {
      Serial.print("ZVOLEN senzor");
      Serial.println(titles4[3]);
      menu = 11;
      stopEncoder0 = stopRightMenu11;
      chooseInput(6);
      counterEncoder0 = -1;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280())
    } 
    else{}
  }

  if (menu == 611 && buttonEncoder0.fallingEdge() && inMenu==1){
  Serial.println("ZVOLEN Back");
  menu = 61;
  stopEncoder0 = stopRightMenu61;
  menuSettings(0, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
  inMenu = 0;
  }    

 if (buttonEncoder0.fallingEdge() && menu == 6) {
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    }
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.println("ZVOLEN ");
      mixerNum = 1;
      menu = 61;
      counterEncoder0 = -1;     
      menuSettings(cursor, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.println("ZVOLEN ");
      mixerNum = 2;
      menu = 61;
      counterEncoder0 = -1;      
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      Serial.println("ZVOLEN ");
      mixerNum = 3;
      menu = 61;
      counterEncoder0 = -1;      

    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
      Serial.println("ZVOLEN ");
      mixerNum = 4;
      menu = 61;
      counterEncoder0 = -1;      

    } 
  }

 if (buttonEncoder0.fallingEdge() && menu == 61) {
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 0;
      stopEncoder0 = stopRightMenu0;
      menupredloha(0, titles0, titles0Count);
    }
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.println("ZVOLEN ");
      mixerNum = 1;
      menu = 611;
      counterEncoder0 = -1;      
      inMenu = 1;
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.println("ZVOLEN ");
      mixerNum = 2;
      menu = 611;
      counterEncoder0 = -1; 
      inMenu = 1;     
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      Serial.println("ZVOLEN ");
      mixerNum = 3;
      menu = 611;
      counterEncoder0 = -1;    
      inMenu = 1;  

    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
      Serial.println("ZVOLEN ");
      mixerNum = 4;
      menu = 611;
      counterEncoder0 = -1;     
      inMenu = 1; 

    } 
  }

 

  //Pro Menu = 10, cteni hodnoty
  if (buttonEncoder0.fallingEdge() && menu == 10){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 4;
      stopEncoder0 = stopRightMenu4;
      menupredloha(0, titles4, titles4Count);
    } 
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.print("ZVOLEN Multiplier");
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280());
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280())
    } 
    // else if (counterEncoder0 >= 8 && counterEncoder0 < stopEncoder0) {
    //   menu = 11;
    //   stopEncoder0 = stopRightMenu11;
    //   Serial.println("ZVOLEN Map menu");
    //   menupredloha(0, titles11, titles11Count);
    // } 
    else{}
  }


//Pro Menu = 11, vyber parametru modulace
  if (buttonEncoder0.fallingEdge() && menu == 11){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 5;
      stopEncoder0 = stopRightMenu5;
    } 
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.print("ZVOLEN par 1 - ");
      menu = 12;
      stopEncoder0 = stopRightMenu12;
      controlPar1 = inputNumber;
      Serial.println(1);
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      frequency1 = inputValue * inputMultiplier;
      Serial.print("ZVOLEN par 2 - ");
      menu = 12;
      stopEncoder0 = stopRightMenu12;
      controlPar2 = inputNumber;
      Serial.println(1);
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
      Serial.println("ZVOLEN par 3");
      menu = 12;
      stopEncoder0 = stopRightMenu12;
      controlPar3 = inputNumber;
      // menu = 4;
      // stopEncoder0 = 4;
      // readValue(turnBMP280())
    } 
    else if (counterEncoder0 >= 8 && counterEncoder0 < stopEncoder0) {
      
    } 
    else{}
  }

//Pro Menu = 11, vyber parametru modulace
  if (buttonEncoder0.fallingEdge() && menu == 12){ 
    if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
      Serial.println("ZVOLEN Back");
      menu = 5;
      stopEncoder0 = stopRightMenu5;
      counterEncoder0 = -1;
    } 
    else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
      Serial.println("ZVOLEN ");
    } 
    else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
      Serial.println("ZVOLEN ");
    } 
    else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      Serial.println("ZVOLEN ");
    } 

    else{}
  }



//----//ZVOLENI PROGRAMU TLACITKEM----


  
//----LISTOVANI NABIDKOU----

  if (newEncoder1 != positionEncoder1) {
    if(newEncoder1 > positionEncoder1){
      counterEncoder1++;
    }
    else if (newEncoder1 < positionEncoder1){
      counterEncoder1--;
    }
    else{}

    if(counterEncoder1 > stopEncoder1){
      counterEncoder1 = stopEncoder1;
    }
    else if (counterEncoder1 < 0){
      counterEncoder1 = 0;
    }
    else{}

    Serial.print("Counter Encoder 1 = ");
    Serial.print(counterEncoder1);
    Serial.println();
    positionEncoder1 = newEncoder1;  

    if (menu == 10){  
      if (inputNumber == 1) {
        Serial.println("Encoder 1");
        menuMonitoring(0, titles10, titles10Count, inputValue, inputMultiplier);
      }
      else{}
    }
    Serial.println();
  }

  if (newEncoder2 != positionEncoder2) {
    if(newEncoder2 > positionEncoder2){
      counterEncoder2++;
    }
    else if (newEncoder2 < positionEncoder2){
      counterEncoder2--;
    }
    else{}

    if(counterEncoder2 > stopEncoder2){
      counterEncoder2 = stopEncoder2;
    }
    else if (counterEncoder2 < 0){
      counterEncoder2 = 0;
    }
    else{}

    Serial.print("Counter Encoder2 = ");
    Serial.print(counterEncoder2);
    Serial.println();
    positionEncoder2 = newEncoder2; 

  // Read encoderu 2
    if (menu == 10){  
      if (inputNumber == 2) {
        Serial.println("Encoder 2");
        menuMonitoring(0, titles10, titles10Count, inputValue, inputMultiplier);
      }
      else{}
    } 

  }

  if (newEncoder3 != positionEncoder3) {
    if(newEncoder3 > positionEncoder3){
      counterEncoder3++;
    }
    else if (newEncoder3 < positionEncoder3){
      counterEncoder3--;
    }
    else{}

    if(counterEncoder3 > stopEncoder3){
      counterEncoder3 = stopEncoder3;
    }
    else if (counterEncoder3 < 0){
      counterEncoder3 = 0;
    }
    else{}

    Serial.print("Counter Encoder 3 = ");
    Serial.print(counterEncoder3);
    Serial.println();
    positionEncoder3 = newEncoder3; 

    if (menu == 10){  
      if (inputNumber == 3) {
        Serial.println("Encoder 3");
        menuMonitoring(0, titles10, titles10Count, inputValue, inputMultiplier);
      }
      else{}
    }    
  }

  if (newEncoder4 != positionEncoder4) {
    if(newEncoder4 > positionEncoder4){
      counterEncoder4++;
    }
    else if (newEncoder4 < positionEncoder4){
      counterEncoder4--;
    }
    else{}

    if(counterEncoder4 > stopEncoder4){
      counterEncoder4 = stopEncoder4;
    }
    else if (counterEncoder4 < 0)
    {
      counterEncoder4 = 0;
    }
    else{}

    Serial.print("Counter Encoder 4 = ");
    Serial.print(counterEncoder4);
    Serial.println();
    positionEncoder4 = newEncoder4; 

    if (menu == 10){  
      if (inputNumber == 4) {
        Serial.println("Encoder 4");
        menuMonitoring(0, titles10, titles10Count, inputValue, inputMultiplier);
      }
      else{}
    }        
  }




  if (newEncoder0 != positionEncoder0){
    if(newEncoder0 > positionEncoder0){
      counterEncoder0--;
    }
    else if (newEncoder0 < 0){
      counterEncoder0++;
    }
    else{} 
    if(counterEncoder0 > stopEncoder0){
      counterEncoder0 = 0;
    }
    else if (counterEncoder0 < 0){
      counterEncoder0 = stopEncoder0;
    }
    else{}


    cursor = floor(counterEncoder0/2.0);   
    positionEncoder0 = newEncoder0;
    buttonEncoder0.update();

    Serial.print(counterEncoder0);
    Serial.print("   ");
    Serial.print(cursor);
    Serial.print("   ");
  // -------------------Menu menu
    if (menu == 0){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Synthesis menu");
        menupredloha(cursor, titles0, titles0Count);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Wave Menu");
        menupredloha(cursor, titles0, titles0Count);
      }  
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Senzor Menu");
        menupredloha(cursor, titles0, titles0Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Monitor menu");
        menupredloha(cursor, titles0, titles0Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      Serial.println("Monitor menu");
      menupredloha(cursor, titles0, titles0Count);
      } 
      else{}
    }
    

  
  // -------------------Synthesis menu
    if (menu == 1){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menupredloha(0, titles1, titles1Count);
      } else if (counterEncoder0 >= 2 && counterEncoder0 <4) {
        Serial.println("Program 1: Ovladani modulátoru");
        menupredloha(1, titles1, titles1Count);
      } else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Program 2: Ovladani FM syntezy");
        menupredloha(2, titles1, titles1Count);
      } else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Program 3: Ovladani Sin oscilatoru");
        menupredloha(3, titles1, titles1Count);
      } else if (counterEncoder0 >= 8 && counterEncoder0 < stopEncoder0) {
        Serial.println("Program 4: Pink Noise");
        menupredloha(4, titles1, titles1Count);
      }
      else{}
    }

  // ------------------- Wavetype menu
    if (menu == 2){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
          Serial.println("Back");
          menupredloha(0,titles2, titles2Count);
        } 
        else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
          Serial.println("Control waveform: Sine");
          menupredloha(1,titles2, titles2Count);
        } 
        else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
          Serial.println("Control waveform: Square");
          menupredloha(2,titles2, titles2Count);
        } 
        else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
          Serial.println("Control waveform: Triangle");
          menupredloha(3,titles2, titles2Count);
        } 
        else if (counterEncoder0 >= 8 && counterEncoder0 < 10) {
          Serial.println("Control waveform: Sawtooth");
          menupredloha(4,titles2, titles2Count);
        } 
        else if (counterEncoder0 >= 10 && counterEncoder0 < stopEncoder0) {
          Serial.println("Control waveform: Pulse");
          menupredloha(5,titles2, titles2Count);
        }
        else{}  
    }
  // ------------------- Senzor init menu
    if (menu == 3){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menupredloha(cursor,titles3, titles3Count);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("BMP20 read");
        menupredloha(cursor,titles3, titles3Count);
      } 
      else if (counterEncoder0 >= 4 && counterEncoder0 < stopEncoder0) {
        Serial.println("ADPS9930 read");
        menupredloha(cursor,titles3, titles3Count);
      } 
      else{}
    }
  // ------------------- Monitoring menu
    if (menu == 4){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menupredloha(0,titles4, titles4Count);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Encoder 1");
        menupredloha(1,titles4, titles4Count);
      } 
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Encoder 2");
        menupredloha(2,titles4, titles4Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Encoder 3");
        menupredloha(3,titles4, titles4Count);
      } 
      else if (8 >= 6 && counterEncoder0 < 10) {
        Serial.println("Encoder 4");
        menupredloha(4,titles4, titles4Count);
      } 
      else if (counterEncoder0 >= 10 && counterEncoder0 < stopEncoder0) {
        Serial.println("Senzor");
        menupredloha(5,titles4, titles4Count);
      } 
      else{}
    }
  // ------------------- Mapping menu
    if (menu == 5){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Encoder 1");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Encoder 2");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Encoder 3");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else if (8 >= 6 && counterEncoder0 < 10) {
        Serial.println("Encoder 4");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else if (counterEncoder0 >= 10 && counterEncoder0 < 12) {
        Serial.println("Senzor 1");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else if (counterEncoder0 >= 12 && counterEncoder0 < stopEncoder0) {
        Serial.println("Senzor 2");
        menupredloha(cursor,titles5, titles5Count);
      } 
      else{}
    }
  // ------------------- Mixing menu
    if (menu == 6){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menupredloha(cursor,titles6, titles6Count);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Mixer 1 - 1");
        menupredloha(cursor,titles6, titles6Count);
      }  
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Mixer 1 - 2");
        menupredloha(cursor,titles6, titles6Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Mixer 1 - 3");
        menupredloha(cursor,titles6, titles6Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      Serial.println("Mixer 1 - 4");
        menupredloha(cursor,titles6, titles6Count);
      } 
      else{}
    }
  // ------------------- Mixing choose mixer submenu
    if (menu == 61){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menuSettings(cursor, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Mixer 1 - 1");
        menuSettings(cursor, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
      }  
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Mixer 1 - 2");
        menuSettings(cursor, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Mixer 1 - 3");
        menuSettings(cursor, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      Serial.println("Mixer 1 - 4");
        menuSettings(cursor, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
      } 
      else{}
    }
  // ------------------- Mixing view values subsubmenu
    if (menu == 611){  
      setMixerGain(mixerNum, mixerNum, titles61, titles61Count, mixer1level1, mixer1level2, mixer1level3, mixer1level4, "MIXER 1");
    }
  // ------------------- Monitoring view element submenu
    if (menu == 10){  
      
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menuMonitoring(0, titles10, titles10Count, inputValue, inputMultiplier);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("value");
        menuMonitoring(1, titles10, titles10Count, inputValue, inputMultiplier);
      } 
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("multiply");
        menuMonitoring(2, titles10, titles10Count, inputValue, inputMultiplier);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
        Serial.println("out");
        menuMonitoring(3, titles10, titles10Count, inputValue, inputMultiplier);
      } 
      // else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
      //   Serial.println("Map");
      //   menuMonitoring(4, titles10, titles10Count, inputValue, inputMultiplier);
      // } 
      else{}
    }
  // ------------------- Mapping choose parameter submenu
    if (menu == 11){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menupredloha(cursor, titles11, titles11Count);
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Par 1");
        menupredloha(cursor, titles11, titles11Count);
      } 
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Par 2");
        menupredloha(cursor, titles11, titles11Count);
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < 8) {
        Serial.println("Par 3");
        menupredloha(cursor, titles11, titles11Count);
      } 
      else if (counterEncoder0 >= 8 && counterEncoder0 < stopEncoder0) {
        Serial.println("Par 4");
        menupredloha(cursor, titles11, titles11Count);
      } 
      else{}
    }
  // ------------------- Mapping monitoring values subsubmenu
    if (menu == 12){  
      if (counterEncoder0 >= 0 && counterEncoder0 < 2) {
        Serial.println("Back");
        menuMapping(0, titles12, titles12Count, inputMax, inputMin, inputMultiplier, "PARAMETR 1");
      } 
      else if (counterEncoder0 >= 2 && counterEncoder0 < 4) {
        Serial.println("Encoder 1");
        menuMapping(1, titles12, titles12Count, inputMax, inputMin, inputMultiplier, "PARAMETR 1");
      } 
      else if (counterEncoder0 >= 4 && counterEncoder0 < 6) {
        Serial.println("Encoder 2");
        menuMapping(2, titles12, titles12Count, inputMax, inputMin, inputMultiplier, "PARAMETR 1" );
      } 
      else if (counterEncoder0 >= 6 && counterEncoder0 < stopEncoder0) {
        Serial.println("Senzor");
        menuMapping(3, titles12, titles12Count, inputMax, inputMin, inputMultiplier, "PARAMETR 1" );
      } 
      else{}
    }
  }


// Tohle nejak nefacha
  if (menu == 10){  
    if (inputNumber == 5) {
      Serial.println("Senzor");
      menuMonitoring(cursor, titles10, titles10Count, inputValue, inputMultiplier);
    }
    else{}
  } 
  
  if (menu == 10 && buttonEncoder0.fallingEdge()){  
    if (inputNumber == 1) {
      Serial.println("Encoder 1");
      menuMonitoring(0, titles10, titles10Count, counterEncoder1, multiplier1);
    }
    else{}
  } 

  if (menu == 10 && buttonEncoder0.fallingEdge()){  
    if (inputNumber == 2) {
      Serial.println("Encoder 2");
      menuMonitoring(0, titles10, titles10Count, counterEncoder2, multiplier2);
    }
    else{}
  } 

  if (menu == 10 && buttonEncoder0.fallingEdge()){  
    if (inputNumber == 3) {
      Serial.println("Encoder 3");
      menuMonitoring(0, titles10, titles10Count, counterEncoder3, multiplier3);
    }
    else{}
  } 

  if (menu == 10 && buttonEncoder0.fallingEdge()){  
    if (inputNumber == 4) {
      Serial.println("Encoder 4");
      menuMonitoring(0, titles10, titles10Count, counterEncoder4, multiplier4);
    }
    else{}
  } 


  // if (menu == 4){ 
  //   if (counterEncoder0 > 0 && counterEncoder0 <= 2) {
  //   Serial.println("Back");
  //   readValue(turnBMP280());;
  //   } 
  
  //----LISTOVANI NABIDKOU----
//----//ENKODER----



  
  frequency2 = 300;
  mapParameter1(controlPar1);
  mapParameter2(controlPar2);
  mapParameter3(controlPar3);
  waveform1.frequency(frequency1);
  sine1.frequency(frequency1);
  sine_fm1.frequency(frequency2);
  reverb1.reverbTime(reverbAmount);

  sgtl5000_1.volume(volume);



  // Right button activates the envelope
  // if (buttonEncoder1.fallingEdge()) {
  //   mixer2.gain(0, 0.0);
  //   mixer2.gain(1, 1.0);
  //   mixer2_envelope = true;
  //   timeout = 0;
  //   envelope1.noteOn();
  // }
  // if (buttonEncoder1.risingEdge()) {
  //   envelope1.noteOff();
  //   timeout = 0;
  // }




  // after 4 seconds of inactivity, go back to
  // steady listening intead of the envelope
  // if (mixer2_envelope == true && timeout > 4000) {
  //   mixer2.gain(0, 0.15);
  //   mixer2.gain(1, 0.0);
  //   mixer2_envelope = false;
  // }

  // use the knobs to adjust parameters
  //float knob1 = (float)analogRead(A1) / 1023.0;
  // float knob2 = (float)analogRead(A2) / 1023.0;
  // float knob3 = (float)analogRead(A3) / 1023.0;



}


