What you need
hook up
Upload the below code to your UNO. I have set 10 seconds represented by interval variable which in code is showing set to 10000 milli seconds.
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 |
//Constants const int buttonPin = 3; const int ledPin = 7; //Variables int buttonState = 0; int flag = 0; // the interval in mS unsigned long interval = 10000; // the time we need to wait to finish watering the plant. unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop() unsigned long previousMillis = 0; // millis() returns an unsigned long. void setup() { //Input or output? pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } void loop() { currentMillis = millis(); // capture the latest value of millis() //Read button state (pressed or not pressed?) buttonState = digitalRead(buttonPin); //If button pressed... if (buttonState == LOW) { //...ones, turn led on! if ( flag == 0) { digitalWrite(ledPin, HIGH); flag = 1; //change flag variable } //...twice, turn led off! else if ( flag == 1) { digitalWrite(ledPin, LOW); flag = 0; //change flag variable again } } if ((unsigned long)(currentMillis - previousMillis) >= interval) { flag = 0; //change flag variable again digitalWrite(ledPin, LOW); // save the "current" time previousMillis = millis(); } } |