Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Example to use a pushbutton to start or stop a blinking LED, using millis()
- More at : http://www.baldengineer.com/pushbutton-and-flashing-led-tutorial-with-millis.html
- */
- const byte button=2;
- const byte LED=10;
- bool blinking = false; //defines when blinking should occur
- unsigned long blinkInterval = 250; // number of milliseconds for blink
- unsigned long currentMillis; // variables to track millis()
- unsigned long previousMillis;
- void setup() {
- pinMode(button, INPUT);
- pinMode(LED, OUTPUT);
- }
- void loop() {
- // this code blinks the LED
- if (blinking) {
- currentMillis = millis(); // better to store in variable, for less jitter
- if ((unsigned long)(currentMillis - previousMillis) >= blinkInterval) { // enough time passed yet?
- digitalWrite(LED, !digitalRead(LED)); // shortcut to toggle the LED
- previousMillis = currentMillis; // sets the time we wait "from"
- }
- } else {
- digitalWrite(LED, LOW); // force LED off when not blinking
- }
- int reading = digitalRead(button);
- delay(50); // crude de-bouncing
- if (reading==LOW) // buttons with pull-up are pressed when LOW
- blinking=true; // start blinking
- else
- blinking=false; // stop blinking
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement