Break a loop and start timer when condition is true

Hi,

so basically im trying to write a program where i have a light sensor detecting light and some servo motors running back and forth while the LDR senses a certain brightness, as soon as the brightness drops i want to start a timer and run another few tests and doing differnt opperations depending on the time the brightness was dropped…
so i have all my funtions working the only issue im having is that i need the timer to start timing as soon as the LDR drops.

basically i have this:

if (LIGHTIN > LIGHTCUTOFF)
{

/* SERVO LOOP */
servo1.write(65);
delay(500);
servo1.write(90);
delay(800);
servo2.write(72);
delay(500);
servo2.write(90);
delay(800);
}
else if (LIGHTIN < LIGHTCUTOFF)
{
INITIALTIME = millis();
while (LIGHTIN < LIGHTCUTOFF)
{
LIGHTIN = analogRead(ANALOGIN);
delay(10);
TIMEBLACK = 0;

}
TIMEBLACK= millis()-INITIALTIME;
Serial.println (TIMEBLACK);

This works except my only problem being if the brightness drops in the middle of the servo loop the timer doesnt start until that loop is done which can lose a few seconds to my calculated time, i need the TIMEBLACK to be more acurate and start as soon as it drops… doesnt necessarily need to stop the servo loop, that can finish or stop, either way, i just need the time to start as soon as it drops out

ive tried a few else amd else ifs and while statements, just cant seem to get it to do what i want, Im sure its simple i just dont have alot of experience in these

thanks
Rohan

rohan85: This works except my only problem being if the brightness drops in the middle of the servo loop the timer doesnt start until that loop is done which can lose a few seconds to my calculated time, i need the TIMEBLACK to be more acurate and start as soon as it drops... doesnt necessarily need to stop the servo loop, that can finish or stop, either way, i just need the time to start as soon as it drops out

you need to develop a non-blocking servo sweep.

Have a look at how millis() is used to manage timing without blocking in Several things at a time.

The example includes movement of a servo so should be fairly close to what you need.

…R

Robin2: Have a look at how millis() is used to manage timing without blocking in Several things at a time.

The example includes movement of a servo so should be fairly close to what you need.

...R

Ok thanks heaps Robin2 I'll have a play with that code tonight

Rohan

I am new here, so quite prepared to be advised differently by the many more experienced people here....

I'm also very new to using simpletimer (well BlynkTimer in my case but almost the same), but couldn't the countdown timer be done using the timer.setTimout ?

so when the value of the LDR is below a certain value (if statement) then timer.setTimout(countdownduration, task)

where task() is a function that runs what you want to do when the light level drops but only runs countdownduration milliseconds after the line above is executed. Keeping the rest of the code running while the countdown occurs.

I have been using delay() in the past but have now started using the simpletimer "timer.setTimeout" wherever possible so the code doesn't get held up as I got rid of the delay() functions... mostly.

If you haven't been using simpletimer (or BlynkTimer) I would advise reading up about it. Particularly the timer.interval (run a function every so many millis() ) and timer.setTimeout (run a function only once, but wait a certain amount of millis before running it = countdown). Using these means the program keeps running and monitoring for changes on the digital pins while the countdown is happening. :)

IMHO using millis() is the simplest way to manage time and, because there is nothing hidden in a library it is easy to debug when things don't work as expected.

...R

Thanks guys.

I used Millis. The servos are moving to hold down two different buttons so a sweep wasn’t going to work as I want it to (need them to move to a position. Hold there for a period and then swap). I used basically the flashing led type code except changed the servo positions instead of turning leds on and off.

Rohan