skizziks_53

5-minute LED blinker v 1.0

Sep 17th, 2020 (edited)
1,831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.93 KB | None | 0 0
  1. /*
  2.   Reddit/Arduino --- September 17, 2020
  3.  
  4.   Button with 5-minute LED blinker
  5.   Board: Uno/Leonardo, Mega (needs a button @ input_pullup and the pin#13 LED)
  6.  
  7.   ----------------------------------------
  8.  
  9.   There is usually more than one way to write a sketch.
  10.   This sketch does not use delay() at all.
  11.   Instead of delay(), this sketch uses three separate software timers that run at the same time, to show how that can be done.
  12.  
  13.   delay() is fine to use for a very simple sketch that only ever has to do ONE thing at a time. It imposes a huge limitation on your programming however.
  14.  
  15.   This sketch uses a software timer to debounce the button (300 milliseconds), another software timer to time the LED blink time (5 minutes),
  16.       and a third software timer to blink the LED on and off (every 100 milliseconds). This was done just to show how all three
  17.       things can be done at the same time.
  18.  
  19.   If you wanted a shorter sketch, one way you could shorten it is to prevent button inputs when the LED blink time is running.
  20.       That way the button would have a 5-minute debounce time, and you wouldn't need the software timer for debouncing the button.
  21.  
  22.   Another way to make it shorter still would be to use the LED blink interval (100 milliseconds) as the time interval towards the total LED blink time.
  23.       That way you wouldn't need the button debounce timer code, or the 5-minute blink cycle timer code. You would just start blinking the LED at 100ms
  24.       and count the individual LED blink changes until it reached 5 minutes, and then you would stop the LED from blinking and re-enable the button.
  25.  
  26.   There are other ways to write the same program even shorter still, but I don't advise that unless you need it (for a small-memory microcontroller).
  27.   In general, you are better off making your code longer and more-understandable than you are making it short and difficult to understand or modify.
  28.  
  29. */
  30.  
  31. // --------> Use long, descriptive variable names, always. Make them understandable.
  32. // --------> Ideally you should never need to look up what a variable is for; it should be obvious from the name.
  33. 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.
  34. int button1_pin_state = 0; // This is to store the state of the button pin when read.
  35. // --------> Name your pins for what they do, it saves you lots of trouble later on.
  36. // --------> A variable for a pin NUMBER should end in "_pin"
  37. // --------> a variable to hold a pin STATE should end in "_pin_state"
  38.  
  39. bool button1_enabled = true; // This is to block checking the button when it is disabled due to the debounce timer.
  40. int button1_debounce_time = 300; // This is the milliseconds time to debounce button1.
  41. unsigned long button1_debounce_begin_time = 0; // Used for millis() values.
  42. unsigned long button1_debounce_current_time = 0; // Used for millis() values.
  43.  
  44. int led_pin = 13; // This is the pin that the LED is on.
  45. int led_pin_state = 0; // This is the state to write to the LED pin (either HIGH or LOW).
  46.  
  47. bool led_timer_active = false;
  48. 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.
  49. int led_blink__seconds_counter = 0; // This variable is used to count up to the blink_cycle_time limit.
  50. unsigned long led_cycle_timer__previous_time = 0; // Used for millis() values.
  51. unsigned long led_cycle_timer__current_time = 0; // Used for millis() values.
  52.  
  53. int blinker_time_interval = 100; // This is the time for the LED to blink on and off.
  54. unsigned long blinker_timer__previous_time = 0; // Used for millis() values.
  55. unsigned long blinker_timer__current_time = 0; // Used for millis() values.
  56.  
  57.  
  58.  
  59.  
  60. void setup() {
  61.   Serial.begin(9600);
  62.   pinMode(button1_pin, INPUT_PULLUP);
  63.   pinMode(led_pin, OUTPUT);
  64.   digitalWrite(led_pin, 0); // Some clone Unos write pin #13 HIGH unless you tell them to put it low.
  65.   Serial.println("Exiting setup.");
  66. }
  67.  
  68.  
  69. void loop() {
  70.  
  71.   if (button1_enabled == true) {
  72.     if (led_timer_active == false) {
  73.       button1_pin_state = digitalRead(button1_pin);
  74.       if (button1_pin_state == LOW) {
  75.         Serial.println("Button1 pressed.");
  76.         disable_button1();
  77.         start_LED_blinker();
  78.       }
  79.     }
  80.   }
  81.  
  82.   if (button1_enabled == false) {
  83.     check_button1_debounce_timer();
  84.   }
  85.  
  86.   if (led_timer_active == true) {
  87.     check_LED_timer(); // This is the function that checks the 5-minute LED run time.
  88.   }
  89.  
  90.   if (led_timer_active == true) {
  91.     check_LED_blinker(); // This is the function that blinks the LED on and off every 100ms.
  92.     // Since the LED doesn't blink at all unless the LED timer is on, you can use the led_timer_active variable
  93.     //      to control both the LED timer and the LED blinker.
  94.   }
  95. } // end of main loop
  96.  
  97. // It is a good habit to learn, to break your program up into functions.
  98. // Each function should do (basically) only one thing.
  99. // Each function should be named descriptively for whatever it does.
  100. // Using lots of functions, or long function names, or long variable names does not make your program compile any longer.
  101.  
  102.  
  103. void disable_button1() {
  104.   Serial.println("Button1 disabled.");
  105.   button1_enabled = false;
  106.   button1_debounce_begin_time = millis();
  107. }
  108.  
  109.  
  110. void check_button1_debounce_timer() {
  111.   button1_debounce_current_time = millis();
  112.   if (button1_debounce_current_time >= button1_debounce_begin_time) {
  113.     if (button1_debounce_current_time >= (button1_debounce_begin_time + button1_debounce_time)) {
  114.       button1_enabled = true;
  115.       Serial.println("Button1 re-enabled.");
  116.     }
  117.   }
  118.   else {
  119.     button1_debounce_begin_time = millis();
  120.   }
  121. }
  122.  
  123.  
  124. void start_LED_blinker() {
  125.   led_timer_active = true;
  126.   led_pin_state = 1;
  127.   digitalWrite(led_pin, led_pin_state);
  128.   led_cycle_timer__previous_time = millis();
  129.   blinker_timer__previous_time = millis();
  130.   led_blink__seconds_counter = 0;
  131.   Serial.println("Starting LED blinker.");
  132. }
  133.  
  134.  
  135. void check_LED_timer() {
  136.   led_cycle_timer__current_time = millis();
  137.   if (led_cycle_timer__current_time >= led_cycle_timer__previous_time) {
  138.     if (led_cycle_timer__current_time >= (led_cycle_timer__previous_time + 1000)) {
  139.       led_blink__seconds_counter++;
  140.       if (led_blink__seconds_counter >= led_blink_cycle_time_in_seconds) {
  141.         led_timer_active = false;
  142.         // Turn the LED off so it doesn't stay on.
  143.         digitalWrite(led_pin, LOW);
  144.         Serial.println("Stopping LED blinker.");
  145.       }
  146.       led_cycle_timer__previous_time = millis();
  147.     }
  148.   }
  149.   else {
  150.     led_cycle_timer__previous_time = millis();
  151.   }
  152. }
  153.  
  154.  
  155. void check_LED_blinker() {
  156.   blinker_timer__current_time = millis();
  157.   if (blinker_timer__current_time >= blinker_timer__previous_time) {
  158.     if (blinker_timer__current_time >= (blinker_timer__previous_time + blinker_time_interval)) {
  159.       led_pin_state = flip_this_pin_state(led_pin_state);
  160.       digitalWrite(led_pin, led_pin_state);
  161.       blinker_timer__previous_time = millis();
  162.     }
  163.   }
  164.   else {
  165.     blinker_timer__previous_time = millis();
  166.   }
  167. }
  168.  
  169.  
  170. int flip_this_pin_state(int this_pin) {
  171.   if (this_pin == 0) {
  172.     this_pin = 1;
  173.   }
  174.   else {
  175.     this_pin = 0;
  176.   }
  177.   return this_pin;
  178. }
  179.  
  180. // ##################### old code below #############################
  181. /*
  182.   // led blinks during 5 minutes
  183.   unsigned long starttime = 0;
  184.   void setup() {
  185.   pinMode(13, OUTPUT);
  186.   }
  187.  
  188.   void loop() {
  189.   if (millis() - starttime <= 300000) {
  190.     digitalWrite(13, HIGH);
  191.     delay(100);
  192.     digitalWrite(13, LOW);
  193.     delay(100);
  194.   }
  195.   }
  196.   // $$$$$$$$$$$$$$$$$$$$$$$$$$
  197.  
  198.   // push button turns led on
  199.   const int pinon = 2;
  200.   const int pinled = 13;
  201.   int ison = HIGH;
  202.  
  203.   void setup() {
  204.  
  205.   pinMode(pinon, INPUT);
  206.   pinMode(pinled, OUTPUT);
  207.   }
  208.   void loop() {
  209.  
  210.   ison = digitalRead(pinon);
  211.  
  212.   if (ison == LOW) {
  213.     delay(50);
  214.     if (ison == LOW ) {
  215.       digitalWrite(pinled, HIGH);
  216.     }
  217.   }
  218.   }
  219. */
  220.  
  221. // ~~~~~~~ the end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment