Motors won't stop running using millis

c

onst int motorA_pin_1=7;         // The numbers of the motor pins
const int motorA_pin_2=6;         // For motor A
const int motorA_pin_speed=5;

const int motorB_pin_1=9;        // The number of the motor pins
const int motorB_pin_2=8;        // For motor B
const int motorB_pin_speed=10;

const int ledPin=13;             // The number of the LED pin  

int motorA_state_pin_1=LOW;        // Used to set the state of the motors
int motorA_state_pin_2=LOW;        // Low is off
int motorB_state_pin_1=LOW;
int motorB_state_pin_2=LOW;

int ledState=LOW;                 // The LED state is low or off

unsigned long previousMillis=0;   // Stores the time the state of the motors was checked

const long interval=6000;    // How long the motor will run for

const long led_interval=2000;     // How long the LED will blink

void setup() {   // Put your code hear to run once

 pinMode(motorA_pin_1,OUTPUT);             // Sets the pins to output
 pinMode(motorA_pin_2,OUTPUT);
 pinMode(motorA_pin_speed,OUTPUT);
 pinMode(motorB_pin_1,OUTPUT);
 pinMode(motorB_pin_2,OUTPUT);
 pinMode(motorB_pin_speed,OUTPUT);
 
 analogWrite(motorA_pin_speed,255);      // sets how fast the motors will turn
 analogWrite(motorB_pin_speed,255);


 unsigned long currentMillis=millis();              // This makes both motors
 if(currentMillis-previousMillis>=interval);{    // run forward for 6 seconds
   previousMillis=currentMillis;
   if(motorA_state_pin_1==LOW){
   if(motorB_state_pin_1==LOW)                    // THIS IS LEG 1
   motorA_state_pin_1=HIGH;
   motorB_state_pin_1=HIGH;
 }else{
   motorA_state_pin_1=LOW;
   motorB_state_pin_1=LOW;  
 }                           // This closes the " ELSE "statement
 }                          // This closes the time
}                        // This closes void setup

void loop() {

 // put your main code here, to run repeatedly:
    digitalWrite(motorA_pin_1,motorA_state_pin_1);
    digitalWrite(motorB_pin_1,motorB_state_pin_1);
    digitalWrite(motorA_pin_2,motorA_state_pin_2);
    digitalWrite(motorB_pin_2,motorB_state_pin_2);
     digitalWrite(ledPin,ledState);
  
   }                                                        // This closes the void loop

This is my grandson's tank program, he had it running great using " delay " but when he was using " blink without delay" the motors just keep running. The motors should run for 6 seconds and then shut off. I have have gone over and over it with him, but my limited knowledge is not helping his frustration
level. Can some one have a look and see what he/we are doing wrong.
Many thanks

For some reason all the millis() code is in setup(). It should be in loop()

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

PS … To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Are the motors supposed to be running for 6 seconds only at the start of the sketch, or are they supposed to do so constantly?

If they are supposed to do so only at the start of the sketch, then you shoud go back to the delay() technique. BWD technique does not work in the setup.

Otherwise, you should explain when they are expected to turn.

Jacques

Your attempt to put the code in code tags failed. Want to try again?

  if(currentMillis-previousMillis>=interval);{    // run forward for 6 seconds

Get rid of the semicolon you embedded.

And the millis() code belongs to loop, not to setup.

Loop() is for things that you want to be done over and over, and forever (or until you pull the plug) It should be empty otherwise.

Setup() is for things that you want done only once, at the start of the sketch.

Jacques

This is a special situation, where I could steer you in either of two directions.

  1. Place everything in setup()
  2. Place everything in loop()

In the second case, maybe a state machine would be the prefered choice:

  • Moving forward
  • Turning right
  • Stopped

But i was thinking that he was headed the right way by putting everything in setup()

Then he should forget about "Blink without delay" and go with delay()

In the loop he is going to have a bank of LED's to flash as the tank is moving.

If all the movement is done in setup() then the code in loop() will not run until the movement has finished.

The essence of using millis() for timing is to record a start time then check frequently whether the required interval has elapsed by comparing the current value of millis() with the start time. The loop() function is ideal for this purpose as you don't have to do anything to make it repeat its code.

I assume that you have read
Using millis() for timing. A beginners guide

chickenhawk:
Ok he can live with out blinking the led's.

I suspect it would be more fruitful if your grandson participates here (under your supervision) rather than trying to deal with the problem at second hand.

...R

why will the motors not shut off after the 6 seconds?

The timing code in the original post is all in setup() so will only be executed once. The millis() style of timing relies on checking repeatedly whether a period has elapsed but the code has no mechanism to check more than once.