#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int pBuzz = 2;
int BUTs[2] = {3, 4};
int LEDs[2] = {5, 6};
int POTs[2] = {A0, A1};
int SENs [2] = {8, 9};
int indSENs [2] = {10, 11};
int bomB = 13;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];       
int altura;
int largura;
int comprimento;
boolean newData = false;
int Volume;
int VolumeMAX;
int Nivel;
int Calc;

void setup(){
  lcd.begin(16,2);
  lcd.clear();
  pinMode(BUTs[0], INPUT_PULLUP);
  pinMode(BUTs[1], INPUT_PULLUP);
  pinMode(LEDs[0], OUTPUT);
  pinMode(LEDs[1], OUTPUT);
  digitalWrite(LEDs[0], HIGH);
  pinMode(SENs[0], INPUT_PULLUP);
  pinMode(SENs[1], INPUT_PULLUP);
  pinMode(indSENs[0], OUTPUT);
  pinMode(indSENs[1], OUTPUT);
  Serial.begin(9600);
  Serial.println("Escreva no estilo <24, 12, 6>  ");
  Serial.println();
}
void loop(){
  recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
        parseData();
        showParsedData();
        newData = false;
  }
  onOFF();
  voL();
  niV();
  alaR();
  senS();
}

void onOFF(){
  if(digitalRead(BUTs[0]) == 0){
    digitalWrite(LEDs[0], HIGH);
    digitalWrite(LEDs[1], LOW);
  }
  if(digitalRead(BUTs[1]) == 0){
    digitalWrite(LEDs[0], LOW);
    digitalWrite(LEDs[1], HIGH);
  }
  if(digitalRead(LEDs[1]) == 1){
    digitalWrite(bomB, HIGH);
  }
  if(digitalRead(LEDs[0]) == 1){
    digitalWrite(bomB, LOW);
  }
}

void voL(){
  lcd.setCursor(0, 0);
  lcd.print("Volume:");
  Volume = analogRead(POTs[0]);
  Volume = map(Volume, 0, 1023, 0, VolumeMAX);
  lcd.print(Volume);
  lcd.print("m3    ");
}

void niV(){
  lcd.setCursor(0, 1);
  lcd.print("Nivel:");
  Nivel = analogRead(POTs[1]);       
  Nivel = map(Nivel, 0, 1023, 0, 100);   
  lcd.print(Nivel);
  lcd.print("%      ");
}

void alaR(){
  Calc = (Nivel * VolumeMAX) / 100;
  if(Volume >= Calc){
   digitalWrite(LEDs[0], HIGH);
   digitalWrite(LEDs[1], LOW);
  }
}

void senS(){
  if(digitalRead(SENs[0]) == 0){
    digitalWrite(indSENs [0] , HIGH);
    while(digitalRead(SENs[0]) == 0){
      for(int buzz = 0; buzz < 7; buzz++){
        tone(pBuzz, 1000);
        delay(500);
        noTone(pBuzz);
        delay(500);
        if(buzz == 10){
          break;
        }
      }
    }
    digitalWrite(LEDs[0], HIGH);
    digitalWrite(LEDs[1], LOW);
  }
else{
    digitalWrite(indSENs [0] , LOW);
  }
  if(digitalRead(SENs[1]) == 0){
    digitalWrite(indSENs [1] , HIGH);
   while(digitalRead(SENs[1]) == 0){
        for(int buzz = 0; buzz < 7; buzz++){
        tone(pBuzz, 500);
        delay(500);
        noTone(pBuzz);
        delay(500);
        if(buzz == 10){
          break;
        }
      }
    }
    digitalWrite(LEDs[0], LOW);
    digitalWrite(LEDs[1], HIGH);
  }
else{
    digitalWrite(indSENs [1] , LOW);
  }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void parseData() {      
    char * strtokIndx; 
    strtokIndx = strtok(tempChars, ","); 
    altura = atoi(strtokIndx);    
    strtokIndx = strtok(NULL, ","); 
    largura = atoi(strtokIndx);     
    strtokIndx = strtok(NULL, ","); 
    comprimento = atoi(strtokIndx);     
}

void showParsedData() {
    Serial.print("altura ");
    Serial.println(altura);
    Serial.print("largura: ");
    Serial.println(largura);
    Serial.print("comprimento: ");
    Serial.println(comprimento);
    VolumeMAX = altura * largura * comprimento;
    Serial.print("Volume Total: ");
    Serial.println(VolumeMAX);
}