// Project#3 - Datetime Automation!
// 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

#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
byte incomingByte = 0x00;       // Create a byte variable named "incomingByte".  The data we receive from the PC will be stored here.
#define RELAY 8


void setup() {
  lcd.begin(16,02);              // THIS SETS UP THE LCD, WHICH CAN SHOW 16 CHARACTERS ACROSS TWO LINES
  lcd.clear();                   // CLEAR THE LCD
  pinMode(LCD_EN,OUTPUT);        // Declare LCD_EN as an output
  digitalWrite(LCD_EN,HIGH);     // Turn the LCD backlight on
  pinMode(RELAY,OUTPUT);         // Configure the relay as an output
  lcd.print("Project#3!");       // Print this to the top line of the LCD
  Serial.begin(115200);          // Start the serial monitor / enable serial prints to the PC at a baud rate of 115200
}

void loop(){   // This is the main loop.  It will look over and over until the PPRInoRPI powers down                        
    while (Serial.available() > 0) {    // If a serial communcation from the PC has been received, do the following:
    incomingByte = Serial.read();       // Read it and store it in 'incomingByte'
    if(incomingByte == 53){             // If the received value is 5 (ASCII 53), then turn the relay on for 10 seconds...
      lcd.clear();                 // Clear the LCD
      lcd.print("RELAY ON!");      // Print this to the top line of the LCD
      digitalWrite(RELAY,HIGH);    // Turn the relay on
      delay(10000);                // Wait 10 seconds / 10000ms
      lcd.clear();                 // Clear the LCD
      lcd.print("RELAY OFF!");     // Print this to the LCD
      digitalWrite(RELAY,LOW);     // Print this to the LCD
      delay(3000);                 // Wait for 3 seconds
      lcd.clear();                 // Clear the LCD
      lcd.print("Project#3!");     // Print this to the top line of the LCD
      }
    // Check out this table: https://www.rapidtables.com/code/text/ascii-table.html
  }
}