#include "Oslo_Demo_lib.h"
#include <Console.h>

//Setting PWM adress
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41);
Adafruit_PWMServoDriver pwm3 = Adafruit_PWMServoDriver(0x43);

//intitialize 7seg
TM1637Display seg(seg_clk, seg_data);

//intitialize bargraphs
Grove_LED_Bar bar1(bg1_clk, bg1_data, 1, LED_BAR_10);
Grove_LED_Bar bar2(bg2_clk, bg2_data, 1, LED_BAR_10);
Grove_LED_Bar bar3(bg3_clk, bg3_data, 1, LED_BAR_10);
Grove_LED_Bar bar4(bg4_clk, bg4_data, 1, LED_BAR_10);
Grove_LED_Bar bar5(bg5_clk, bg5_data, 1, LED_BAR_10);
Grove_LED_Bar bar6(bg6_clk, bg6_data, 1, LED_BAR_10);
Grove_LED_Bar bar7(bg7_clk, bg7_data, 1, LED_BAR_10);
Grove_LED_Bar bar8(bg8_clk, bg8_data, 1, LED_BAR_10);

//intitalize Led Panel
Adafruit_NeoPixel rgbWS = Adafruit_NeoPixel(numberLED, RGB_panel , NEO_GRB + NEO_KHZ800);

//initialize PCF
PCF8574 pcf1(0x38);
PCF8574 pcf2(0x39);


/*---------------------------------------- Global Variables -------------------------------------------------*/
int REL[] = {-1, 22, 24, 26, 28, 30, 32, 34, 36, 23, 25, 27, 29, 31, 33, 35, 37};

boolean V1_ord, V2_ord, V5_ord, V6_ord; 
boolean Alarm_1_ord, Alarm_2_ord, Alarm_3_ord, Alarm_4_ord, Alarm_5_ord, Alarm_6_ord, Alarm_7_ord, Alarm_8_ord;
boolean Heater_ord, Cooler_ord, Counter_clock_ord, Counter_up_down_ord, Counter_reset_ord, Buzzer_ord;
boolean Mixer_on_40_ord, Mixer_ord, Current_peak_on = 0;
boolean Cooler_on, Cooler_FT, Heater_on, Max_level, Min_level, Mixer_on, Mixer_power_on, Overflow, V1_opened, V1_closed, V2_opened, V2_closed, V5_opened, V5_closed, V6_opened, V6_closed ;
boolean Reset_counter, Inflow_leakage, Outflow_leakage, Fan_broken, Test_mixer, P1, P2, P3, P4, Magic_button;

int Temperature_PWM, Mixer_current_PWM, Inflow_PWM, Outflow_PWM, Level_PWM, Tachometer_PWM;
int  Counter_index = 0;
uint8_t seg_null[] = { 0x0, 0x0, 0x0, 0x0 };


enum LED {RGB1 = 1, RGB2, RGB3, RGB4, RGB5, RGB6, RGB7, RGB8, RGB9, RGB10, RGB11};

float Temperature = 20, Mixer_current = 0,  Inflow = 0, Outflow = 0,Level = 0, Tachometer = 0 ; //AO
float Mixer_ref = 100, V3_ref = 100, V4_ref = 100;  //AI
float Mixer_ref_map = 0, V3_ref_map = 0, V4_ref_map = 0;  //AI map
float Cooler_power = 10000, Heater_power = 10000;

float InflowPT = 0, OutflowPT = 0;
float  Current_noise = 0, Mixer_speed = 0, Mixer_speed_set = 0, Mixer_speed_PT = 0, Decrease_const = 0;
boolean TITD = 0, TDTD = 0, TITD_on = 0, TDTD_on = 0; //Temperature increase/derease time delay, 
int IDC = 0, DDC = 0; // Increase/decrease temperature delay counter

//configurable variables
float Bypass_maxflow = 0.4, Leakage = 30, Max_flow = 30, Tank = 200, Liquid_value = 0, Temperature_valves = 20, Temperature_tank = 0, Max_temperature = 180;
unsigned Task = 50;

String inputString = "";

/*------------------------------------------ Code ----------------------------------------------------------*/
void setup() 
{
  Serial.begin(9600);
  Serial3.begin(9600);
  initialize_all();
  pins_setup(); 
  Serial3.println("Do you want to configure simulator? y/n");
  int timeout = 5000, t = 0;
  while(t<timeout)
  {
    char c = Serial3.read(); 
    if(c == 'y')
    {
      Serial3.println("Simulator configuration started. Open configuration application.");
      simulatorConfiguration();
      break;
    }
    else if(c == 'n')    
    {
       Serial3.println("Simlator configuration timeout. Simulator default setup.");
       break;
    }
    else
    {
      t += 50;
      delay(50);
    }
  }
  
      
      Serial3.println(Bypass_maxflow);
      Serial3.println(Leakage);
      Serial3.println(Max_flow);
      Serial3.println(Tank);
      Serial3.println(Liquid_value);
      Serial3.println(Temperature_valves);
      Serial3.println(Temperature_tank);
      Serial3.println(Max_temperature);
      Serial3.println(Cooler_power);
      Serial3.println(Heater_power);
      Serial3.println(Task);
  
}

void loop()
{
//------------ Testing code --------------
//seg_test();
//bg_test();bg_test();
//relay_test();
//PWM_test();
//motor_fan_test();
//LED_panel_test();

//Serial3.println("Main loop");

readSignals();

Inflow = min(V1_ord * ((V3_ref/100) * Max_flow + V5_ord * Bypass_maxflow * Max_flow) + Inflow_leakage * Leakage, 30);
Outflow = min(V2_ord * ((V4_ref/100) * Max_flow + V6_ord * Bypass_maxflow * Max_flow) + Outflow_leakage * Leakage, 30);
InflowPT = Inflow/(3600/Task);
OutflowPT = Outflow/(3600/Task);
Liquid_value = constrain(Liquid_value + InflowPT - OutflowPT, 0, Tank * 1.02);
Level = min((Liquid_value/Tank)*100, 102);

Temperature_tank = Temperature;
Temperature_tank = Temperature_tank + ((Heater_ord * (TemperaturePT/Liquid_value)) - (Cooler_ord * (TemperaturePT/Liquid_value)) + (TITD * (TemperaturePT/Liquid_value)) - (TDTD * (TemperaturePT/Liquid_value)));
if((InflowPT + Liquid_value) == 0)
  Temperature = 20;
else
  Temperature = (((InflowPT * Temperature_valves) + (Liquid_value * Temperature_tank)) / (InflowPT + Liquid_value));
Temperature = constrain(Temperature, 0, Max_temperature);

Mixer_speed_set = Mixer_ord * map(Mixer_ref, 0, 100, 0, Max_speed);
Mixer_speed_PT = Task * 4;
Decrease_const = float(map(Mixer_ref, 0, 100, 40, 300))/100.0;
if(Mixer_speed <= Mixer_speed_set)
{
  Mixer_speed = constrain(Mixer_speed + Mixer_speed_PT, 0, Mixer_speed_set);  //speed increase
    if(Mixer_speed < (Mixer_speed_set/4))
      Current_peak_on = 1;
    else
      Current_peak_on = 0;
}
else
  Mixer_speed = max(0, Mixer_speed - (Mixer_speed_PT * Decrease_const));    //speed decrease
Tachometer = map(Mixer_speed, 0, Max_speed, 100, 255);
Current_noise = float(random(0,100))/100.0;
Mixer_current = Mixer_ord * (Mixer_min_current + Level * 0.1 + Mixer_ref * 0.1 + Current_noise + Current_peak_on * Current_peak);

Min_level = (Level < 1.5);
Max_level = (Level >= 100);
Overflow = (Level > 100) & (Level <= 102);    //level switches

writeSignals();
delay(Task);
}

/*-------------------------------------------- Functions ----------------------------------------------------*/

int digitalRead2(int pin)
{
  if (analogRead(pin) > 90)
    return HIGH;
  return LOW;
}

void writeSignals()
{
  //Relays
  digitalWrite(REL[1], !Cooler_ord);
  digitalWrite(REL[2], !Mixer_ord);
  digitalWrite(REL[3], !Mixer_ord);
  digitalWrite(REL[4], !Heater_ord);
  digitalWrite(REL[5], !Cooler_ord);
  digitalWrite(REL[6], !V1_ord);
  digitalWrite(REL[7], V5_ord);
  digitalWrite(REL[8], !V2_ord);
  digitalWrite(REL[9], V6_ord);
  digitalWrite(REL[10], !Min_level);
  digitalWrite(REL[11], V1_ord);
  digitalWrite(REL[12], !V5_ord);
  digitalWrite(REL[13], V2_ord);
  digitalWrite(REL[14], !V6_ord);
  digitalWrite(REL[15], !Overflow);
  digitalWrite(REL[16], !Max_level); 

  //RGB LEDs
  writeRGB(RGB1);
  writeRGB(RGB2);
  writeRGB(RGB3);
  writeRGB(RGB4);
  writeRGB(RGB5);
  writeRGB(RGB6);
  writeRGB(RGB7);
  writeRGB(RGB8);
  writeRGB(RGB9);
  writeRGB(RGB10);
  writeRGB(RGB11);

  //Bargraphs
  bar1.setLevel((Mixer_current/Max_current) * 10);
  bar2.setLevel((Mixer_ref/100) * 10);
  bar3.setLevel((Mixer_speed/Max_speed) * 10);
  bar4.setLevel((V3_ref/100) * 10);
  bar5.setLevel((Inflow/Max_flow) * 10);
  bar6.setLevel((Temperature/Max_temperature) * 10);
  bar7.setLevel((V4_ref/100) * 10);
  bar8.setLevel((Outflow/Max_flow) * 10);

  //Alarm LED panel
  rgbWS.begin();
  int brightness = 40;
  setRGB(brightness * Alarm_1_ord, 0  , 0  , 1);
  setRGB(brightness * Alarm_2_ord, 0  , 0  , 2);
  setRGB(brightness * Alarm_3_ord, 0  , 0  , 3);
  setRGB(brightness * Alarm_4_ord, 0  , 0  , 4);
  setRGB(brightness * Alarm_5_ord, 0  , 0  , 5);
  setRGB(brightness * Alarm_6_ord, 0  , 0  , 6);
  setRGB(brightness * Alarm_7_ord, 0  , 0  , 7);
  setRGB(brightness * Alarm_8_ord, 0  , 0  , 8);

  //Analog signals
  analogWrite(mixer, Mixer_ord * Tachometer);
  analogWrite(fan, Cooler_ord * 255);
  
  //PWMs
  Temperature_PWM = map(Temperature, 0, Max_temperature, 0, 4095);
  Temperature_PWM = constrain(Temperature_PWM, 0, 4095);
  pwm3.setPWM(PWM1, 0, Temperature_PWM);

  Mixer_current_PWM = map(Mixer_current, 0, Max_current, 0, 4095);
  Mixer_current_PWM = constrain(Mixer_current_PWM, 0, 4095);
  pwm3.setPWM(PWM2, 0, Mixer_current_PWM);

  Inflow_PWM = map(Inflow, 0, Max_flow, 0, 4095);
  Inflow_PWM = constrain(Inflow_PWM, 0, 4095);
  pwm3.setPWM(PWM3, 0, Inflow_PWM);

  Outflow_PWM = map(Outflow, 0, Max_flow, 0, 4095);
  Outflow_PWM = constrain(Outflow_PWM, 0, 4095);
  pwm3.setPWM(PWM4, 0, Outflow_PWM);

  Level_PWM = map(Level, 0, 100, 0, 4095);
  Level_PWM = constrain(Level_PWM, 0, 4095);
  pwm3.setPWM(PWM5, 0, Level_PWM);
  pwm3.setPWM(PWM7, 0, Level_PWM);  //BG

  Tachometer_PWM = map(Tachometer, 75, 255, 0, 4095);
  Tachometer_PWM = constrain(Tachometer_PWM, 0, 4095);
  pwm3.setPWM(PWM6, 0, Tachometer_PWM);

  //Buzzer
  if(Buzzer_ord)
    tone(buzzer, 500, 1);
  else
    digitalWrite(buzzer, HIGH);

  seg.setBrightness(0x0f);
  if(Counter_reset_ord)
    Counter_index = 0;
    
  if(Counter_up_down_ord && Counter_clock_ord)
    Counter_index++;
    
  if(!Counter_up_down_ord && Counter_clock_ord)
    Counter_index--;
   seg.showNumberDec(Counter_index, false, 4, 0);
 
}

void readSignals()
{
  //VC1
  V1_ord = !digitalRead2(V1) || pcf1.read(4);
  V5_ord = !digitalRead2(V5);
  V2_ord = !digitalRead2(V2);
  V6_ord = !digitalRead2(V6) || pcf1.read(5);
  
  //VC2
  Counter_clock_ord = digitalRead2(Counter_clock) || pcf1.read(7);
  Counter_up_down_ord = digitalRead2(Counter_up_down) || pcf1.read(6);
  Counter_reset_ord = digitalRead2(Counter_reset) || digitalRead(btn1);
  Alarm_1_ord = digitalRead2(Alarm_1);
  Alarm_3_ord = digitalRead2(Alarm_3);
  Alarm_5_ord = digitalRead2(Alarm_5);
  Alarm_7_ord = digitalRead2(Alarm_7);
  Heater_ord = digitalRead2(Heater_on_pin) || pcf1.read(3);
  
  //VC3
  Cooler_ord = pcf2.read(0) || Fan_broken;
  Alarm_2_ord = pcf2.read(1);
  Alarm_4_ord = pcf2.read(2);
  Alarm_6_ord = pcf2.read(3);
  Alarm_8_ord = pcf2.read(4);
  Buzzer_ord = pcf2.read(5);
  Mixer_on_40_ord = pcf2.read(6);
  Mixer_ord = pcf2.read(7) || Test_mixer;

  //Buttons
  Inflow_leakage = digitalRead(btn2);
  Outflow_leakage = pcf1.read(0);
  Fan_broken = pcf1.read(1);
  Test_mixer = pcf1.read(2);
  P1 = pcf1.read(3);
  P2 = pcf1.read(4);
  P3 = pcf1.read(5);
  P4 = pcf1.read(6);
  Magic_button = pcf1.read(7);  

  //Current (Analog) converters - CC
  Mixer_ref_map = analogRead(A0);
  Mixer_ref = map(Mixer_ref_map, 0, 1023, 0, 100);
  V3_ref_map = analogRead(A1);
  V3_ref = map(V3_ref_map, 0, 1023, 0, 100);
  V4_ref_map = analogRead(A2);
  V4_ref = map(V4_ref_map, 0, 1023, 0, 100);

  //Temperature time delay
  if(Heater_ord)
    {IDC++; TITD_on = 0;}
  else
    {IDC -= TD_speed; IDC = constrain(IDC, 0, (TD_speed*Max_TTD)/Task); TITD_on = 1;}   

    TITD = TITD_on * (IDC > 0);  
  
  if(Cooler_ord)
    {DDC++; TDTD_on = 0;}
  else
    {DDC -= TD_speed; DDC = constrain(DDC, 0, (TD_speed*Max_TTD)/Task); TDTD_on = 1;}

    TDTD = TDTD_on * (DDC > 0);

}




void writeRGB(int led)
{
  switch(led)
  {
    case(RGB1):
    {
      if(Mixer_ord)
      {
        pwm1.setPWM(RGB1_g, 0, Valve_opened_g);
        pwm1.setPWM(RGB1_r, 0, Valve_opened_r);
      }
      else
      {
        pwm1.setPWM(RGB1_g, 0, 0);
        pwm1.setPWM(RGB1_r, 0, 0);
      }
    }
    break;

    case(RGB2):
    {
      if(Mixer_ord)
      {
        pwm1.setPWM(RGB2_g, 0, Valve_opened_g);
        pwm1.setPWM(RGB2_r, 0, Valve_opened_r);
      }
      else
      {
        pwm1.setPWM(RGB2_g, 0, 0);
        pwm1.setPWM(RGB2_r, 0, 0);
      }
    }
    break;

    case(RGB3):
    {
      if(V1_ord)
      {
        pwm1.setPWM(RGB3_g, 0, Valve_opened_g);
        pwm1.setPWM(RGB3_r, 0, Valve_opened_r);
      }
      else
      {
        pwm1.setPWM(RGB3_g, 0, Valve_closed_g);
        pwm1.setPWM(RGB3_r, 0, Valve_closed_r);
      }
    }
    break;


    case(RGB4):
    {
      if(V5_ord)
      {
        pwm1.setPWM(RGB4_g, 0, Valve_opened_g);
        pwm1.setPWM(RGB4_r, 0, Valve_opened_r);
      }
      else
      {
        pwm1.setPWM(RGB4_g, 0, Valve_closed_g);
        pwm1.setPWM(RGB4_r, 0, Valve_closed_r);
      }
    }
    break;

    case(RGB5):
    {
      if((Liquid_value/Tank)*100 >= 102)
      {
        pwm2.setPWM(RGB5_g, 0, Valve_opened_g);
        pwm2.setPWM(RGB5_r, 0, Valve_opened_r);
      }
      else
      {
        pwm2.setPWM(RGB5_g, 0, 0);
        pwm2.setPWM(RGB5_r, 0, 0);
      }
    }
    break;

    case(RGB6):
    {
      if((Liquid_value/Tank)*100 >= 100)
      {
        pwm2.setPWM(RGB6_g, 0, Valve_opened_g);
        pwm2.setPWM(RGB6_r, 0, Valve_opened_r);
      }
      else
      {
        pwm2.setPWM(RGB6_g, 0, 0);
        pwm2.setPWM(RGB6_r, 0, 0);
      }
    }
    break;

    case(RGB7):
    {
      if((Liquid_value/Tank)*100 < 5)
      {
        pwm2.setPWM(RGB7_g, 0, Valve_opened_g);
        pwm2.setPWM(RGB7_r, 0, Valve_opened_r);
      }
      else
      {
        pwm2.setPWM(RGB7_g, 0, 0);
        pwm2.setPWM(RGB7_r, 0, 0);
      }
    }
    break;

    case(RGB8):
    {
      if(Cooler_ord)
      {
        pwm2.setPWM(RGB8_g, 0, Valve_opened_g);
        pwm2.setPWM(RGB8_r, 0, Valve_opened_r);
      }
      else
      {
        pwm2.setPWM(RGB8_g, 0, 0);
        pwm2.setPWM(RGB8_r, 0, 0);
      }
    }
    break;

    case(RGB9):
    {
      if(Heater_ord)
      {
        pwm2.setPWM(RGB9_g, 0, Valve_opened_g);
        pwm2.setPWM(RGB9_r, 0, Valve_opened_r);
      }
      else
      {
        pwm2.setPWM(RGB9_g, 0, 0);
        pwm2.setPWM(RGB9_r, 0, 0);
      }
    }
    break;
    
    case(RGB10):
    {
      if(V6_ord)
      {
        pwm3.setPWM(RGB10_g, 0, Valve_opened_g);
        pwm3.setPWM(RGB10_r, 0, Valve_opened_r);
      }
      else
      {
        pwm3.setPWM(RGB10_g, 0, Valve_closed_g);
        pwm3.setPWM(RGB10_r, 0, Valve_closed_r);
      }
    }
    break;

    case(RGB11):
    {
      if(V2_ord)
      {
        pwm3.setPWM(RGB11_g, 0, Valve_opened_g);
        pwm3.setPWM(RGB11_r, 0, Valve_opened_r);
      }
      else
      {
        pwm3.setPWM(RGB11_g, 0, Valve_closed_g);
        pwm3.setPWM(RGB11_r, 0, Valve_closed_r);
      }
    }
    break;

    default:
    break;
  }
}

void initialize_all()
{
  //PWMs
  pwm1.begin();
  pwm2.begin();
  pwm3.begin();
  pwm1.setPWMFreq(490);
  pwm2.setPWMFreq(490);
  pwm3.setPWMFreq(490);
  
  for(int i = 0; i < 16; ++i)
  {
    pwm1.setPWM(i, 0, 0);
    pwm2.setPWM(i, 0, 0);
    pwm3.setPWM(i, 0, 0);
  }

  //PCF
  pcf1.begin();
  pcf2.begin();
  
  //Bargraphs
  bar1.begin();
  bar2.begin();
  bar3.begin();
  bar4.begin();
  bar5.begin();
  bar6.begin();
  bar7.begin();
  bar8.begin();
  bar1.setLevel(0);
  bar2.setLevel(0);
  bar3.setLevel(0);
  bar4.setLevel(0);
  bar5.setLevel(0);
  bar6.setLevel(0);
  bar7.setLevel(0);
  bar8.setLevel(0);

  //LED Panel
  rgbWS.begin();
  for(int i=1; i<9; ++i)
  {
    setRGB(0 , 0  , 0  , i);
  }

  //7Seg
  seg.setSegments(0);
}

void pins_setup()
{  
  //buttons
  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  
  //buzzer
  pinMode(buzzer, OUTPUT);
  pinMode(buzzerGND, OUTPUT);
  digitalWrite(buzzer, HIGH);
  digitalWrite(buzzerGND, LOW);

  for(int i=1; i<17; ++i)
  {
    pinMode(REL[i], OUTPUT);
    digitalWrite(REL[i], HIGH); 
  }
}






/*-------------------------------------------- TESTS ------------------------------------------------*/

void seg_test()
{
  seg.setBrightness(0x0f);
  uint8_t seg_null[] = { 0x0, 0x0, 0x0, 0x0 };
  seg.setSegments(seg_null);
   for(int i = 0, digits = 1, pos = 3; i < 11; ++i)
  {
    if(i == 10 || i == 100 || i == 1000)
      {
        digits++; pos--;
      }
    seg.showNumberDec(i, false, digits ,pos);   //number, false, number of digits, position [0][1][2][3]
    delay(500);
  }  
  seg.setSegments(seg_null);
}

void motor_fan_test()
{
  //motor
   for(int i=0; i<18; ++i)
  {
    analogWrite(mixer, 75+10*i);
    delay(300);
  }
  analogWrite(mixer, 0);
  
  //fan
  analogWrite(fan, 255);
  delay(3000);
  analogWrite(fan, 0);
  
  //buzzer
  tone(buzzer, 500, 500); //pin, freq, delay
  delay(500);
  digitalWrite(buzzer, HIGH);
}

void relay_test()
{
 for(int i = 0; i < 16; ++i)
  {
  digitalWrite(REL[i], LOW);
  delay(500);
  digitalWrite(REL[i], HIGH);
  delay(500);
  }   
}
void bg_test()
{
  // Walk through the levels - small bg
  for (int i = 0; i <= 10; i++)
  {
    bar1.setLevel(i);
    bar2.setLevel(i);
    bar3.setLevel(i);
    bar4.setLevel(i);
    bar5.setLevel(i);
    bar6.setLevel(i);
    bar7.setLevel(i);
    bar8.setLevel(i);
  //  pwm2.setPWM(big_bg, 0, 409*i);
    delay(100);
  }
    bar1.setLevel(0);
    bar2.setLevel(0);
    bar3.setLevel(0);
    bar4.setLevel(0);
    bar5.setLevel(0);
    bar6.setLevel(0);
    bar7.setLevel(0);
    bar8.setLevel(0);
   // pwm2.setPWM(big_bg, 0, 0);
}

void setRGB (byte r, byte g, byte b, int number) 
{
  uint32_t color;
  color = rgbWS.Color(r, g, b);
  rgbWS.setPixelColor(number - 1, color);
  rgbWS.show();
}

void LED_panel_test()
{
  rgbWS.begin();
   for(int i=1; i<9; ++i)
  {
    setRGB(40  , 0  , 0  , i);
    delay(200);
  }
  for(int i=1; i<9; ++i)
  {
    setRGB(0 , 0  , 0  , i);
    delay(200);
  }
}



void simulatorConfiguration()
{
  while(1)
  {
    if (Serial.available() > 0)
    {
      // read the incoming byte:
      char c = Serial.read();
      inputString += c;
      if(c == ']')
      {
      // say what you got:
      Serial3.println("I received: ");
      Serial3.println(inputString);

      if(inputString.equals("[DISSCONECTED]"))
      {
        inputString = "";
        break;
      }

      else if(inputString.equals("[PING]"))
      {
        Serial.write("[PONG]");
        inputString = "";
      }
      else
      {

        int delimiter1 = 0;
        int delimiter2 = 0;
        int n = 11;
        float valArray[n];
        for(int i=0; i<n; ++i)
        {
          if(i==0)
            {delimiter1 = inputString.indexOf('['); delimiter2 = inputString.indexOf(';', i);}
          else if(i == n-1)
            {delimiter1 = delimiter2; delimiter2 = inputString.indexOf(']');}
          else
             {delimiter1 = delimiter2; delimiter2 = inputString.indexOf(';',delimiter1+1);}
             
          String valString = inputString.substring(delimiter1+1,delimiter2);
          valArray[i] = valString.toFloat();
        }
       
  
        Bypass_maxflow = valArray[0]/100;
        Leakage = valArray[1];
        Max_flow = valArray[2];
        Tank = valArray[3];
        Liquid_value = valArray[4];
        Temperature_valves = valArray[5];
        Temperature_tank = valArray [6];
        Max_temperature = valArray [7];
        Cooler_power = valArray[8];
        Heater_power = valArray[9];
        Task = (unsigned) valArray[10];
  
        inputString = "";
      }
      }
    }
    
  }
}
