Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit/Arduino --- September 17, 2020
- Button with 5-minute LED blinker
- Board: Uno/Leonardo, Mega (needs a button @ input_pullup and the pin#13 LED)
- ----------------------------------------
- There is usually more than one way to write a sketch.
- This sketch does not use delay() at all.
- Instead of delay(), this sketch uses three separate software timers that run at the same time, to show how that can be done.
- 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.
- This sketch uses a software timer to debounce the button (300 milliseconds), another software timer to time the LED blink time (5 minutes),
- and a third software timer to blink the LED on and off (every 100 milliseconds). This was done just to show how all three
- things can be done at the same time.
- If you wanted a shorter sketch, one way you could shorten it is to prevent button inputs when the LED blink time is running.
- That way the button would have a 5-minute debounce time, and you wouldn't need the software timer for debouncing the button.
- 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.
- 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
- 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.
- 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).
- 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.
- */
- // --------> Use long, descriptive variable names, always. Make them understandable.
- // --------> Ideally you should never need to look up what a variable is for; it should be obvious from the name.
- 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.
- int button1_pin_state = 0; // This is to store the state of the button pin when read.
- // --------> Name your pins for what they do, it saves you lots of trouble later on.
- // --------> A variable for a pin NUMBER should end in "_pin"
- // --------> a variable to hold a pin STATE should end in "_pin_state"
- bool button1_enabled = true; // This is to block checking the button when it is disabled due to the debounce timer.
- int button1_debounce_time = 300; // This is the milliseconds time to debounce button1.
- unsigned long button1_debounce_begin_time = 0; // Used for millis() values.
- unsigned long button1_debounce_current_time = 0; // Used for millis() values.
- int led_pin = 13; // This is the pin that the LED is on.
- int led_pin_state = 0; // This is the state to write to the LED pin (either HIGH or LOW).
- bool led_timer_active = false;
- 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.
- int led_blink__seconds_counter = 0; // This variable is used to count up to the blink_cycle_time limit.
- unsigned long led_cycle_timer__previous_time = 0; // Used for millis() values.
- unsigned long led_cycle_timer__current_time = 0; // Used for millis() values.
- int blinker_time_interval = 100; // This is the time for the LED to blink on and off.
- unsigned long blinker_timer__previous_time = 0; // Used for millis() values.
- unsigned long blinker_timer__current_time = 0; // Used for millis() values.
- void setup() {
- Serial.begin(9600);
- pinMode(button1_pin, INPUT_PULLUP);
- pinMode(led_pin, OUTPUT);
- digitalWrite(led_pin, 0); // Some clone Unos write pin #13 HIGH unless you tell them to put it low.
- Serial.println("Exiting setup.");
- }
- void loop() {
- if (button1_enabled == true) {
- if (led_timer_active == false) {
- button1_pin_state = digitalRead(button1_pin);
- if (button1_pin_state == LOW) {
- Serial.println("Button1 pressed.");
- disable_button1();
- start_LED_blinker();
- }
- }
- }
- if (button1_enabled == false) {
- check_button1_debounce_timer();
- }
- if (led_timer_active == true) {
- check_LED_timer(); // This is the function that checks the 5-minute LED run time.
- }
- if (led_timer_active == true) {
- check_LED_blinker(); // This is the function that blinks the LED on and off every 100ms.
- // Since the LED doesn't blink at all unless the LED timer is on, you can use the led_timer_active variable
- // to control both the LED timer and the LED blinker.
- }
- } // end of main loop
- // It is a good habit to learn, to break your program up into functions.
- // Each function should do (basically) only one thing.
- // Each function should be named descriptively for whatever it does.
- // Using lots of functions, or long function names, or long variable names does not make your program compile any longer.
- void disable_button1() {
- Serial.println("Button1 disabled.");
- button1_enabled = false;
- button1_debounce_begin_time = millis();
- }
- void check_button1_debounce_timer() {
- button1_debounce_current_time = millis();
- if (button1_debounce_current_time >= button1_debounce_begin_time) {
- if (button1_debounce_current_time >= (button1_debounce_begin_time + button1_debounce_time)) {
- button1_enabled = true;
- Serial.println("Button1 re-enabled.");
- }
- }
- else {
- button1_debounce_begin_time = millis();
- }
- }
- void start_LED_blinker() {
- led_timer_active = true;
- led_pin_state = 1;
- digitalWrite(led_pin, led_pin_state);
- led_cycle_timer__previous_time = millis();
- blinker_timer__previous_time = millis();
- led_blink__seconds_counter = 0;
- Serial.println("Starting LED blinker.");
- }
- void check_LED_timer() {
- led_cycle_timer__current_time = millis();
- if (led_cycle_timer__current_time >= led_cycle_timer__previous_time) {
- if (led_cycle_timer__current_time >= (led_cycle_timer__previous_time + 1000)) {
- led_blink__seconds_counter++;
- if (led_blink__seconds_counter >= led_blink_cycle_time_in_seconds) {
- led_timer_active = false;
- // Turn the LED off so it doesn't stay on.
- digitalWrite(led_pin, LOW);
- Serial.println("Stopping LED blinker.");
- }
- led_cycle_timer__previous_time = millis();
- }
- }
- else {
- led_cycle_timer__previous_time = millis();
- }
- }
- void check_LED_blinker() {
- blinker_timer__current_time = millis();
- if (blinker_timer__current_time >= blinker_timer__previous_time) {
- if (blinker_timer__current_time >= (blinker_timer__previous_time + blinker_time_interval)) {
- led_pin_state = flip_this_pin_state(led_pin_state);
- digitalWrite(led_pin, led_pin_state);
- blinker_timer__previous_time = millis();
- }
- }
- else {
- blinker_timer__previous_time = millis();
- }
- }
- int flip_this_pin_state(int this_pin) {
- if (this_pin == 0) {
- this_pin = 1;
- }
- else {
- this_pin = 0;
- }
- return this_pin;
- }
- // ##################### old code below #############################
- /*
- // led blinks during 5 minutes
- unsigned long starttime = 0;
- void setup() {
- pinMode(13, OUTPUT);
- }
- void loop() {
- if (millis() - starttime <= 300000) {
- digitalWrite(13, HIGH);
- delay(100);
- digitalWrite(13, LOW);
- delay(100);
- }
- }
- // $$$$$$$$$$$$$$$$$$$$$$$$$$
- // push button turns led on
- const int pinon = 2;
- const int pinled = 13;
- int ison = HIGH;
- void setup() {
- pinMode(pinon, INPUT);
- pinMode(pinled, OUTPUT);
- }
- void loop() {
- ison = digitalRead(pinon);
- if (ison == LOW) {
- delay(50);
- if (ison == LOW ) {
- digitalWrite(pinled, HIGH);
- }
- }
- }
- */
- // ~~~~~~~ the end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment