// Project#8 - Access Granted!
// Setup instructions:  
// Place jumpers across all 8 LCD header connections (See project video)
// Apply a jumper to the RELAY header to connect GPIO8 to the relay drive circuit
// Apply a jumper to the BUZZ header to connect ADC pin A5 to the buzzer driver

#include "LiquidCrystal.h"      // INCLUDE THE LIBRARY THAT HELPS TO DRIVE THE LCD.
LiquidCrystal lcd(2,3,4,5,6,10);// LCD pins RS/E/D4/D5/D6/D7 connected to UNO pins 2,3,4,5,6,10 Respectively.  USING THE LCD QUICK-CONNECT DIP SWITCH DOES ALL OF THIS FOR YOU ON CHIP-A
#define LCD_EN 11               // LCD Backlight controller definition  
#define relay1 8                // Set the relay1 driver pin to GPIO8
byte incomingByte = 0x00;       // This byte stores the incoming information from the PC
int hold = 0;                   // Create int hold, and set to 0
int relay1_state = 0;           // Create relay1_state variable and set to 0

void setup() {                  // Set up electronics
  lcd.begin(16,02);             // THIS SETS UP THE LCD, WHICH CAN SHOW 16 CHARACTERS ACROSS TWO LINES
  lcd.clear();                  // CLEAR THE LCD
  Serial.begin(115200);         // Set up serial monitor and serial comms to a baud rate of 115200.
  pinMode(relay1,OUTPUT);       // Configure RELAY as an output
  digitalWrite(relay1,LOW);     // Turn the relay off
  pinMode(LCD_EN,OUTPUT);       // Declare LCD_EN as an output
  digitalWrite(LCD_EN,HIGH);    // Turn the LCD backlight on
  pinMode(A5,OUTPUT);           // Change ADC pin A5 from an ADC pin to a digital output
  lcd.print("Project#8/9");
  delay(3000);
  lcd.clear();
  lcd.print("Relay OFF");
}

void loop(){  // This is the main loop, and will loop forever until the RPInoRPI powers down
    while (Serial.available() > 0) {    // Execute the following code only when an instruction from the PC has been received.
    incomingByte = Serial.read();       // Read the incoming data and store it in incomingByte
    if(incomingByte == 53){             // If the received value was 1 (ASCII 49), then do the following:
       digitalWrite(relay1,HIGH);       // Turn the relay ON
       lcd.clear();                     // Clear the LCD
       lcd.print("    Relay ON    ");   // Print this to the LCD
       lcd.setCursor(0,1);              // Set the LCD cursor to the second line
       lcd.print("ACCESS GRANTED!");    // Print this to the second line
       digitalWrite(A5,HIGH);         // Turn the buzzer on
       delay(1000);                    // Wait 1/10th of a second/100ms, then turn the buzzer off and wait another 100ms
       digitalWrite(A5,LOW);
       delay(9000);                    // Wait 10 seconds
       lcd.clear();                     // Clear the LCD
       lcd.print("Relay OFF");          // Print this to the LCD
       digitalWrite(relay1,LOW);        // Turn the relay OFF
     }
     else{
       lcd.clear();                     // Clear the LCD
       lcd.print("    Relay OFF    ");  // Print this to the LCD
       lcd.setCursor(0,1);              // Set the LCD cursor to the second line
       lcd.print("BAD COMBO");          // Print this to the second line
       for(int i = 0 ; i < 5 ; i++){    // Turn the buzzer on and off 5 times
         digitalWrite(A5,HIGH);         // Turn the buzzer on
         delay(100);                    // Wait 1/10th of a second/100ms, then turn the buzzer off and wait another 100ms
         digitalWrite(A5,LOW);
         delay(100);
       }
       delay(4000);                     // Wait 4 seconds
       lcd.clear();                     // Clear the LCD
       lcd.print("Relay OFF");          // Print this to the LCD
       digitalWrite(relay1,LOW);        // Turn the relay OFF
     }
    }
}
     