I have read that the delay functions work in the way that it stop all the reading(execution) of the code during the delay time .
I would like to know if there is another function doing the same as delay() but still execute the rest of the code or a way to isolate the delay() in the part of the program ?
Save the time from millis() that an event happens then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action.
Flammeur:
I would like to know if there is another function doing the same as delay() but still execute the rest of the code or a way to isolate the delay() in the part of the program ?
If you were posting in an automobile forum, your question would probably read like that:
"I would like to know if there is another pedal in my car doing the same as the brake pedal, but still I want the car going at full speed without slowing down?"
I'd say:
If you don't want to slow down your car, don't step on the brake pedal!
If you don't want to slow down your Arduino, don't use the delay() function!
Flammeur:
So if i get it right if i just need to replace delay() by millis()?
If you just replace delay() by millis() to avoid your Arduino slowing down is the same as replacing a step on the brake pedal of your car by a step on the foot of the co-driver to avoid slowing down the car.
What you have to replace is "the programming logic of your sketch" and not a single function call.
Did you already have a look at the example sketch you can load from the IDE menu:
File - Examples - 02.Digital - BlinkWithoutDelay
unsigned long startTime = 0;
const long interval = 1000; // 1 second = 1000 millis
bool timeFlag = false;
void setup()
{
}
void loop()
{
if (something you want to Time)
{
timeFlag = true;
startTime = millis(); // Begin timing sequence
// do stuff here when timing sequence starts
}
unsigned long currentTime = millis();
if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence
{
timeFlag = false;
// do stuff here when time is up
}
}
So now i have one part of the code using milli() but i have another section in the programs using the setTime(0) i would like to know if this variable will affect the part with milli() ?
I use a light sensor, if the light for 10s >500 the light turn on and then I use the setTime(0) to restart the counter I think it's depending of the time.h library not sure.
That why I would like to know if it restart all the counter or if the milli() work independently
Flammeur:
I use a light sensor, if the light for 10s >500 the light turn on and then I use the setTime(0) to restart the counter I think it's depending of the time.h library not sure.
That why I would like to know if it restart all the counter or if the milli() work independently
you don't need to restart a counter. Just take a sample of millis() when the light goes > 500, and use that number as your starting point to count 10 seconds also using millis().