Having a challenge with millis. I want to run a set of code every 2 minutes and only run for 30 sec max when conditions are right.
ml is the millis counter
left and right are analog readings of small solar cells
leftPin makes the action occur
long ml=0;
then the code:
if (millis()>ml+120000)
{ ml=millis();
while ((left > 600 || right > 600)&&((left-right)>20) && (millis()<(ml+30000)))
{digitalWrite(leftPin, LOW);
left = analogRead(analogPin2);
right = analogRead(analogPin3);}
}
anything with the millis stand out as incorrect?
Thanks in advance.
Harry
I want to run a set of code every 2 minutes and only run for 30 sec max when conditions are right.
Sounds so straightforward! But specifying things is actually really hard:
Do you always want to run for the full 30 seconds, or do you want to stop running as soon as the conditions are not right any more?
Do you want to start 2 minutes from the start of when you last ran the set of code, or 1:30 from the time that it finished? (this becomes relevant if you do not always run for the full 30 sec)
If the conditions are not right at the start of a 2-minute mark, do you want to start as soon as conditions become right, or do you want to check only on the 2 minute boundary?
If when conditions are not right at the two minute mark you want to start when conditions become right, do you want to stop 30 seconds after the conditions became right, or do you want to stop 30 seconds after that 2 minute mark? And when you you want to start again? 2 minutes from the time conditions became right, two minutes from the time you started checking, or 1:30 from the time you finished?
And then you have the issue of noise and bounce.
If conditions cease being right before the end of the 30 seconds, should we stop only if conditions cease being right and stay not being right for more than a second?
For that matter, if we are not starting unless conditions are right, should we start only if conditions are right and have remained right for at least second prior?