I want motors to activate once, then stop, then restart once a condition is met

cdcoursey:
So I'm working on a project to open and close the curtains in my room when the sun rises or sets. I can get the motor to activate once the voltage from a PV cell exceeds a certain voltage (as it would as the sun rises). However, I can't figure out how to get it to stop looping once the voltage exceeds that critical value.

Basically, I need the motor to turn for a period of time once the board detects that the sun has risen, then stop and just wait until the voltage is below a certain voltage before it turns again to close the curtain.

I've tried if/else, do while, but I just can't figure out how to get those to do what I need to do.

Use a 'flag' variable. Eg. int flag = 0;
When you want the motor to move for the first time, then just move it as usual.....using this if statement...

if (flag == 0){
//move motor;
flag = 1;
}

You can also use other flags, or could use other values for this same flag (eg. flag = 2, flag = 3 etc) - for keeping track of what state your system is in. Eg.... if another sensor senses that light levels are really low enough, then set some flag so that your system can do something.

Maybe you have heard of it already .... this following word "hysteresis". If not, then read up on this one, as it can stop erratic on/off 'chatter'. Eg.... close curtain when the voltage level is greater or equal to 1.5 volt. Open curtain when the voltage level less than or equal to 0.8 volt. Basically....don't make the open and closing voltage equal to the same value.