/********* 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: GPSTime
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-10-29 01:18:16
	- Source Code generated by: Francesco Alessandro

********* Pleasedontcode.com **********/
/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>
#include <Wire.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 readGPSData(void);
void displayTimeOnLED(void);
void triggerContactClosure(void);

/***** 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);

/***** DEFINITION OF I2C PINS *****/
const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;

// Create an object of the MicroNMEA class
MicroNMEA gps;

void setup(void)
{
  // Initialize digital output pin for contact closure
  pinMode(contactClosure_PIN_D2, OUTPUT);

  // Initialize software serial for GPS module
  gpsSerial.begin(9600);

  // Initialize I2C communication for LCD display
  Wire.begin();
  LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.clear();
}

void loop(void)
{
  readGPSData();
  displayTimeOnLED();
  triggerContactClosure();
}

void readGPSData(void)
{
  // Read NMEA data from GPS module
  while (gpsSerial.available())
  {
    char c = gpsSerial.read();
    // Process the received character using the gps object
    gps.process(c);
  }
}

void displayTimeOnLED(void)
{
  // Get the current time from the GPS module using the gps object
  int hour = gps.getHour();
  int minute = gps.getMinute();
  int second = gps.getSecond();

  // Display the time on the LCD display
  LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(hour);
  lcd.print(":");
  if (minute < 10)
  {
    lcd.print("0");
  }
  lcd.print(minute);
  lcd.print(":");
  if (second < 10)
  {
    lcd.print("0");
  }
  lcd.print(second);
}

void triggerContactClosure(void)
{
  // Check if minutes and seconds are 00:00 using the gps object
  if (gps.getMinute() == 0 && gps.getSecond() == 0)
  {
    // Trigger contact closure for about 1 second
    digitalWrite(contactClosure_PIN_D2, HIGH);
    delay(1000);
    digitalWrite(contactClosure_PIN_D2, LOW);
  }
}