Hi everyone.
I need a help.
I want to start action under the IF statement when the time reach the set point.
But the program never start it. Because it always miss it.
Can I somehow write in to the code that:
Before the IF statement stop everything else, just wait for set time and then do the IF statement
const int ledPin = 13;
unsigned long ontime = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
ontime = 10000;
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(millis());
if (millis() == ontime){
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
}
after completion is still valid millis() > ontime and starts the IF again.
So, flag the fact that the action has happened and don't do it again or if that is really all the Arduino is required to do use delay() to cause the 10 second pause and put the whole code in setup() so that it runs once.
So, flag the fact that the action has happened and don't do it again or if that is really all the Arduino is required to do use delay() to cause the 10 second pause and put the whole code in setup() so that it runs once.
The code that I put here, it is just an example. The whole program is much more complicated, I just tried to show off what the problem is.
Sorry guys, I made a mistake .
I try GypsumFantastics code on example code is work just fine. But in more complex codes it's not work because the microcontroler has too many things to do that the delay is too large.
I have to use like UKHeliBob sad "Teach the OP to fish" code.
What if I well understand means the program will wait for the right moment and then do action.
I have to use like UKHeliBob sad "Teach the OP to fish" code.
What if I well understand means the program will wait for the right moment and then do action.
I am afraid that what I posted may have been lost in translation.
What I meant was, that if a working solution is posted then your immediate needs may be satisfied but you may not have learned anything, ie you have been "given a fish" but if suggestions as to how to do something are posted and you code it for yourself then you have learned something that will be useful for ever, like "learning to fish"
Look at the thread that Robin linked to and you will learn a lot about timing without blocking code.
GypsumFantastic:
The delay() was in the OP's code. I just added the allDone flag.
So, without the OP's code, it is impossible to have known that leaving in that delay may have had consequences... but, wouldn't it be better to demonstrate the non-blocking method to change the state back... since you took the time to write edit the function?