#include <Wire.h>
#include <LiquidCrystal_I2C.h>

int _mode ;
int _light ;
int _time ;
int _time2 ;
// For these LCD controls to work you MUST replace the standard LCD library from...
// https://github.com/marcoschwartz/LiquidCrystal_I2C
// Direct download https://github.com/marcoschwartz/LiquidCrystal_I2C/archive/master.zip
// Your project will not compile until this is done.
LiquidCrystal_I2C lcd_I2C_27(0x27,16,2); // set the LCD address for a 16 chars and 2 line display
int _currentlight ;

void setup(){  // put your setup code here, to run once:
  lcd_I2C_27.init (); // initialize the lcd
  lcd_I2C_27.backlight();
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  _mode = 0 ;

  _light = 0 ;

  _time = 0 ;

  _time2 = 0 ;

  lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
  lcd_I2C_27.print( "Hover sensor to" ); // Print a message to the LCD.

  lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
  lcd_I2C_27.print( "start" ); // Print a message to the LCD.

}

void loop(){  // put your main code here, to run repeatedly:
  _light = 0 ;
  if ( _mode == 0 ) {
    if (  analogRead( A0 ) < 700  &&  _light == 0  ) {
      Serial.print("detected"); //print message
      Serial.print(" "); //print a blank
      Serial.println();
      _mode = 1 ;
    }
    else  {
      Serial.print("undetected"); //print message
      Serial.print(" "); //print a blank
      Serial.println();
    }
  }
  if ( _mode == 1 ) {
    lcd_I2C_27.clear();
    lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
    lcd_I2C_27.print( "preheat the oven" ); // Print a message to the LCD.
    lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
    lcd_I2C_27.print( "to 170" ); // Print a message to the LCD.
    delay( 1000 ); // waits a few milliseconds 
    _mode = 2 ;
  }
  if ( _mode == 2 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "Melt the butter" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "Hover to start" ); // Print a message to the LCD.
      _mode = 3 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 3 ) {
    if ( _currentlight < 300 ) {
      _time = ( _time + 1 ) ;
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "Melt the butter" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( _time ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(4 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "sec" ); // Print a message to the LCD.
      if ( _time == 10 ) {
        lcd_I2C_27.clear();
        lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
        lcd_I2C_27.print( "time's up" ); // Print a message to the LCD.
        tone(11, 440, 2000); //buzzer or speaker frequency 
        _time = 0 ;
        _mode = 4 ;
      }
    }
    delay( 1000 ); // waits a few milliseconds 
  }
  if ( _mode == 4 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "Add flour, sugar" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "and chocolate" ); // Print a message to the LCD.
      _mode = 5 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 5 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "use mixing" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "machine to mix" ); // Print a message to the LCD.
      _mode = 6 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 6 ) {
    _currentlight = analogRead( A0 ) ;
    _time = 0 ;
    if ( _currentlight < 300 ) {
      _time2 = ( _time2 + 1 ) ;
      delay( 1000 ); // waits a few milliseconds 
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "mixing" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( _time2 ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(4 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "sec" ); // Print a message to the LCD.
      if ( _time == 20 ) {
        lcd_I2C_27.clear();
        lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
        lcd_I2C_27.print( "time's up" ); // Print a message to the LCD.
        tone(11, 440, 2000); //buzzer or speaker frequency 
        _mode = 7 ;
      }
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 7 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "roll them to" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "walnut-size balls" ); // Print a message to the LCD.
      delay( 3000 ); // waits a few milliseconds 
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "place them on" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "tray" ); // Print a message to the LCD.
      _mode = 8 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 8 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "flatten them" ); // Print a message to the LCD.
      _mode = 9 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 9 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "put into oven" ); // Print a message to the LCD.
      lcd_I2C_27.setCursor(0 , 1) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "bake for 20mins" ); // Print a message to the LCD.
      _mode = 10 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
  if ( _mode == 10 ) {
    _currentlight = analogRead( A0 ) ;
    if ( _currentlight < 300 ) {
      lcd_I2C_27.clear();
      lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 
      lcd_I2C_27.print( "done :)" ); // Print a message to the LCD.
      _mode = 0 ;
      delay( 1000 ); // waits a few milliseconds 
    }
  }
}
