Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Turn OFF a LED, for some time (interval), when a button is pushed
- // - wire Arduino pin to button
- // - wire other side of button to ground/minus
- // so a push of the button will pull Arduino pin LOW
- const int pushButton = 7;
- const int LedPin = 6;
- int buttonState = HIGH; //power-on state - when button is NOT pushed
- int ledState = HIGH; //power-on state - LED is ON
- //timing variables
- unsigned long holdMillis = 0; // save the time when the button was pushed
- unsigned long interval = 5000; // save the LED OFF interval - default is 5 seconds
- // 10 minutes is: (10 min x 60 seconds x 1000 milli seconds = 600000)
- void setup() {
- pinMode(pushButton, INPUT_PULLUP);
- pinMode(LedPin, OUTPUT);
- //turn ON LED
- digitalWrite(LedPin, ledState);
- } //end - setup()
- void loop() {
- //if LED is ON, check button
- if (ledState == HIGH) {
- //read button state
- buttonState = digitalRead(pushButton);
- //if button is pushed (is LOW) then take action
- if (buttonState == LOW) {
- //turn OFF LED
- ledState = LOW;
- //remember the current time
- holdMillis = millis();
- }
- }else{
- // LED is OFF - so check to see if time has passed
- //has the time passed
- if ( millis() - holdMillis >= interval ) {
- //then turn ON LED
- ledState = HIGH;
- }
- } // end - if(ledSTATE)
- //update LED pin
- digitalWrite(LedPin, ledState);
- } //end - loop()
Add Comment
Please, Sign In to add comment