Advertisement
pleasedontcode

Flashlight Toggle rev_01

Oct 28th, 2024
90
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: Flashlight Toggle
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-28 05:58:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn flashlight on and off */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29.  
  30. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  31. const uint8_t btn_PushButton_PIN_D2 = 2; // Button pin
  32. const uint8_t flashlight_PIN = 9;        // Flashlight pin (LED)
  33.  
  34. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  35. // Initialize the EasyButton object with the button pin
  36. EasyButton button(btn_PushButton_PIN_D2);
  37.  
  38. /****** FUNCTION DEFINITIONS *****/
  39. // Function to be called when the button is pressed
  40. void onPressed()
  41. {
  42.     // Toggle the flashlight state
  43.     static bool flashlightState = false; // Variable to keep track of flashlight state
  44.     flashlightState = !flashlightState;   // Toggle state
  45.     digitalWrite(flashlight_PIN, flashlightState ? HIGH : LOW); // Set flashlight pin
  46.     Serial.println(flashlightState ? "Flashlight ON" : "Flashlight OFF"); // Print current state
  47. }
  48.  
  49. void setup(void)
  50. {
  51.     // Start serial communication at 115200 baud rate
  52.     Serial.begin(115200);
  53.     Serial.println();
  54.     Serial.println(">>> EasyButton pressed example <<<");
  55.  
  56.     // Initialize the button
  57.     button.begin();
  58.     // Attach the onPressed function to the button press event
  59.     button.onPressed(onPressed);
  60.  
  61.     // Set the button pin mode
  62.     pinMode(btn_PushButton_PIN_D2, INPUT_PULLUP);
  63.    
  64.     // Set the flashlight pin mode
  65.     pinMode(flashlight_PIN, OUTPUT); // Set flashlight pin as output
  66.     digitalWrite(flashlight_PIN, LOW); // Ensure flashlight is off initially
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // Read the button state
  72.     button.read();
  73. }
  74.  
  75. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement