/********* 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: LED Toggle
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-11-03 12:44:03
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* il sistema inizia con il led verde acceso . se */
/* premo il pulsante 1 non succede nulla. quando */
/* premo il pulsante due il sistema si sblocca e */
/* quindi quando premo il pulsante 1 cambia il led */
/* che si accende. se premo di nuovo il pulsante 2 il */
/* sistema bloc */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include //https://github.com/evert-arias/EasyButton
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void); // Function prototype for updating outputs
void onButton1Pressed(void); // Function prototype for button 1 press handler
void onButton2Pressed(void); // Function prototype for button 2 press handler
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button1_PushButton_PIN_D2 = 2;
const uint8_t button2_PushButton_PIN_D3 = 3;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t ledrosso_LED_PIN_D4 = 4;
const uint8_t ledverde_LED_PIN_D5 = 5;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool ledrosso_LED_PIN_D4_rawData = 0; // LED red state
bool ledverde_LED_PIN_D5_rawData = 1; // LED green state (initially on)
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Instantiate EasyButton objects for each button
EasyButton button1(button1_PushButton_PIN_D2); // Button 1 instance
EasyButton button2(button2_PushButton_PIN_D3); // Button 2 instance
// Variable to track system state (locked/unlocked)
bool systemUnlocked = false;
void setup(void)
{
// put your setup code here, to run once:
pinMode(button1_PushButton_PIN_D2, INPUT_PULLUP);
pinMode(button2_PushButton_PIN_D3, INPUT_PULLUP);
pinMode(ledrosso_LED_PIN_D4, OUTPUT);
pinMode(ledverde_LED_PIN_D5, OUTPUT);
// Initialize the buttons
button1.begin();
button2.begin();
// Set initial state of the LEDs
updateOutputs();
// Attach button press handlers
button1.onPressed(onButton1Pressed);
button2.onPressed(onButton2Pressed);
}
void loop(void)
{
// put your main code here, to run repeatedly:
// Read the button states
button1.read();
button2.read();
}
void updateOutputs()
{
// Update the LED states based on the raw data
digitalWrite(ledrosso_LED_PIN_D4, ledrosso_LED_PIN_D4_rawData);
digitalWrite(ledverde_LED_PIN_D5, ledverde_LED_PIN_D5_rawData);
}
void onButton1Pressed(void) {
if (systemUnlocked) {
// Toggle the red LED state when button 1 is pressed
ledrosso_LED_PIN_D4_rawData = !ledrosso_LED_PIN_D4_rawData; // Toggle LED state
updateOutputs(); // Update outputs after changing LED state
}
}
void onButton2Pressed(void) {
// Toggle the system state when button 2 is pressed
systemUnlocked = !systemUnlocked; // Lock or unlock the system
if (systemUnlocked) {
ledverde_LED_PIN_D5_rawData = 0; // Turn off green LED when unlocked
} else {
ledverde_LED_PIN_D5_rawData = 1; // Turn on green LED when locked
}
updateOutputs(); // Update outputs after changing states
}
/* END CODE */