Advertisement
pleasedontcode

LED Toggle rev_01

Nov 3rd, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: LED Toggle
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-03 12:44:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* il sistema inizia con il led verde acceso . se */
  21.     /* premo il pulsante 1 non succede nulla. quando */
  22.     /* premo il pulsante due il sistema si sblocca e */
  23.     /* quindi quando premo il pulsante 1 cambia il led */
  24.     /* che si accende. se premo di nuovo il pulsante 2 il */
  25.     /* sistema bloc */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void); // Function prototype for updating outputs
  35. void onButton1Pressed(void); // Function prototype for button 1 press handler
  36. void onButton2Pressed(void); // Function prototype for button 2 press handler
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t button1_PushButton_PIN_D2     = 2;
  40. const uint8_t button2_PushButton_PIN_D3     = 3;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t ledrosso_LED_PIN_D4       = 4;
  44. const uint8_t ledverde_LED_PIN_D5       = 5;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool    ledrosso_LED_PIN_D4_rawData     = 0; // LED red state
  49. bool    ledverde_LED_PIN_D5_rawData     = 1; // LED green state (initially on)
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Instantiate EasyButton objects for each button
  53. EasyButton button1(button1_PushButton_PIN_D2); // Button 1 instance
  54. EasyButton button2(button2_PushButton_PIN_D3); // Button 2 instance
  55.  
  56. // Variable to track system state (locked/unlocked)
  57. bool systemUnlocked = false;
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.  
  63.     pinMode(button1_PushButton_PIN_D2,  INPUT_PULLUP);
  64.     pinMode(button2_PushButton_PIN_D3,  INPUT_PULLUP);
  65.  
  66.     pinMode(ledrosso_LED_PIN_D4,     OUTPUT);
  67.     pinMode(ledverde_LED_PIN_D5,     OUTPUT);
  68.  
  69.     // Initialize the buttons
  70.     button1.begin();
  71.     button2.begin();
  72.  
  73.     // Set initial state of the LEDs
  74.     updateOutputs();
  75.    
  76.     // Attach button press handlers
  77.     button1.onPressed(onButton1Pressed);
  78.     button2.onPressed(onButton2Pressed);
  79. }
  80.  
  81. void loop(void)
  82. {
  83.     // put your main code here, to run repeatedly:
  84.  
  85.     // Read the button states
  86.     button1.read();
  87.     button2.read();
  88. }
  89.  
  90. void updateOutputs()
  91. {
  92.     // Update the LED states based on the raw data
  93.     digitalWrite(ledrosso_LED_PIN_D4, ledrosso_LED_PIN_D4_rawData);
  94.     digitalWrite(ledverde_LED_PIN_D5, ledverde_LED_PIN_D5_rawData);
  95. }
  96.  
  97. void onButton1Pressed(void) {
  98.     if (systemUnlocked) {
  99.         // Toggle the red LED state when button 1 is pressed
  100.         ledrosso_LED_PIN_D4_rawData = !ledrosso_LED_PIN_D4_rawData; // Toggle LED state
  101.         updateOutputs(); // Update outputs after changing LED state
  102.     }
  103. }
  104.  
  105. void onButton2Pressed(void) {
  106.     // Toggle the system state when button 2 is pressed
  107.     systemUnlocked = !systemUnlocked; // Lock or unlock the system
  108.     if (systemUnlocked) {
  109.         ledverde_LED_PIN_D5_rawData = 0; // Turn off green LED when unlocked
  110.     } else {
  111.         ledverde_LED_PIN_D5_rawData = 1; // Turn on green LED when locked
  112.     }
  113.     updateOutputs(); // Update outputs after changing states
  114. }
  115.  
  116. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement