pleasedontcode

Debounced Blink rev_01

Sep 21st, 2025
134
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: Debounced Blink
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-09-21 07:02:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* ensure all functions are working properly */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void initSystem(void);
  32. bool isButtonPressed(void);
  33. void updateLED(void);
  34.  
  35. // Pin definitions
  36. const int LED_PIN = 13;
  37. const int BUTTON_PIN = 2;
  38.  
  39. // LED blink control
  40. unsigned long lastToggleTime = 0;
  41. unsigned int blinkInterval = 500; // milliseconds
  42. bool ledState = false;
  43.  
  44. void initSystem(void)
  45. {
  46.     // Initialize system components and state
  47.     // (Currently handled in setup; this function reserved for future expansion)
  48. }
  49.  
  50. void updateLED(void)
  51. {
  52.     unsigned long now = millis();
  53.     if (now - lastToggleTime >= blinkInterval) {
  54.         lastToggleTime = now;
  55.         ledState = !ledState;
  56.         digitalWrite(LED_PIN, ledState ? HIGH : LOW);
  57.     }
  58. }
  59.  
  60. bool isButtonPressed(void)
  61. {
  62.     // Simple debounce: detect a press and wait for release
  63.     if (digitalRead(BUTTON_PIN) == LOW) {
  64.         delay(50); // simple debounce
  65.         if (digitalRead(BUTTON_PIN) == LOW) {
  66.             // Wait for button release
  67.             while (digitalRead(BUTTON_PIN) == LOW) {
  68.                 // busy wait
  69.             }
  70.             return true;
  71.         }
  72.     }
  73.     return false;
  74. }
  75.  
  76. void setup(void)
  77. {
  78.     // put your setup code here, to run once:
  79.     pinMode(LED_PIN, OUTPUT);
  80.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  81.     digitalWrite(LED_PIN, LOW);
  82.     // Initialize last toggle time to current time
  83.     lastToggleTime = millis();
  84.     initSystem();
  85. }
  86.  
  87.  
  88. void loop(void)
  89. {
  90.     // Debounced button press changes blink interval
  91.     if (isButtonPressed()) {
  92.         // Toggle between two speeds for visibility
  93.         blinkInterval = (blinkInterval == 500) ? 200 : 500;
  94.     }
  95.  
  96.     // Update LED state based on current interval
  97.     updateLED();
  98.  
  99.     // Other tasks can be added here
  100. }
  101.  
  102. /* END CODE */
  103.  
Advertisement
Add Comment
Please, Sign In to add comment