CCCPaste

Button Control 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 Control
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-04-18 12:15:50

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The system control an RGB LED and a piezo using */
	/* Arduino UNO.A short press on the push button (less */
	/* than 300ms) shall change the RGB status in a */
	/* sequence of: dimmed, red, green, blue. A long */
	/* press and release (more than 500ms) shall trigger */
	/* the piezo. */
/****** END SYSTEM REQUIREMENTS *****/

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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t myButton_PushButton_PIN_D2 = 2;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t myRgb_LEDRGB_Red_PIN_D4 = 4;
const uint8_t myRgb_LEDRGB_Green_PIN_D5 = 5;
const uint8_t myRgb_LEDRGB_Blue_PIN_D6 = 6;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool myRgb_LEDRGB_Red_PIN_D4_rawData = 0;
bool myRgb_LEDRGB_Green_PIN_D5_rawData = 0;
bool myRgb_LEDRGB_Blue_PIN_D6_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float myRgb_LEDRGB_Red_PIN_D4_phyData = 0.0;
float myRgb_LEDRGB_Green_PIN_D5_phyData = 0.0;
float myRgb_LEDRGB_Blue_PIN_D6_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(myButton_PushButton_PIN_D2);

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

  pinMode(myButton_PushButton_PIN_D2, INPUT_PULLUP);

  pinMode(myRgb_LEDRGB_Red_PIN_D4, OUTPUT);
  pinMode(myRgb_LEDRGB_Green_PIN_D5, OUTPUT);
  pinMode(myRgb_LEDRGB_Blue_PIN_D6, OUTPUT);

  button.begin();
  button.onPressedFor(300, onPressedForDuration); // Set the duration to 300ms for a short press
  button.onPressedFor(500, onPressedForDuration); // Set the duration to 500ms for a long press
}

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

  button.read();
}

void onPressedForDuration()
{
  if (button.wasPressed()) {
    // Code to be executed when the button is pressed
    if (button.pressedFor(300)) {
      // Code to be executed for a short press (less than 300ms)
      // Change the RGB status in a sequence of: dimmed, red, green, blue
    }
    else if (button.pressedFor(500)) {
      // Code to be executed for a long press (more than 500ms)
      // Trigger the piezo
    }
  }
}