Whiles Ifs and Millis Oh My!

Continuing to have problems with my Airplane machine, This time i want the machine to know a piece of paper was not picked up. I have a sensor that is working for sensing the paper and one motor to pick up the paper and another to draw it into the first location.

various pins and variables setup snipped but thats not where the problem lies.

void loop(){
long startTime=millis()+1500;
Serial.println(startTime);
 //paraphrasing: start loadMotor
 //start feedMotor
 //loadMotor stops after one revolution, feedMotor continues, paper0Sw is the sensor

paper0SwSt=digitalRead(paper0Sw);
          
while(paper0SwSt==HIGH){
    paper0SwSt=digitalRead(paper0Sw);
    delay(1);
    long nowTime=millis();
    Serial.print(nowTime);
        Serial.print(", ");

    if (nowTime > startTime){          // if paper was not licked up, reset start time and reload paper
      Serial.println("GO");
      long timeStart=millis()+1500;
      Serial.print("new ST ");
      Serial.println(timeStart);
      reload();
    }
  }

void reload(){
//start load motor for one more revolution to try and grab another piece of paper.
}

The parts i have not included are working fine with out the time check and reload. The problem is it doesn’t pick up a piece of paper every time. It is already reading the paper sensor to trigger another motor for the next step. The Serial print out looks like this:
1935
3435
2170, 2171, 2172, 2174, [snipped]3421, 3427, 3433, 3439, GO
new start time 4948
3697, GO
new start time 5197
3924, GO
new start time 5424

The first time the if statement works correctly, then the startTime is reset but the if statement seems to be using the old startTime value when deciding to continue with the if statement.
I need it to read the paper sensor to start the next step or do something else when there is no paper. Is there a problem with nesting an If inside a while loop? Why would it not use the new startTime value?

Please Help!

Look at the name of the variable that you are testing in the if and compare it with the variable that you are re-setting to millis()+1500

UKHeliBob: Look at the name of the variable that you are testing in the if and compare it with the variable that you are re-setting to millis()+1500

thanks but no dice, but that was my typo here, not in the sketch.

here's the real code, well part of it, its a 5mb sketch all told.

void fold(){                            //**********************Fold**************
  Serial.println("Fold");
  digitalWrite(startYellowLed,HIGH); 
  Serial.println(millis());
  long startTime=millis()+1500;                    // set srart time var
  Serial.println(startTime);
  digitalWrite(feedMotorIn,HIGH);        //start motors
  digitalWrite(loadMotorIn,HIGH);
  int loadSwSt=digitalRead(loadSw);
  while(loadSwSt==HIGH){                  //read loader switch
      loadSwSt=digitalRead(loadSw);
      //delay(1);
  }
  while(loadSwSt==LOW){
      loadSwSt=digitalRead(loadSw);
      //delay(1);
  }
   delay(loadSwDelay);
  digitalWrite(loadMotorIn,LOW);          //stop loader motor
  digitalWrite(loadMotorOut,HIGH);
  delay(10);
  digitalWrite(loadMotorOut,LOW);
   int paper0SwSt=digitalRead(paper0Sw);      //read paper0 switch
  while(paper0SwSt==HIGH){
    paper0SwSt=digitalRead(paper0Sw);
    delay(1);
    long nowTime=millis();
    Serial.print(nowTime);
        Serial.print(", ");

    if (nowTime > startTime){          // if papaer was not licked up, reset start time and reload paper
      Serial.println("GO");
      long startTime=millis()+1500;
      Serial.print("new start time ");
      Serial.println(startTime);
      reload();
    }
  }
long timeStart=millis()+1500;
      Serial.print("new ST ");
      Serial.println(timeStart);

Rather limited scope there.

long startTime=millis()+1500;

startTime is the time to quit waiting for the paper sensor to register that it has contacted a sheet of paper.

When you start using reasonable names, I'll be inclined to read further. Though, I'm not sure you know what start means...

You're declaring startTime in two places:

long startTime=millis()+1500;                    // set srart time var
long startTime=millis()+1500;

I expect the second line was intended to update the first variable, not declare a second variable of the same name.

I know what start means. I started out with startTime=millis(); and had if(nowTime>startTime+1500) but i thought that too much math inside the if condition was the problem. so i moved the math to the beginning and didn't rename the variable. Please give me a little leeway here. I'm self taught and figure things out mostly by trial and error. I read the reference have some basic understanding, but the way this is working makes no sense to me. So i googled what i thought was my problem with no luck since if and when are so prevalent in c++. I ask here because it's really to\he only source of guidance that i have. Can we please skip the judgement about variable names, length of the delay and how often i declare a variable if it isn't causing the problem i asked about? Also please do not read this as sarcastic, posting here is very frustrating for me because of my lack of real training. thanks in advance.

You should declare a variable just once, hence my earlier comment about limited scope; you should have had an assignment, not a new declaration of the variable.

You also should not be adding time variables/values. Only subtraction is guaranteed to work.

if(millis() - startTime > interval)

is the only proper way to use millis(). Then, the name startTime makes sense.

PaulS: You also should not be adding time variables/values. Only subtraction is guaranteed to work.

if(millis() - startTime > interval)

is the only proper way to use millis(). Then, the name startTime makes sense.

YAY PaulS! thank you for that nugget of knowledge. that was exactly what i needed. it's working just as i wanted now.

AWOL: You should declare a variable just once, hence my earlier comment about limited scope; you should have had an assignment, not a new declaration of the variable.

this I should know, i usually just let it go unless it wont compile. Thank you too, i need help with my code cleanliness too.

Sorry for being a pain in the ass, i appreciate your help. I'll let you know when i have a Video of it working.

Nate