CCCPaste

Button Toggle rev_01

/********* 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 Toggle
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-02-09 19:56:20

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* if trigger is pressed, output 7 will turn on off */
	/* for 10 times, then will stay on for 10 seconds */
	/* then will turn on off 3 times then turn off */
/****** END SYSTEM REQUIREMENTS *****/

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t trigger_PushButton_PIN_D2 = 2;
const uint8_t output_PIN_D7 = 7;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton triggerButton(trigger_PushButton_PIN_D2);

// State variables
bool isTriggered = false;
bool isToggleOn = false;
int toggleCount = 0;
unsigned long startTime = 0;

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(trigger_PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(output_PIN_D7, OUTPUT);

  // Initialize the EasyButton object
  triggerButton.begin();
}

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

  // Read the state of the button
  triggerButton.read();

  // Check if the button is pressed
  if (triggerButton.isPressed())
  {
    // Toggle the output pin if the button is pressed
    isTriggered = true;
    toggleOutputPin();
  }

  // Check if the toggle count has reached 10
  if (toggleCount == 10)
  {
    // Turn on the output pin for 10 seconds
    if (isToggleOn)
    {
      digitalWrite(output_PIN_D7, HIGH);
      startTime = millis();
      isToggleOn = false;
    }
    else
    {
      // Check if 10 seconds have passed
      if (millis() - startTime >= 10000)
      {
        // Turn off the output pin
        digitalWrite(output_PIN_D7, LOW);
        isToggleOn = true;
        toggleCount = 0;
        isTriggered = false;
      }
    }
  }

  // Check if the toggle count has reached 3 after the 10 seconds
  if (toggleCount >= 10 && toggleCount < 13)
  {
    // Toggle the output pin
    toggleOutputPin();
  }
}

void toggleOutputPin()
{
  if (isToggleOn)
  {
    digitalWrite(output_PIN_D7, HIGH);
  }
  else
  {
    digitalWrite(output_PIN_D7, LOW);
  }

  isToggleOn = !isToggleOn;
  toggleCount++;
}