#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);

#include <Servo.h>
Servo myservo1;
Servo myservo2;

String text;
int pos = 0 ;
int led_Recycle = 12; //ไฟสถานะตรงขยะ Recycle
int led_Normal = 13; //ไฟสถานะตรงขยะ ทั่วไป

bool chk_idel = true ;

void setup() {
  lcd.begin();
  lcd.setCursor(0,0);lcd.print  (" Welcome to Bin AI ");
  lcd.setCursor(0,1);lcd.print  ("    168 training   ");
  
  Serial.begin(115200);
  Serial.setTimeout(100); 

  pinMode(led_Recycle,OUTPUT); digitalWrite(led_Recycle, LOW);
  pinMode(led_Normal,OUTPUT); digitalWrite(led_Normal, LOW);

  myservo1.attach(9);
  myservo2.attach(10);

  myservo1.write(90);
  myservo2.write(95);

  lcd.setCursor(0,1);lcd.print ("                    ");
  lcd.setCursor(0,0);lcd.print ("                    ");
  lcd.setCursor(0,0);lcd.print ("  Please scan image ");
  lcd.setCursor(0,1);lcd.print ("<<<<<<<<  >>>>>>>>>>");
}

void loop() {


if (chk_idel == true){

  if (Serial.available() > 0) {
    text = Serial.readStringUntil('\n');
    
    if (text == "recycle"){ 
      lcd.setCursor(0,0);lcd.print ("                    ");
      lcd.setCursor(0,1);lcd.print ("                    ");
      lcd.setCursor(0,0);lcd.print (" waste separation =>");
      lcd.setCursor(0,1);lcd.print (text);
      digitalWrite(led_Recycle,HIGH);
      chk_idel = false;
//  ##################################  เปิดฝา - ขวา ###################################  //  
      for(pos = 90; pos >= 0; pos --){
        myservo1.write(pos); 
        delay(20); 
      } 
      delay(3000); 
      for(pos = 90; pos >= 45; pos --){
        myservo2.write(pos); 
        delay(20);
      } 
      delay(3000);

//  ##################################  ปิดฝา - ขวา ###################################  //     
      
      for(pos = 45; pos <= 95; pos ++){
          myservo2.write(pos); 
          delay(20);
      }       
      for(pos = 2; pos <= 90; pos ++){
        myservo1.write(pos); 
        delay(20);
      }
      digitalWrite(led_Recycle, LOW);

      chk_idel = true;
      Serial.flush();
      Serial.end();
      delay(1000);
      Serial.begin(115200);
    }


    if (text == "normal" ){
      lcd.setCursor(0,0);lcd.print ("                    ");
      lcd.setCursor(0,1);lcd.print ("                    ");
      lcd.setCursor(0,0);lcd.print (" waste separation =>");
      lcd.setCursor(0,1);lcd.print (text);
      digitalWrite(led_Normal,HIGH);

      chk_idel = false;
    //##################################  เปิดฝา - ซ้าย ###################################  //  
      for(pos = 90; pos >= 2; pos --){
        myservo1.write(pos); 
        delay(20);
      } 
      delay(3000);

      for(pos = 95; pos <= 135; pos ++){
        myservo2.write(pos); 
        delay(20);
      } 
      delay(3000);

    //  ##################################  ปิดฝา - ซ้าย ###################################  // 
      for(pos = 135; pos >= 95; pos --){
        myservo2.write(pos); 
        delay(20);
      }
      digitalWrite(led_Normal,LOW); 
      for(pos = 2; pos <= 90; pos ++){
        myservo1.write(pos); 
        delay(20);
      } 
          
      chk_idel = true;

      Serial.flush();
      Serial.end();
      delay(1000);
      Serial.begin(115200);
    }

  }    

}else{
  chk_idel = false;
  lcd.setCursor(0,0);lcd.print ("                    ");
  lcd.setCursor(0,1);lcd.print ("                    ");
  lcd.setCursor(0,0);lcd.print ("  Please scan image ");
  lcd.setCursor(0,1);lcd.print ("<<<<<<<<  >>>>>>>>>>");
}  
    

  
}

  
