/********* 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: Digital Control
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2024-02-11 12:37:01

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* lcd_LCD1602I2C_I2C line one: 1CZAS,   second line: */
	/* 2CZAS  1_PushButton when pressed starts */
	/* subtracting 0:01 value from 1CZAS every 1 second. */
	/* 2_PushButton held down starts subtracting the */
	/* value 0:01 from 2CZAS every 1 second. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* 1CZAS is the time clock, set the start value: 7:00 */
	/* 2CZAS is the time clock, set the start value: 2:00 */
	/* 2_PushButton released restores the initial value */
	/* of 2CZAS */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Wire.h>
#include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
#include <LiquidCrystal_I2C.h>    //https://github.com/marcoschwartz/LiquidCrystal_I2C

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void); // Function to update the output data

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t PUSHBUTTON_PIN_D2 = 2;
const uint8_t PUSHBUTTON_PIN_D5 = 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t L1_LED_PIN_D3 = 3;
const uint8_t B_ACTIVEBUZZER_OUTPUT_PIN_D4 = 4;

/***** DEFINITION OF I2C PINS *****/
const uint8_t LCD_I2C_PIN_SDA_A4 = A4;
const uint8_t LCD_I2C_PIN_SCL_A5 = A5;
const uint8_t LCD_I2C_SLAVE_ADDRESS = 0x27; // Replace with the correct I2C address of your LCD module

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool l1_LED_PIN_D3_rawData = false;
bool b_ActiveBuzzer_output_PIN_D4_rawData = false;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float l1_LED_PIN_D3_phyData = 0.0;
float b_ActiveBuzzer_output_PIN_D4_phyData = 0.0;

/***** DEFINITION OF SYSTEM VARIABLES *****/
bool button1_pressed = false;
bool button2_pressed = false;
uint8_t czas1_minutes = 7;
uint8_t czas1_seconds = 0;
uint8_t czas2_minutes = 2;
uint8_t czas2_seconds = 0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button1(PUSHBUTTON_PIN_D2);
EasyButton button2(PUSHBUTTON_PIN_D5);
LiquidCrystal_I2C lcd(LCD_I2C_SLAVE_ADDRESS, 16, 2);  // Change the columns and rows according to your LCD module

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(PUSHBUTTON_PIN_D2, INPUT_PULLUP);
    pinMode(PUSHBUTTON_PIN_D5, INPUT_PULLUP);
    pinMode(L1_LED_PIN_D3, OUTPUT);
    pinMode(B_ACTIVEBUZZER_OUTPUT_PIN_D4, OUTPUT);
    lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
    lcd.backlight(); // Turn on the LCD backlight
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    button1.read();
    button2.read();

    // Check if button 1 was pressed
    if (button1.wasPressed())
    {
        czas1_seconds--;
        if (czas1_seconds == 255) // Check for underflow
        {
            czas1_seconds = 59;
            czas1_minutes--;
            if (czas1_minutes == 255) // Check for underflow
            {
                // Restore initial value
                czas1_minutes = 7;
                czas1_seconds = 0;
            }
        }
    }

    // Check if button 2 was held down
    if (button2.isPressed())
    {
        czas2_seconds--;
        if (czas2_seconds == 255) // Check for underflow
        {
            czas2_seconds = 59;
            czas2_minutes--;
            if (czas2_minutes == 255) // Check for underflow
            {
                // Restore initial value
                czas2_minutes = 2;
                czas2_seconds = 0;
            }
        }
    }

    // Check if button 2 was released
    if (button2.wasReleased())
    {
        // Restore initial value
        czas2_minutes = 2;
        czas2_seconds = 0;
    }

    // Display the time on the LCD
    lcd.setCursor(0, 0);
    lcd.print("1CZAS: ");
    lcd.print(czas1_minutes);
    lcd.print(":");
    if (czas1_seconds < 10)
    {
        lcd.print("0");
    }
    lcd.print(czas1_seconds);

    lcd.setCursor(0, 1);
    lcd.print("2CZAS: ");
    lcd.print(czas2_minutes);
    lcd.print(":");
    if (czas2_seconds < 10)
    {
        lcd.print("0");
    }
    lcd.print(czas2_seconds);

    updateOutputs(); // Refresh output data
    delay(1000); // Delay for 1 second
}

void updateOutputs(void)
{
    digitalWrite(L1_LED_PIN_D3, l1_LED_PIN_D3_rawData);
    digitalWrite(B_ACTIVEBUZZER_OUTPUT_PIN_D4, b_ActiveBuzzer_output_PIN_D4_rawData);
}
