Timer, Button and Motor

I have a L293DNE motor driver that I would like to power and drive a motor (single direction) with the push of a button. After 5 seconds (time adjustable), from the push of the button, I would like to shut the motor off until the next push of the button. Press the reset button, then start and the program repeats.

problem: I cannot get the timer to reset and motor stays on

Does anyone have any suggestions? I tried to comment the code as best I could. Hopefully it makes sense.

code below:

const int leftForward = 2;
const int leftBackward = 3;
const int StartButton = 5;
const int resetButton = 4;
int machinedoneyet = LOW;
int timer=0;
int resetState = 0;
int StartState = 0;
int RunMode;

void setup()
{
pinMode(leftForward , OUTPUT);
pinMode(leftBackward , OUTPUT);
pinMode(StartButton, INPUT);
pinMode(resetButton, INPUT);
Serial.begin(9600);
}

void loop()
{

resetState = digitalRead(resetButton); //find button state to see if timer reset needs to happen
StartState = digitalRead(StartButton); //find button state to see if machine needs to be set to run mode

//timer = millis();
if (resetState == HIGH) { // reset timer if the reset button is pushed
timer = 0;

}
Serial.println(timer);

if ((StartState == HIGH) && (machinedoneyet == LOW)) { //check to see if Start button has been pushed and if the machine has finished counting to set time ( 5 sec)
RunMode = HIGH; //sets machine in run mode, idea is to make it so start button doesn’t need to be held down
}

if ((timer < 5000) && (RunMode == HIGH) ){ //if timer less than and machine in run mode, keep the motor going.
digitalWrite(leftForward , LOW);
digitalWrite(leftBackward , HIGH);
} else if (timer > 5000) { //if timer is past 5 seconds,
digitalWrite(leftForward , LOW); //shut down motor,
digitalWrite(leftBackward , LOW);//shut down motor,
machinedoneyet = HIGH; //toggle machinedoneyet variable HIGH,
RunMode = LOW; // stop run mode,
timer=0; //reset timer for next cycle

}
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
CCing @232 because I know they love code tags:

232:

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.

What you need to do is set a timestamp variable to the value of millis() when the motor is started. Something like this:

  if ((StartState == HIGH) && (machinedoneyet == LOW)) { //check to see if Start button has been pushed and if the machine has finished counting to set time ( 5 sec)
    RunMode = HIGH; //sets machine in run mode, idea is to make it so start button doesn't need to be held down
    startTimestamp = millis();
  }

Then to determine whether the time is up you compare millis() to the timestamp. Something like this:

if((RunMode == HIGH) && (millis() - startTimestamp > onDuration)) {

You comment out the line that sets ‘timer’ and expect the code to track time?

//timer = millis();
if (resetState == HIGH) { // reset timer if the reset button is pushed
  timer = 0;

}
Serial.println(timer);

if ((StartState == HIGH) && (machinedoneyet == LOW)) { //check to see if Start button has been pushed and if the machine has finished counting to set time ( 5 sec)
RunMode = HIGH; //sets machine in run mode, idea is to make it so start button doesn't need to be held down
}


if ((timer < 5000) && (RunMode == HIGH) ){ //if timer less than and machine in run mode, keep the motor going.
  digitalWrite(leftForward , LOW);
  digitalWrite(leftBackward , HIGH); 
} else if (timer > 5000) { //if timer is past 5 seconds,

Not that your time tracking is much good. You need to do a little study on timing for which there are many threads here that teach it and the first of the Nick Gammon tutorials in my sig space at the bottom of this post gives a clear explanation with code of what, how and why of the doing.

You don't reset a timer, like you try to do (a variable won't change by itself, by the way).

Instead you record when one event happened (record the value millis() the moment the button goes down - so you need state tracking there as well - unless you want the 5 seconds run time to be from the moment the button is released), then run the motor until the current time is more than 5 seconds past the recorded time.

Thanks All

Pert: my apologies about the formatting. Right after I posted I saw the instructions on how to post. I tried to delete and repost, but for some reason it did not work. I will do better next time.

GoForSmoke: commenting out timer :frowning: my mistake. I was trying something that totally didn’t work which required a comment of my timer. I deleted that and forgot to take the comment off of my timer. I will research the timer stuff and see what I can find.

wvmarle: thanks for reinforcing that I can’t reset a timer. newbie error.

Ill do some playing around to see what I can do and ill report back in a few days. Thanks for your help!

To get the difference between 2 times on Arduino (millis or micros) you subtract the start time from the end time.

Subtract to get the difference.

As long as you use unsigned variables it will work up to the maximum value the variable can hold.

If I use type byte variables then I could time up to 255 whether millis or micros. With millis that's just over 1/4 second.
If I use type word variables then I could time up to 65535 whether millis or micros. With millis that over 1 minute.
If I use type unsigned long variables then I could time up to 4294967296 whether millis or micros. With millis, just over 49 days, 17 hours, 2 minutes and 47 seconds.

If I same the time and call that 'start' then I can subtract that from millis() and see how long since 'start' it has been till I make the subtraction. If I keep doing that every so often, the difference will get bigger every time and if I check that against a desired wait, eventually the wait will be discovered to finish.

unsigned long tStart, tWait = 1000; // will use millis, wait is 1 second

.................... code

tStart = millis();

..................... more code

if ( millis() - tStart >= tWait )
{
tStart = tStart + tWait; // starting over
Serial.println( F( "hooray!, the wait is done!" ));
}