MrAlvin

ON for a time (60 sec) when button pushed - ver 1

Jan 2nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. /*
  2.   This is a combination of the examples
  3.   - "Button" and
  4.   - "Blink without Delay"
  5.  
  6.  
  7.   Function of the sketch:
  8.  
  9.   Turns a light emitting diode(LED) connected to digital pin 13,
  10.   ON for a time interval (here 60 seconds),
  11.   when pressing a pushbutton attached to pin 2 (or pulling pin 2 HIGH).
  12.  
  13.   The time is from 60 seconds from the release of the button,
  14.   and the time will be extended if the button is pressed
  15.   before the 60 seconds is up.
  16.  
  17.   Because we combine with the method from "Blink without Delay"
  18.   other code can run at the same time without
  19.   being interrupted/stopped by the timeout code needed to
  20.   turn the LED on and off.
  21.  
  22.  
  23.   The circuit:
  24.  
  25.   - LED attached from pin 13 to ground
  26.   - pushbutton attached to pin 2 from +5V
  27.   - 10K resistor attached to pin 2 from ground
  28.  
  29.  
  30.   - Note: on most Arduinos there is already an LED on the board
  31.     attached to pin 13.
  32.  
  33.   - Note 2: The pushbutton is connected to be active on a HIGH of pin 2.
  34.  
  35. */
  36.  
  37. //// Button/input constants and variables  
  38. // constants won't change. They're used here to set pin numbers:
  39. const int buttonPin = 2;     // the number of the pushbutton pin
  40. const int ledPin =  13;      // the number of the LED pin
  41.  
  42. // variables will change:
  43. int buttonState = 0;         // variable for reading the pushbutton status
  44.  
  45.  
  46. //// Timeout constants and variables  
  47. // constants won't change.
  48. const long interval = 60000;             // interval (in milliseconds) to keep the LED ON for (60 seconds)
  49.  
  50. // variables will change:
  51. unsigned long previousMillis = 0;        // will store last time the LED was turned ON
  52.  
  53.  
  54.  
  55. void setup() {
  56.   // initialize the LED pin as an output:
  57.   pinMode(ledPin, OUTPUT);
  58.   // initialize the pushbutton pin as an input:
  59.   pinMode(buttonPin, INPUT);
  60. }
  61.  
  62.  
  63. void loop() {
  64. // check to see if it's time to blink the LED (or to turn the LED on or off) ;
  65.   // that is, if the difference between the current time
  66.   // and last time you blinked the LED (or turned the LED on) is bigger than
  67.   // the interval at which you want to blink the LED (turn the LED off).
  68.   unsigned long currentMillis = millis();
  69.  
  70.   // read the state of the pushbutton value:
  71.   buttonState = digitalRead(buttonPin);
  72.  
  73.  
  74.   // check if the pushbutton is pressed (or the pin is HIGH).
  75.   // If it is, the buttonState is HIGH:
  76.   if (buttonState == HIGH) {
  77.    
  78.     // turn LED on:
  79.     digitalWrite(ledPin, HIGH);
  80.    
  81.     // save the start of the press/activation time
  82.     previousMillis = currentMillis;
  83.   }
  84.  
  85.  
  86.   // check to see if there is a timeout condition
  87.    if (currentMillis - previousMillis >= interval) {
  88.    
  89.     // turn LED off:
  90.     digitalWrite(ledPin, LOW);
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment