We already know that we can blink one LED with Arduino and also we can blink one LED with 555 IC. We can also blink 2 LEDs alternatively with Arduino and also blink 2 LEDs alternatively with 555 IC. From our older examples, we can start LED to be on with one pushbutton press and turn-off with another pushbutton keypress. Arduino Blink LED With Pushbutton Control to Turn ON and Off is Few Steps Higher Than Basic Example. There is Matter of Repeat Checking by Microcontroller. It is not exactly easy to a beginner but mandatory next step to learn.
Arduino Blink LED With Pushbutton Control to Turn ON and Off
Wiring/circuit diagram of this project is very easy. One digital pin of Arduino will be connected to LED, LED’s another leg will be connected to GND of Arduino. Alternatively, the onboard LED on Arduino board can be used. Connection for pushbutton is easy too – one digital pin of Arduino will be connected to pushbutton, pushbutton’s another leg will be connected to GND of Arduino with a resistor with value like 1K Ohm to ensure not much current going back to Arduino board.
Very basic code will be :
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | const int kPinBtn = 2; const int kPinLed = 13; boolean kPress = false; void setup() { pinMode(kPinBtn, INPUT_PULLUP); pinMode(kPinLed, OUTPUT); } void loop() { if(digitalRead(kPinBtn) == LOW && kPress == false) { digitalWrite(kPinLed, HIGH); kPress = true; } if(digitalRead(kPinBtn) == LOW && kPress == true) { digitalWrite(kPinLed, LOW); kPress = false; } } |
You will notice the odd with a delay. If we want to achieve is to have a button alternate between blink and off smoothly, that can be done in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | enum { LED=13, ButtonPin=2, BounceMS=50, BlinkMS=256 }; unsigned long buttonAt; bool blinky, bouncy, buttonState; void setup() { pinMode(LED, OUTPUT); pinMode(ButtonPin, INPUT_PULLUP); blinky = bouncy = buttonState = false; buttonAt = millis(); } void loop() { // Detect button changes if (bouncy) { if (millis() - buttonAt > BounceMS) bouncy = false; // End of debounce period } else { if (digitalRead(ButtonPin) != buttonState) { buttonState = digitalRead(ButtonPin); buttonAt = millis(); bouncy = true; // Start debounce period if (buttonState) { // Was button just pressed? blinky = !blinky; // Toggle blink-state } } } // Control light-blinking if (blinky) { // Turn LED on for BlinkMS ms, then off for same digitalWrite(LED, (millis() - buttonAt)%(2*BlinkMS) < BlinkMS); } else { digitalWrite(LED, LOW); } } |
We can not directly use easy logic to start and stop. That will leads to a very long time between successive checks of the button as there is a delay between cycles! That invites some other functions to use, like using the millis function. Notice some changes in connection in this diagram :
Another code with millis :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /* Pushbutton-controlled blinking LED * David A. Mellis * http://arduino.berlios.de */ int ledPin = 13; // LED connected to digital pin 13 int ledValue = LOW; // previous value of the LED long ledStartTime = 0; // will store last time LED was updated long ledBlinkInterval = 1000; // interval at which to blink (milliseconds) int buttonPin = 2; // an active-high momentary pushbutton on pin 2 int buttonValue = HIGH; // the current state of the button long buttonPressTime = 0; // will store the time that the button was pressed void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() { // Check to see if the button is pressed. If so, mark the time. When the // button is released, calculate how long it was pressed, and make the led // blink at the same rate. buttonValue = digitalRead(buttonPin); // button press if (buttonValue==LOW && buttonPressTime==0) { buttonPressTime = millis(); // capture the time of the press } // button release if (buttonValue==HIGH && buttonPressTime!=0) { ledBlinkInterval = millis() - buttonPressTime; // set the new flash interval buttonPressTime = 0; // clear the button press time } // check to see if it's time to blink the LED; that is, is the difference // between the current time and last time we blinked the LED bigger than // the interval at which we want to blink the LED. if (millis() - ledStartTime > ledBlinkInterval) { ledStartTime = millis(); // remember the last time we blinked the LED // if the LED is off turn it on and vice-versa. if (ledValue == LOW) ledValue = HIGH; else ledValue = LOW; digitalWrite(ledPin, ledValue); } } |
We can use timer library :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include "Timer.h" // Pin 13 has a LED connected on most Arduino boards. // give it a name: const int led = 13; const int buttonPin = 2; // declare state variables as global bool buttonState = LOW; bool buttonState_prev = LOW; bool toggleBlinking; bool blinkState; // declare a Timer object so blinking can be done without loosing button presses Timer timer_b; int blink_id; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); // initial state: toggle led each 1000 ms blink_id = timer_b.oscillate(led, 1000, HIGH); blinkState = true; toggleBlinking = false; } // the loop routine runs over and over again forever: void loop() { // Update the timer (required) timer_b.update(); // check if button has been pressed (HIGH to LOW), // then debounce it and raise toggle flag buttonState = digitalRead(buttonPin); if ((buttonState != buttonState_prev) && (buttonState_prev == HIGH)) { // simple button debounce (confirm press after some ms) delay (50); buttonState = digitalRead(buttonPin); if (buttonState != buttonState_prev) toggleBlinking = true; } // keep current LED state unless the button has been pushed switch (blinkState) { case true: // if button has been pushed, stop blinking and change LED state if (toggleBlinking == true) { timer_b.stop (blink_id); blinkState = false; } break; case false: digitalWrite(led, LOW); // if button has been pushed, start blinking and change LED state if (toggleBlinking == true) { blink_id = timer_b.oscillate(led, 1000, HIGH); blinkState = true; } break; } buttonState_prev = buttonState; toggleBlinking = false; } |
This ends this tutorial.
Tagged With arduino led on off button code , arduino blink , arduino led blink with button , arduino led blink , arduino with LED and push button , arduino blink with button , arduino keypress , arduino control led with pushbutton , arduino pushbutton led , светодиод управляемый кнопкой arduino