Constant button Input keeps restarting millis

I’m building a self motorized cart with a 300 watt scooter motor. I’m setting motor speed and length of time it moves forward off to potentiometers. The problem is I want cart to launch with a tether pull cord causing a constant high signal that Keeps Restarting millis. Yea sorry I’m new to this programing stuff and I have been able to blend many codes together off other forums to get this so far. For you experience programmers please go easy on me. The button I’m trying to fix this on is stageStartButton

Millis_Pot_MotorLED_For_Cart_.001.ino (1.45 KB)

The only way for millis() to reset is for the Arduino to reset. Add a Serial.print() statement to setup(), to see if setup() is being called over and over.

 if
        (lastChange + runTime <= millis())

This is ass-backwards. You should be comparing now (as returned by millis()) MINUS then (lastChange) is less than, greater than, etc. some interval.

Addition involving unsigned variables can cause problems. Subtraction is guaranteed to work.

You REALLY should be looking for when the switch BECOMES pressed, not IS pressed. Look at the state change detection example.

I switched around millis to subtraction. and this is the current program I was working with on state of detection last night. I didn’t post that due to thinking I was on the wrong path. Still a no go for turning off motor. Thanks for trying to help.

Millis_Pot_MotorLED_For_Cart_.002.ino (1.56 KB)

    if (buttonState != lastButtonState){
          lastButtonState = millis();  // ledpin state changed NOW

If the current state of the switch does not equal the previous state of the switch, set the previous state of the switch to now. Does that make ANY sense?

You need a variable, of type unsigned long, to record the time that the switch became pressed. That variable will NOT be named lastButtonState. Name it switchPressedTime, so it is obvious what the variable's value is.

     else{  lastButtonState - runTime >= millis(); 
 analogWrite(pwmMotor, LOW);//was digitalWrite causing backfeeding. lighting all light on board
   lastButtonState = buttonState;

 }

The curly brace positioning and indenting suck.

     else
     {
        lastButtonState - runTime >= millis(); 
        analogWrite(pwmMotor, LOW);
        lastButtonState = buttonState;
     }

That's way better, and all I did was rearrange the white space and delete a useless comment.

So, you need to explain the first line. Almost always, when the line contains >=, it should also contain if( and ).

Thanks for the guidance. I will work on this when I get home

I finally have the time where when button pushed and release time stops for millis. I have to try some things and fix my indents, brackets, verbs and whitespace before I post the next code. lol I do appreciate your trying to help. I think with one button pushed and held down I'm getting a millis time stamp that don't keep growing and that's a start. I will work on this again when I get home tonight. I have only been chasing this for 2 1/2 weeks.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

Thanks Robin I will check that out