Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit/Arduino --- September 17, 2020
- Button with 5-minute LED blinker ver 2.0
- Board: Uno/Leonardo, Mega (needs a button @ input_pullup and the pin#13 LED)
- ----------------------------------------
- Two different LED blink times added: time=on and time=off are separate values.
- */
- 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.
- 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_on = 200; // This is the milliseconds time for the LED to stay on when it's blinking.
- int blinker_time_interval_off = 800; // This is the milliseconds time for the LED to stay off when it's blinking.
- int this_blink_interval = 0; // This is a variable to hold one of the above values in use.
- 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
- 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) {
- // Below switches between the two led blink inverval times (on/off)
- this_blink_interval = blinker_time_interval_off; // This sets it to the 'off' time by default.
- if (led_pin_state == 1) { // This changes it to the 'on' time if the LED is currently HIGH.
- this_blink_interval = blinker_time_interval_on;
- }
- if (blinker_timer__current_time >= (blinker_timer__previous_time + this_blink_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;
- }
- // ~~~~~~~ the end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment