CCCPaste

Time Controller rev_11

/********* 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: Time Controller
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2024-02-11 13:31:00

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* lcd_LCD1602I2C_I2C line one: 1CZAS,   second line: */
	/* 2CZAS  1CZAS is the time clock, set the start */
	/* value: 7:00  2CZAS is the time clock, set the */
	/* start value: 2:00  2_PushButton held down starts */
	/* subtracting 0:01 value from 2CZAS every 1 second */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Subtracts the value 0:01 from 1CZAS every 1 second */
	/* from startup.  Releasing the 2_PushButton restores */
	/* the initial 2CZAS value.  The minimum value of */
	/* 1CZAS and 2CZAS is 0:00.  Activate the */
	/* b_ActiveBuzzer_output when the minimum value is */
	/* 0:00 */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup();
void loop();
void updateCzasClock();
void updatePushButtons();
void updateOutputs();

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t PushButton_PIN_D5 = 5;
const uint8_t PushButton_PIN_D2 = 2;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t ActiveBuzzer_output_PIN_D4 = 4;

/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool ActiveBuzzer_output_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
int czas1Hour = 7;
int czas1Minute = 0;
int czas2Hour = 2;
int czas2Minute = 0;
bool subtractCzas2 = false;
const int BUZZER_MINUTE = 0;

/****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
EasyButton button(PushButton_PIN_D5);
EasyButton button2(PushButton_PIN_D2);
LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);

void onPressedForDuration()
{
  // Callback function to be called when the button is pressed for the given duration.
  subtractCzas2 = true;
}

void setup()
{
  // put your setup code here, to run once:
  pinMode(PushButton_PIN_D5, INPUT_PULLUP);
  pinMode(PushButton_PIN_D2, INPUT_PULLUP);

  pinMode(ActiveBuzzer_output_PIN_D4, OUTPUT);

  button.begin();
  button.onPressedFor(2000, onPressedForDuration);

  button2.begin();

  lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
  lcd.setBacklight(HIGH); // Turn on the backlight
  lcd.clear(); // Clear the LCD screen
  lcd.setCursor(0, 0);
  lcd.print("1CZAS:     7:00");
  lcd.setCursor(0, 1);
  lcd.print("2CZAS:     2:00");
}

void loop()
{
  // put your main code here, to run repeatedly:
  updateCzasClock();
  updatePushButtons();
  updateOutputs();
}

void updateCzasClock()
{
  if (subtractCzas2)
  {
    if (czas2Minute > BUZZER_MINUTE)
    {
      czas2Minute--;
    }
    else
    {
      if (czas2Hour > 0)
      {
        czas2Hour--;
        czas2Minute = 59;
      }
      else
      {
        if (czas1Minute > BUZZER_MINUTE)
        {
          czas1Minute--;
        }
        else
        {
          if (czas1Hour > 0)
          {
            czas1Hour--;
            czas1Minute = 59;
          }
          else
          {
            digitalWrite(ActiveBuzzer_output_PIN_D4, HIGH);
          }
        }
      }
    }
  }
}

void updatePushButtons()
{
  button.read();
  button2.read();

  if (button.isReleased())
  {
    subtractCzas2 = false;
  }

  if (button2.isPressed())
  {
    if (czas2Minute > BUZZER_MINUTE || czas2Hour > 0)
    {
      czas2Minute--;
      delay(1000);
    }
  }
}

void updateOutputs()
{
  digitalWrite(ActiveBuzzer_output_PIN_D4, ActiveBuzzer_output_PIN_D4_rawData);

  lcd.setCursor(7, 0);
  lcd.print(czas1Hour < 10 ? "0" : "");
  lcd.print(czas1Hour);
  lcd.print(":");
  lcd.print(czas1Minute < 10 ? "0" : "");
  lcd.print(czas1Minute);

  lcd.setCursor(7, 1);
  lcd.print(czas2Hour < 10 ? "0" : "");
  lcd.print(czas2Hour);
  lcd.print(":");
  lcd.print(czas2Minute < 10 ? "0" : "");
  lcd.print(czas2Minute);
}