Advertisement
pleasedontcode

LED Toggle rev_02

Nov 3rd, 2024
76
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:45:53
  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. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* do not use pullup */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs(void); // Function prototype for updating outputs
  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; // Red LED state
  49. bool    ledverde_LED_PIN_D5_rawData     = 1; // Green LED state (starts ON)
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Initialize EasyButton objects for each button without pull-up resistors
  53. EasyButton button1(button1_PushButton_PIN_D2, 35, false); // No pullup
  54. EasyButton button2(button2_PushButton_PIN_D3, 35, false); // No pullup
  55.  
  56. // Variable to track if the system is unlocked
  57. bool systemUnlocked = false;
  58.  
  59. // Function to handle button press events
  60. void onButton1Pressed() {
  61.   if (systemUnlocked) { // Only toggle the red LED if the system is unlocked
  62.     ledrosso_LED_PIN_D4_rawData = !ledrosso_LED_PIN_D4_rawData; // Toggle LED state
  63.   }
  64. }
  65.  
  66. void onButton2Pressed() {
  67.   systemUnlocked = !systemUnlocked; // Toggle system lock state
  68.   // If the system is locked, turn off the red LED
  69.   if (!systemUnlocked) {
  70.     ledrosso_LED_PIN_D4_rawData = 0; // Ensure red LED is off when locked
  71.   }
  72. }
  73.  
  74. void setup(void)
  75. {
  76.     // put your setup code here, to run once:
  77.  
  78.     pinMode(button1_PushButton_PIN_D2, INPUT); // No pull-up resistor
  79.     pinMode(button2_PushButton_PIN_D3, INPUT); // No pull-up resistor
  80.  
  81.     pinMode(ledrosso_LED_PIN_D4, OUTPUT);
  82.     pinMode(ledverde_LED_PIN_D5, OUTPUT);
  83.  
  84.     // Initialize the green LED to be ON
  85.     digitalWrite(ledverde_LED_PIN_D5, HIGH);
  86.  
  87.     // Initialize the EasyButton objects
  88.     button1.begin();
  89.     button2.begin();
  90.  
  91.     // Attach button press event handlers
  92.     button1.onPressed(onButton1Pressed);
  93.     button2.onPressed(onButton2Pressed);
  94. }
  95.  
  96. void loop(void)
  97. {
  98.     // put your main code here, to run repeatedly:
  99.  
  100.     button1.read(); // Read the state of button 1
  101.     button2.read(); // Read the state of button 2
  102.     updateOutputs(); // Refresh output data
  103. }
  104.  
  105. void updateOutputs()
  106. {
  107.     digitalWrite(ledrosso_LED_PIN_D4, ledrosso_LED_PIN_D4_rawData);
  108.     digitalWrite(ledverde_LED_PIN_D5, ledverde_LED_PIN_D5_rawData);
  109. }
  110.  
  111. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement