millis doesn't seem to be doing what I want

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

Variables involving millis should be unsigned long. You should calculate time by subtraction and not addition to avoid issues at rollover.

You should post the whole program and describe what it is supposed to do and what it actually does. The error may not be where you think it is.

It would really help to read the documentation on millis().

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?

The demo Several Things at a Time illustrates the use of millis() to manage timing.

This

if (millis()>ml+120000)

should be

if (millis() - ml > 120000)

...R

Robin2:
The demo Several Things at a Time illustrates the use of millis() to manage timing.

This

if (millis()>ml+120000)

should be

if (millis() - ml > 120000)

...R

sp. "if (millis() - ml >= 120000)"

AWOL:
sp. "if (millis() - ml >= 120000)"

I was going to do that but I changed my mind as it is not what the OP had.

I don't think it makes much difference. The important thing is not to use

if (millis() - ml == 120000)

because it is too precise and might miss something.

...R