MrAlvin

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

Jan 2nd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. /*
  2.   This is a combination of the examples
  3.   - "Button" and
  4.   - "Blink without Delay" and
  5.   - a state-machine
  6.  
  7.  
  8.   Function of the sketch:
  9.  
  10.   Turns a light emitting diode(LED) connected to digital pin 13,
  11.   ON for a time interval (here 60 seconds),
  12.   when pressing a pushbutton attached to pin 2 (or pulling pin 2 HIGH).
  13.  
  14.   The time is 60 seconds from the button is pushed.
  15.   A new ON period can be activated only after the 60 seconds have passed.
  16.  
  17.   Because we use the method from "Blink without Delay"
  18.   other code can run at the same time without
  19.   being interrupted/stopped by the timeout 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.
  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. int state_idx = 0;           // variable indication current state of state-machine
  45.  
  46.  
  47. //// Timeout constants and variables  
  48. // constants won't change.
  49. const long interval = 60000;             // interval (in milliseconds) to keep the LED ON for (60 seconds)
  50.  
  51. // variables will change:
  52. unsigned long previousMillis = 0;        // will store last time the LED was turned ON
  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.  
  64. void loop() {
  65.   // check to see if it's time to blink the LED (or to turn the LED on or off) ;
  66.   // that is, if the difference between the current time
  67.   // and last time you blinked the LED (or turned the LED on) is bigger than
  68.   // the interval at which you want to blink the LED (turn the LED off).
  69.   unsigned long currentMillis = millis();
  70.  
  71.   // state-machine
  72.   switch(state_idx) {
  73.    
  74.     case 0: // read button / input pin
  75.       // read the state of the pushbutton value:
  76.       buttonState = digitalRead(buttonPin);
  77.      
  78.       // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  79.       if (buttonState == HIGH) {
  80.        
  81.         // remember the start time
  82.         previousMillis = currentMillis;
  83.        
  84.         // turn LED on:
  85.         digitalWrite(ledPin, HIGH);
  86.        
  87.         // and move into next state of state-machine -> to waite for the timeout to happen
  88.         state_idx = 1;
  89.        
  90.       } else {
  91.         // turn/make sure LED off:
  92.         digitalWrite(ledPin, LOW);
  93.       }
  94.       break;  // case 0 is done
  95.      
  96.     case 1:  // waite for some time - until interval has passed
  97.       if (currentMillis - previousMillis >= interval) {
  98.         // time to turn off the LED
  99.         digitalWrite(ledPin, LOW);
  100.  
  101.         // and move back to first state of state-machine -> read the button /input pin
  102.         state_idx = 0;
  103.        
  104.       } else {
  105.         // turn/make sure LED on:
  106.         digitalWrite(ledPin, HIGH);
  107.       }
  108.       break; // case 1 is done
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment