Hi,
I've reada number of forum articles, tutorials, etc on millis, elapsed millis, timer but can't win.
What I'm trying to do is turn a Led on 10 minutes after the button has been pushed.
I can get it to work using a long delay, however am looking for a better way.
Is there a simple way to replace the delay line in my code?
Cheers,
Dylan
drdjw93:
I've reada number of forum articles, tutorials, etc on millis, elapsed millis, timer but can't win.
That means you didn't read enough articles on millis yet. Try to grasp the concept by rewriting the examples given there yourself. That's the only way you'll learn, asking for somebody else to provide the code for you is completely useless if you don't understand it.
Pieter
drdjw93:
Is there a simple way to replace the delay line in my code?
Not without seeing the code...and be aware that it will almost certainly involve millis().
But if all you want the Arduino to do is wait for 10 minutes then do something using a delay() is a perfectly reasonable and very simple way to do it. So I guess there's probably more to it than you've told us.
Steve
DVDdoug:
(It's best to use greater-than or less-than when comparing time because it's only equal once.)
Or never equal. The Arduino millis() function is allowed to skip a millisecond. Also your other code may take more than 1ms.
Thanks for the replies everyone.
I'm sorry I didn't add the code :S, I've amended it since to include my understanding of millis(), derived from m playing around with the code and progressively amending it however, when I add the condition of "if (TrolleyEndState == HIGH) {" the code "digitalWrite(OpenSolenoidPin, LOW);" doesn't work as intended.
const int TrolleyEndPin = 14;
const int OpenSolenoidPin = 13;
const int ResetPin = 16;
const int CloseSolenoidPin = 12;
int TrolleyEndState = 0;
int ResetState = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
pinMode(OpenSolenoidPin, OUTPUT);
pinMode(TrolleyEndPin, INPUT);
pinMode(CloseSolenoidPin, OUTPUT);
pinMode(ResetPin, INPUT);
}
void loop() {
TrolleyEndState = digitalRead(TrolleyEndPin);
ResetState = digitalRead(ResetPin);
if (TrolleyEndState == HIGH) {
unsigned long currentMillis = millis();
if (currentMillis >= 600000) {
digitalWrite(OpenSolenoidPin, LOW);
delay (2500);
digitalWrite(OpenSolenoidPin, HIGH);
}
} else {
digitalWrite(OpenSolenoidPin, HIGH);
delay(5);
if (ResetState == HIGH) {
digitalWrite(CloseSolenoidPin, LOW);
}else {
digitalWrite(CloseSolenoidPin, HIGH);
}
}
}
Read the how to use this forum sticky post and edit that post to comply with the requirements for posting code.
if (currentMillis >= 600000)
Is not the way you use the millis timer. You NEVER use the absolute value only a difference between some referance time and now.