HI Guys,
Big Picture - I have a project running where I press a button widget and it turns on a solenoid that allows water to flow to water my plants. I want to have it turn off after 5 minutes.
I want to use mills() (and have done so in the past) but am really struggling with this seemingly simple process.
As a test setup I am using a simple Arduino Uno with an LED and Button (I press the button and the LAD should turn on for 3 seconds). I can do this easily with delay() - but have spent four hours trying to replicate with millis() - I am clearly missing something very, very basic - HELP! Please!
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
unsigned long currentMillis;
long previousMillis = 0;
long interval = 3000; // wait before turn off led
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if(buttonState == HIGH) // pressed button
{
digitalWrite(ledPin, HIGH); // turn on led
delay(3000);
//unsigned long currentMillis = millis(); // wait 3 secs
//if(currentMillis - previousMillis > interval)
//{
//previousMillis = currentMillis;
digitalWrite(ledPin, LOW); // then turn off led
//}
}
}
Once I get this working with millis() I’ll implement it into my existing Blynk watering project.
cul
billd
PS. Yes, I know this is not a strictly Blynk issue, but I get far more useful, helpful and meaningful response in this community than other other I’m apart of . . .and the delay()/millis() issue is core to keeping Blynk projects working well. Thnx