/********* 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: Flashlight Toggle
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-10-28 05:58:06
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* turn flashlight on and 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 btn_PushButton_PIN_D2 = 2; // Button pin
const uint8_t flashlight_PIN = 9; // Flashlight pin (LED)
/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Initialize the EasyButton object with the button pin
EasyButton button(btn_PushButton_PIN_D2);
/****** FUNCTION DEFINITIONS *****/
// Function to be called when the button is pressed
void onPressed()
{
// Toggle the flashlight state
static bool flashlightState = false; // Variable to keep track of flashlight state
flashlightState = !flashlightState; // Toggle state
digitalWrite(flashlight_PIN, flashlightState ? HIGH : LOW); // Set flashlight pin
Serial.println(flashlightState ? "Flashlight ON" : "Flashlight OFF"); // Print current state
}
void setup(void)
{
// Start serial communication at 115200 baud rate
Serial.begin(115200);
Serial.println();
Serial.println(">>> EasyButton pressed example <<<");
// Initialize the button
button.begin();
// Attach the onPressed function to the button press event
button.onPressed(onPressed);
// Set the button pin mode
pinMode(btn_PushButton_PIN_D2, INPUT_PULLUP);
// Set the flashlight pin mode
pinMode(flashlight_PIN, OUTPUT); // Set flashlight pin as output
digitalWrite(flashlight_PIN, LOW); // Ensure flashlight is off initially
}
void loop(void)
{
// Read the button state
button.read();
}
/* END CODE */