skizziks_53

5-minute LED blinker v2.0

Sep 17th, 2020 (edited)
1,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. /*
  2.   Reddit/Arduino --- September 17, 2020
  3.  
  4.   Button with 5-minute LED blinker ver 2.0
  5.   Board: Uno/Leonardo, Mega (needs a button @ input_pullup and the pin#13 LED)
  6.  
  7.   ----------------------------------------
  8.  
  9.   Two different LED blink times added: time=on and time=off are separate values.
  10.  
  11. */
  12.  
  13. int button1_pin = 2; // This is the pin that the button is connected to. This will be used as INPUT_PULLUP, so the active state = LOW.
  14. int button1_pin_state = 0; // This is to store the state of the button pin when read.
  15.  
  16. bool button1_enabled = true; // This is to block checking the button when it is disabled due to the debounce timer.
  17. int button1_debounce_time = 300; // This is the milliseconds time to debounce button1.
  18. unsigned long button1_debounce_begin_time = 0; // Used for millis() values.
  19. unsigned long button1_debounce_current_time = 0; // Used for millis() values.
  20.  
  21. int led_pin = 13; // This is the pin that the LED is on.
  22. int led_pin_state = 0; // This is the state to write to the LED pin (either HIGH or LOW).
  23.  
  24. bool led_timer_active = false;
  25. int led_blink_cycle_time_in_seconds = 300; // The 5-minute time for the LED to blink is counted in seconds here. 300 seconds == 5 minutes.
  26. int led_blink__seconds_counter = 0; // This variable is used to count up to the blink_cycle_time limit.
  27. unsigned long led_cycle_timer__previous_time = 0; // Used for millis() values.
  28. unsigned long led_cycle_timer__current_time = 0; // Used for millis() values.
  29.  
  30. int blinker_time_interval_on = 200; // This is the milliseconds time for the LED to stay on when it's blinking.
  31. int blinker_time_interval_off = 800; // This is the milliseconds time for the LED to stay off when it's blinking.
  32. int this_blink_interval = 0; // This is a variable to hold one of the above values in use.
  33. unsigned long blinker_timer__previous_time = 0; // Used for millis() values.
  34. unsigned long blinker_timer__current_time = 0; // Used for millis() values.
  35.  
  36.  
  37.  
  38.  
  39. void setup() {
  40.   Serial.begin(9600);
  41.   pinMode(button1_pin, INPUT_PULLUP);
  42.   pinMode(led_pin, OUTPUT);
  43.   digitalWrite(led_pin, 0); // Some clone Unos write pin #13 HIGH unless you tell them to put it low.
  44.   Serial.println("Exiting setup.");
  45. }
  46.  
  47.  
  48. void loop() {
  49.  
  50.   if (button1_enabled == true) {
  51.     if (led_timer_active == false) {
  52.       button1_pin_state = digitalRead(button1_pin);
  53.       if (button1_pin_state == LOW) {
  54.         Serial.println("Button1 pressed.");
  55.         disable_button1();
  56.         start_LED_blinker();
  57.       }
  58.     }
  59.   }
  60.  
  61.   if (button1_enabled == false) {
  62.     check_button1_debounce_timer();
  63.   }
  64.  
  65.   if (led_timer_active == true) {
  66.     check_LED_timer(); // This is the function that checks the 5-minute LED run time.
  67.   }
  68.  
  69.   if (led_timer_active == true) {
  70.     check_LED_blinker(); // This is the function that blinks the LED on and off every 100ms.
  71.     // Since the LED doesn't blink at all unless the LED timer is on, you can use the led_timer_active variable
  72.     //      to control both the LED timer and the LED blinker.
  73.   }
  74. } // end of main loop
  75.  
  76.  
  77. void disable_button1() {
  78.   Serial.println("Button1 disabled.");
  79.   button1_enabled = false;
  80.   button1_debounce_begin_time = millis();
  81. }
  82.  
  83.  
  84. void check_button1_debounce_timer() {
  85.   button1_debounce_current_time = millis();
  86.   if (button1_debounce_current_time >= button1_debounce_begin_time) {
  87.     if (button1_debounce_current_time >= (button1_debounce_begin_time + button1_debounce_time)) {
  88.       button1_enabled = true;
  89.       Serial.println("Button1 re-enabled.");
  90.     }
  91.   }
  92.   else {
  93.     button1_debounce_begin_time = millis();
  94.   }
  95. }
  96.  
  97.  
  98. void start_LED_blinker() {
  99.   led_timer_active = true;
  100.   led_pin_state = 1;
  101.   digitalWrite(led_pin, led_pin_state);
  102.   led_cycle_timer__previous_time = millis();
  103.   blinker_timer__previous_time = millis();
  104.   led_blink__seconds_counter = 0;
  105.   Serial.println("Starting LED blinker.");
  106. }
  107.  
  108.  
  109. void check_LED_timer() {
  110.   led_cycle_timer__current_time = millis();
  111.   if (led_cycle_timer__current_time >= led_cycle_timer__previous_time) {
  112.     if (led_cycle_timer__current_time >= (led_cycle_timer__previous_time + 1000)) {
  113.       led_blink__seconds_counter++;
  114.       if (led_blink__seconds_counter >= led_blink_cycle_time_in_seconds) {
  115.         led_timer_active = false;
  116.         // Turn the LED off so it doesn't stay on.
  117.         digitalWrite(led_pin, LOW);
  118.         Serial.println("Stopping LED blinker.");
  119.       }
  120.       led_cycle_timer__previous_time = millis();
  121.     }
  122.   }
  123.   else {
  124.     led_cycle_timer__previous_time = millis();
  125.   }
  126. }
  127.  
  128.  
  129. void check_LED_blinker() {
  130.   blinker_timer__current_time = millis();
  131.   if (blinker_timer__current_time >= blinker_timer__previous_time) {
  132.     // Below switches between the two led blink inverval times (on/off)
  133.     this_blink_interval = blinker_time_interval_off; // This sets it to the 'off' time by default.
  134.     if (led_pin_state == 1) { // This changes it to the 'on' time if the LED is currently HIGH.
  135.       this_blink_interval = blinker_time_interval_on;
  136.     }
  137.     if (blinker_timer__current_time >= (blinker_timer__previous_time + this_blink_interval)) {
  138.       led_pin_state = flip_this_pin_state(led_pin_state);
  139.       digitalWrite(led_pin, led_pin_state);
  140.       blinker_timer__previous_time = millis();
  141.     }
  142.   }
  143.   else {
  144.     blinker_timer__previous_time = millis();
  145.   }
  146. }
  147.  
  148.  
  149. int flip_this_pin_state(int this_pin) {
  150.   if (this_pin == 0) {
  151.     this_pin = 1;
  152.   }
  153.   else {
  154.     this_pin = 0;
  155.   }
  156.   return this_pin;
  157. }
  158.  
  159.  
  160.  
  161. // ~~~~~~~ the end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment