/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Scanner
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-10-29 14:21:12
	- Source Code generated by: AlexWind

********* Pleasedontcode.com **********/
/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <MicroNMEA.h>

/****** SYSTEM REQUIREMENT 1 *****/
/* Reads NMEA 8bit data from GPS module. Reads hours */
/* minutes, seconds and Display h, m, s on 6x 7seg */
/* LED display. When mins and secs are 00 00, contact */
/* closure for about 1 sec. */

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void displayTime(uint8_t hours, uint8_t minutes, uint8_t seconds);
void triggerContactClosure();

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t contactClosure_PIN_D2 = 2;

/***** DEFINITION OF Software Serial *****/
const uint8_t gpsSerial_PIN_SERIAL_TX_A1 = A1;
const uint8_t gpsSerial_PIN_SERIAL_RX_A0 = A0;
SoftwareSerial gpsSerial(gpsSerial_PIN_SERIAL_RX_A0, gpsSerial_PIN_SERIAL_TX_A1);

// Initialize MicroNMEA library
MicroNMEA MicroNMEA;

void setup(void)
{
  // Set contact closure pin as output
  pinMode(contactClosure_PIN_D2, OUTPUT);

  // Initialize GPS serial communication
  gpsSerial.begin(9600);

  // Initialize LCD display
  LiquidCrystal_I2C lcd(0x27, 20, 4);
  lcd.begin(20, 4);
  lcd.backlight();
}

void loop(void)
{
  // Read GPS data
  if (gpsSerial.available())
  {
    char c = gpsSerial.read();
    MicroNMEA.process(c);
  }

  // Check if a valid fix is available
  if (MicroNMEA.isValid())
  {
    // Get the time from the GPS data
    uint8_t hours = MicroNMEA.getHour();
    uint8_t minutes = MicroNMEA.getMinute();
    uint8_t seconds = MicroNMEA.getSecond();

    // Display the time on the LCD display
    displayTime(hours, minutes, seconds);

    // Check if minutes and seconds are 00:00
    if (minutes == 0 && seconds == 0)
    {
      // Trigger contact closure for about 1 second
      triggerContactClosure();
    }
  }
}

void displayTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
  // Display the time on the LCD display
  LiquidCrystal_I2C lcd(0x27, 20, 4);
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(hours);
  lcd.print(":");
  if (minutes < 10)
  {
    lcd.print("0");
  }
  lcd.print(minutes);
  lcd.print(":");
  if (seconds < 10)
  {
    lcd.print("0");
  }
  lcd.print(seconds);
}

void triggerContactClosure()
{
  // Trigger contact closure for about 1 second
  digitalWrite(contactClosure_PIN_D2, HIGH);
  delay(1000);
  digitalWrite(contactClosure_PIN_D2, LOW);
}