CCCPaste

Button Timer rev_09

/********* 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: Button Timer
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2024-02-11 13:10:28

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* lcd_LCD1602I2C_I2C line one: 1CZAS,   second line: */
	/* 2CZAS  every 1 second subtract the value 0:01 from */
	/* 1CZAS  2_PushButton held down starts subtracting */
	/* the value 0:01 from 2CZAS every 1 second.  The */
	/* minimum value of 1CZAS and 2CZAS is 0:00. */
/****** END SYSTEM REQUIREMENTS *****/

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void update1Czas(void);
void update2Czas(void);
void onPressedForDuration1(void);
void onPressedForDuration2(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t BUTTON_PIN_D2 = 2;
const uint8_t BUTTON_PIN_D5 = 5;

/***** DEFINITION OF I2C PINS *****/
const uint8_t I2C_PIN_SDA_A4 = A4;
const uint8_t I2C_PIN_SCL_A5 = A5;
const uint8_t I2C_SLAVE_ADDRESS = 39;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
const uint8_t LED_PIN_D3 = 3;
const uint8_t BUZZER_PIN_D4 = 4;
float LED_PIN_D3_phyData = 0.0;
float BUZZER_PIN_D4_phyData = 0.0;

/***** DEFINITION OF TIME VARIABLES *****/
unsigned long previousMillis1Czas = 0;
unsigned long previousMillis2Czas = 0;
const unsigned long interval1Czas = 1000;
const unsigned long interval2Czas = 1000;
int minutes1Czas = 0;
int seconds1Czas = 0;
int minutes2Czas = 0;
int seconds2Czas = 0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button1(BUTTON_PIN_D2);
EasyButton button2(BUTTON_PIN_D5);
LiquidCrystal_I2C lcd(I2C_SLAVE_ADDRESS, 16, 2); // Changed the parameters for lcd.begin()

void setup(void)
{
  // put your setup code here, to run once:

  // Initialize the buttons
  button1.begin();
  button2.begin();

  // Set button callbacks
  button1.onPressedFor(2000, onPressedForDuration1);
  button2.onPressedFor(2000, onPressedForDuration2);

  // Initialize the LCD
  lcd.begin(16, 2); // Changed the parameters for lcd.begin()
  lcd.setBacklight(LOW);

  // Set up the pin modes
  pinMode(LED_PIN_D3, OUTPUT);
  pinMode(BUZZER_PIN_D4, OUTPUT);
}

void loop(void)
{
  // put your main code here, to run repeatedly:

  // Update button states
  button1.read();
  button2.read();

  // Update 1CZAS
  update1Czas();

  // Update 2CZAS
  update2Czas();
}

void update1Czas()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis1Czas >= interval1Czas)
  {
    previousMillis1Czas = currentMillis;

    if (button1.isPressed())
    {
      if (minutes1Czas > 0 || seconds1Czas > 0)
      {
        seconds1Czas--;
        if (seconds1Czas < 0)
        {
          seconds1Czas = 59;
          minutes1Czas--;
          if (minutes1Czas < 0)
          {
            minutes1Czas = 0;
            seconds1Czas = 0;
          }
        }
      }
    }
    else
    {
      seconds1Czas++;
      if (seconds1Czas > 59)
      {
        seconds1Czas = 0;
        minutes1Czas++;
      }
    }

    lcd.setCursor(0, 0);
    lcd.print("1CZAS: ");
    if (minutes1Czas < 10)
    {
      lcd.print("0");
    }
    lcd.print(minutes1Czas);
    lcd.print(":");
    if (seconds1Czas < 10)
    {
      lcd.print("0");
    }
    lcd.print(seconds1Czas);
  }
}

void update2Czas()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis2Czas >= interval2Czas)
  {
    previousMillis2Czas = currentMillis;

    if (button2.isPressed())
    {
      if (minutes2Czas > 0 || seconds2Czas > 0)
      {
        seconds2Czas--;
        if (seconds2Czas < 0)
        {
          seconds2Czas = 59;
          minutes2Czas--;
          if (minutes2Czas < 0)
          {
            minutes2Czas = 0;
            seconds2Czas = 0;
          }
        }
      }
    }
    else
    {
      seconds2Czas++;
      if (seconds2Czas > 59)
      {
        seconds2Czas = 0;
        minutes2Czas++;
      }
    }

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

void onPressedForDuration1()
{
  // Code to be executed when button1 is pressed for the given duration.
  // Add your code here
}

void onPressedForDuration2()
{
  // Code to be executed when button2 is pressed for the given duration.
  // Add your code here
}