Having trouble running DC motor for specific amount of time then stopping using milli()

Hello, I am trying to have two DC Motors run for a specific amount of time when a specific button is pressed on an IR Remote. The forward and backwards buttons work fine but when I try to get the robot to turn by having the motors running in opposite directions for a specific amount of time (using the zero and 1 buttons as shown in the code) the robot just keeps rotating endlessly and will not stop until I press the power button. Typically I use the delay() function but it was not working and many of you on the forum recommended using the millis() function so I gave that a try (first time using millis() ) and still no luck. The code is shown below if anyone can help fix it would be greatly appreciated. (Sorry for all the comments, it helps me keep track of the circuit). Also, I don't think it matters but I am using a nano 33 iot board, and I am mainly only testing this using the zero button at the moment.

// MUST USE EXTERNAL POWER SOURCE FOR DC MOTOR
// EVERYTHING IN CIRCUIT MUST SHARE A COMMON GROUND (USE GROUND RAIL AND ENSURE ALIGNED WITH POWER SUPPLY)
/* PIN1 ON L293D STARTS TO LEFT OF NOTCH(SIDE CIRCLE INDENTION IS ON) PINS LOOP AROUND TO WHERE 
PIN DIRECTLY ACROSS FROM PIN 1 IS PIN 16 */
//PINS ON LEFT OF L293D CONTROL 1 MOTOR PINS ON RIGHT CONTROL OTHER
// FOR EN1(L293D PIN 1) AND EN2(L293D PIN 9) PINS ON L293D MUST USE PWM PIN (~) (ANALOG)
// L293D PINS 4,5,12,13 ARE GND PINS AND MUST BE CONNECTED TO RAIL IF USED ON EITHER SIDE

#include <IRremote.hpp>

const int IR_RECEIVE_PIN = 19; // Y pin on IR DECODER IS READ PIN, MUST BE PWM PIN(~), R IS POWER PIN (5V), G IS GND
unsigned long lastCode = 0; // Variable to store the last valid code
//bool motorOn = false;
unsigned long powerButton = 0xBA45FF00; // !!! ALL HEXADECIMAL NUMBERS MUST START WITH 0x !!!
unsigned long forwardButton = 0xF609FF00;
unsigned long backwardsButton = 0xF807FF00 ;
unsigned long zeroButton = 0xE916FF00;
unsigned long oneButton = 0xF30CFF00;
unsigned long twoButton = 0xE718FF00;
unsigned long threeButton = 0xA15EFF00;
unsigned long fourButton = 0xF708FF00;
unsigned long fiveButton = 0xE31CFF00;
unsigned long sixButton = 0xA55AFF00;
unsigned long sevenButton = 0xBD42FF00;
unsigned long receivedCode; // NEED TO HAVE THIS ASSIGNED OUTSIDE OF LOOP TO WORK WITH LED OR OTHER CONNECTED CIRCUIT


bool running = false;  // To track if motor is activly running
unsigned long runningStartTime; // Time when the motors running started
unsigned long runTime;  // Run time for the current button press
int motor1speedpin=2; // L293D PIN 1 (EN1: CONTROLS MOTOR SPEED) MUST BE PWM PIN(~)(ANALOG); ARDUINO PIN 5(~);
int M1directionpin1=4;// L293D pin 2 (IN1) on motor controller ; ARDUINO PIN 4 (DIGITAL PIN); 
int M1directionpin2=7; // L293D pin 7 (IN2) on motor controller ; ARDUINO PIN 2 (DIGITAL PIN)
int motor1speed=250;//MAX 255 MIN 0 (MAY NOT RUN AT ZERO) (LOWEST SELF STARTING RUNNING SPEED =~ 90)
int halfm1speed=125;
int thirdm1speed=85;
int coastm1speed=200;
int motor2speedpin=16; //L293D PIN 9 (EN2: CONTROLS MOTOR SPEED) MUST BE PWM PIN(~)(ANALOG); ARDUINO PIN 11(~);
int M2directionpin1=14; // L293D pin 10 (IN3) on motor controller ; ARDUINO PIN 7 (DIGITAL PIN);
int M2directionpin2=15; // L293D pin 15 (IN4) on motor controller ; ARDUINO PIN 8 (DIGITAL PIN)
int motor2speed=250; //MAX 255 MIN 0 (MAY NOT RUN AT ZERO) (LOWEST SELF STARTING RUNNING SPEED =~ 90)
int halfm2speed=125;
int thirdm2speed=85;
int coastm2speed=200;
// RED WIRE ON MOTOR 1 TO L293D PIN 3 (OUT 1)
// BLACK WIRE ON MOTOR 1 TO L293D PIN 6 (OUT 2)
// RED WIRE ON MOTOR 2 TO L293D PIN 11 (OUT 3)
// BLACK WIRE ON MOTOR 2 TO L293D PIN 14 (OUT 4)
// GND FROM MOTOR CONTROLLER TO GND RAIL; L293D PIN 4 for motor 1 & PIN 12 for motor 2
// L293D PIN 8 CONNECTED TO EXTERNAIL POWER SUPPLY RAIL (+); FOR MOTOR 2 WE DO SAME WITH PIN 16


void stopMotors() {
  digitalWrite(M1directionpin1, LOW);
  digitalWrite(M1directionpin2, LOW);
  digitalWrite(M2directionpin1, LOW);
  digitalWrite(M2directionpin2, LOW);
  analogWrite(motor1speedpin, 0);
  analogWrite(motor2speedpin, 0);
}

void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
  pinMode(motor1speedpin, OUTPUT);
  pinMode(M1directionpin1, OUTPUT);
  pinMode(M1directionpin2, OUTPUT);
  pinMode(motor2speedpin, OUTPUT);
  pinMode(M2directionpin1, OUTPUT);
  pinMode(M2directionpin2, OUTPUT);
}

void loop(){

  if (IrReceiver.decode()) 
  {
     receivedCode = IrReceiver.decodedIRData.decodedRawData;
    
    // If received code is non-zero, update lastCode and print it
    if (receivedCode != 0) {
      lastCode = receivedCode;
      Serial.println(lastCode, HEX);
    }
    // If received code is zero, reprint the last valid code
    else if (lastCode != 0) {
      Serial.println(lastCode, HEX);
    }
    
    IrReceiver.resume(); // Enable receiving of the next value
  }

  if(receivedCode == powerButton) {  // STOP
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,LOW);
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,LOW);
  }
  
    
  if(receivedCode == forwardButton) {  // GO FORWARDS (MOTORS SPIN BACK MAKE R2 GO FORWARD)
    digitalWrite(M1directionpin1,LOW); // FOR RIGHT WHEEL FORWARD M1D1 = LOW, M1D2 = HIGH
    digitalWrite(M1directionpin2,HIGH);
    digitalWrite(M2directionpin1,HIGH);  // FOR LEFT WHEEL FORWARD M2D1 = HIGH, M2D2 = LOW 
    digitalWrite(M2directionpin2,LOW);
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
  }

  if(receivedCode == backwardsButton) {  // GO BACKWARDS  (MOTORS SPIN FORWARD MAKE R2 GO BACK)
    digitalWrite(M1directionpin1,HIGH);
    digitalWrite(M1directionpin2,LOW);   // FOR RIGHT WHEEL BACKWARDS M1D1 = HIGH, M1D2 = LOW
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,HIGH);   // FOR LEFT WHEEL BACKWARDS M2D1 = LOW, M2D2 = HIGH
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
  }  

  if(receivedCode == zeroButton && !running) {  // ROTATE LEFT 1.75 SEC
    runTime = 1750;
    runningStartTime = millis();
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,HIGH);   
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,HIGH);
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
    running = true;
  }


  if(receivedCode == oneButton) {  // ROTATE LEFT 1.6 SEC
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,HIGH);   
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,HIGH);
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
    delay(1600);
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,LOW);
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,LOW);
  } 
  
   // Stop motors after the specified run time
  if (running && runningStartTime - runTime >= runTime) {
    stopMotors();
    running = false; // Reset running flag
  } 
}

I have uploaded an image of the circuit, I am confident the circuit works, as it will rotate left right, forward, & and back on command just not for x amount of time which is believe would be more of a problem with the code

Try

if (running && millis() - runningStartTime >= runTime) {
3 Likes
  • BTW, when you ask questions, it's always recommended you show us a schematic to review.

i see that you capture a timestamp, runningStartTime, but don't see where it is used

look this over

  • i beleive this code is easier to read, cleaner format
  • less redundant and similar symbol names
  • Capitalize Constants
  • has a timer to stop some commands
  • has a single function to set dir and speed, using sign of speed to determine direction
  • has prints to help indicate receive command
  • see pg 49 of The C Programming Language for explanation of 0 >= SpdMleft ? LOW : HIGH
  • only processes IR commands when code received
// IR remote two wheel drive 

# include <IRremote.hpp>

const byte PinMleftSpd   = 2;
const byte PinMleftDir1  = 4;
const byte PinMleftDir2  = 7;

const byte PinMrightSpd  = 16;
const byte PinMrightDir1 = 14;
const byte PinMrightDir2 = 15;


// -------------------------------------

const unsigned long CodePower     = 0xBA45FF00;
const unsigned long CodeForward   = 0xF609FF00;
const unsigned long CodeReverse   = 0xF807FF00;
const unsigned long CodeZero      = 0xE916FF00;
const unsigned long CodeOne       = 0xF30CFF00;
const unsigned long CodeTwo       = 0xE718FF00;
const unsigned long CodeThree     = 0xA15EFF00;
const unsigned long CodeFour      = 0xF708FF00;
const unsigned long CodeFive      = 0xE31CFF00;
const unsigned long CodeSix       = 0xA55AFF00;
const unsigned long CodeSeven     = 0xBD42FF00;

const int IR_RECEIVE_PIN = 19;

// -----------------------------------------------------------------------------
// -------------------------------------
// RED   WIRE ON MOTOR 1 TO L293D PIN 3 (OUT 1)
// BLACK WIRE ON MOTOR 1 TO L293D PIN 6 (OUT 2)
// RED   WIRE ON MOTOR 2 TO L293D PIN 11 (OUT 3)
// BLACK WIRE ON MOTOR 2 TO L293D PIN 14 (OUT 4)
// GND FROM MOTOR CONTROLLER TO GND RAIL
// ; L293D PIN 4 for motor 1 & PIN 12 for motor 2
// L293D PIN 8 CONNECTED TO EXTERNAIL POWER SUPPLY RAIL (+); FOR MOTOR 2 WE DO SAME WITH PIN 16

const int SpdFull    = 250;
const int SpdHalf    = SpdFull / 2;
const int Spd3rd     = SpdFull / 3;
const int SpdStop    = 0;

// -----------------------------------------------------------------------------
void
setSpd (
    int SpdMleft,       // negative for reverse
    int SpdMright )     // negative for reverse
{
    digitalWrite (PinMleftDir1,  0 >= SpdMleft  ? LOW  : HIGH);
    digitalWrite (PinMleftDir2,  0 >  SpdMleft  ? HIGH : LOW);
    digitalWrite (PinMrightDir1, 0 >  SpdMright ? HIGH : LOW);
    digitalWrite (PinMrightDir2, 0 >= SpdMright ? LOW  : HIGH);

    analogWrite  (PinMleftSpd,  abs(SpdMleft));
    analogWrite  (PinMrightSpd, abs(SpdMright));
}

// -----------------------------------------------------------------------------
unsigned long msecPeriod;
unsigned long msecRun;

void loop ()
{
    unsigned long msec = millis ();

    // ---------------------------
    // timer
    if (msecRun && msec - msecRun >= msecPeriod)  {
        setSpd (0, 0);       // stop
        msecRun = 0;
    }

    // ---------------------------
    // monitor receiver
    if (! IrReceiver.decode ())
        return;                     // nothing to do

    // read code
    unsigned long code = IrReceiver.decodedIRData.decodedRawData;
    Serial.println (code, HEX);


    switch (code)  {
    case CodeForward:
        Serial.println ("forward");
        setSpd (SpdFull, SpdFull);
        break;

    case CodeReverse:
        Serial.println ("reverse");
        setSpd (-SpdFull, -SpdFull);
        break;

    case CodeZero:                      // rotate left 1.75 sec
        Serial.println ("zero - rotate left");
        msecPeriod = 1750;
        msecRun    = msec;
        setSpd (-SpdHalf,  SpdHalf);
        break;

    case CodeOne:                      // rotate right 1.75 sec
        Serial.println ("one  - rotate left");
        msecPeriod = 1750;
        msecRun    = msec;
        setSpd ( SpdHalf, -SpdHalf);
        break;

    case CodeTwo:                      // turn right
        Serial.println ("two  - turn right");
        setSpd ( SpdFull,  SpdHalf);
        break;

    case CodeThree:
        Serial.println ("three");
        break;

    case CodeFour:
        Serial.println ("four");
        break;

    case CodeFive:
        Serial.println ("five");
        break;

    case CodeSix:
        Serial.println ("six");
        break;

    default:
        setSpd (0, 0);       // stop
        break;
    }

    IrReceiver.resume();
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    IrReceiver.begin (IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

    pinMode (PinMleftSpd,  OUTPUT);
    pinMode (PinMleftDir1, OUTPUT);
    pinMode (PinMleftDir2, OUTPUT);
    pinMode (PinMrightSpd,  OUTPUT);
    pinMode (PinMrightDir1, OUTPUT);
    pinMode (PinMrightDir2, OUTPUT);

    setSpd (0, 0);
}

Hi, @epwalker97
Sorry but had to spread your post out.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Get rid of the delays, and use the millis() concept with all your code. In other words, after so many millis it is time to read the IR, or after so many millis() it is time to check receivedCode.

You coded

  if (running && runningStartTime - runTime >= runTime) {

which is a medium complex condition. To narrow down what is really happening do serial printing of each and every variable and calculated results

Serial.print("running=");
Serial.print(running);
Serial.print(" runningStartTime=");
Serial.print(runningStartTime );
Serial.print(" runningStartTime - runTime=");
Serial.print(runningStartTime);
Serial.print(" - ");
Serial.print(runTime);
Serial.print(" = ");
Serial.print(runningStartTime - runTime);
Serial.println();

Unfortunately, this solution did not work

Unfortunately, this code did not do the trick either. Same result.

Then your problem is elsewhere, because that test is the bog standard one for testing if an interval has expired. It's literally used everywhere. It works.

so what trick did it do?

you didn't see it stop when doing a rotate with the zero and one cmds?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Do you have a DMM? (Digital MultiMeter)

Have you checked the voltage of your 9V smoke detector batteries?
Those batteries are not designed for this sort of application.

How much current does your circuit draw?

Can you please post link to data/specs of your DC motors.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I figured it out guys! Trick is to reset millis() to zero (didn't know that was possible) and refer to the code below to run through the logic. Was specifically using the zero button and one button for testing and the variables associated, as well as the if statement at the very end of the program(really the key to it all).

// MUST USE EXTERNAL POWER SOURCE FOR DC MOTOR
// EVERYTHING IN CIRCUIT MUST SHARE A COMMON GROUND (USE GROUND RAIL AND ENSURE ALIGNED WITH POWER SUPPLY)
/* PIN1 ON L293D STARTS TO LEFT OF NOTCH(SIDE CIRCLE INDENTION IS ON) PINS LOOP AROUND TO WHERE 
PIN DIRECTLY ACROSS FROM PIN 1 IS PIN 16 */
//PINS ON LEFT OF L293D CONTROL 1 MOTOR PINS ON RIGHT CONTROL OTHER
// FOR EN1(L293D PIN 1) AND EN2(L293D PIN 9) PINS ON L293D MUST USE PWM PIN (~) (ANALOG)
// L293D PINS 4,5,12,13 ARE GND PINS AND MUST BE CONNECTED TO RAIL IF USED ON EITHER SIDE

#include <IRremote.hpp>

const int IR_RECEIVE_PIN = 19; // Y pin on IR DECODER IS READ PIN, MUST BE PWM PIN(~), R IS POWER PIN (5V), G IS GND
unsigned long lastCode = 0; // Variable to store the last valid code
//bool motorOn = false;
unsigned long powerButton = 0xBA45FF00; // !!! ALL HEXADECIMAL NUMBERS MUST START WITH 0x !!!
unsigned long forwardButton = 0xF609FF00;
unsigned long backwardsButton = 0xF807FF00 ;
unsigned long zeroButton = 0xE916FF00;
unsigned long oneButton = 0xF30CFF00;
unsigned long twoButton = 0xE718FF00;
unsigned long threeButton = 0xA15EFF00;
unsigned long fourButton = 0xF708FF00;
unsigned long fiveButton = 0xE31CFF00;
unsigned long sixButton = 0xA55AFF00;
unsigned long sevenButton = 0xBD42FF00;
unsigned long receivedCode; // NEED TO HAVE THIS ASSIGNED OUTSIDE OF LOOP TO WORK WITH LED OR OTHER CONNECTED CIRCUIT


bool running = false;  // To track if motor is activly running
int desiredTime;  
int motor1speedpin=2; // L293D PIN 1 (EN1: CONTROLS MOTOR SPEED) MUST BE PWM PIN(~)(ANALOG); ARDUINO PIN 5(~);
int M1directionpin1=4;// L293D pin 2 (IN1) on motor controller ; ARDUINO PIN 4 (DIGITAL PIN); 
int M1directionpin2=7; // L293D pin 7 (IN2) on motor controller ; ARDUINO PIN 2 (DIGITAL PIN)
int motor1speed=250;//MAX 255 MIN 0 (MAY NOT RUN AT ZERO) (LOWEST SELF STARTING RUNNING SPEED =~ 90)
int halfm1speed=125;
int thirdm1speed=85;
int coastm1speed=200;
int motor2speedpin=16; //L293D PIN 9 (EN2: CONTROLS MOTOR SPEED) MUST BE PWM PIN(~)(ANALOG); ARDUINO PIN 11(~);
int M2directionpin1=14; // L293D pin 10 (IN3) on motor controller ; ARDUINO PIN 7 (DIGITAL PIN);
int M2directionpin2=15; // L293D pin 15 (IN4) on motor controller ; ARDUINO PIN 8 (DIGITAL PIN)
int motor2speed=250; //MAX 255 MIN 0 (MAY NOT RUN AT ZERO) (LOWEST SELF STARTING RUNNING SPEED =~ 90)
int halfm2speed=125;
int thirdm2speed=85;
int coastm2speed=200;
int startTime;
// RED WIRE ON MOTOR 1 TO L293D PIN 3 (OUT 1)
// BLACK WIRE ON MOTOR 1 TO L293D PIN 6 (OUT 2)
// RED WIRE ON MOTOR 2 TO L293D PIN 11 (OUT 3)
// BLACK WIRE ON MOTOR 2 TO L293D PIN 14 (OUT 4)
// GND FROM MOTOR CONTROLLER TO GND RAIL; L293D PIN 4 for motor 1 & PIN 12 for motor 2
// L293D PIN 8 CONNECTED TO EXTERNAIL POWER SUPPLY RAIL (+); FOR MOTOR 2 WE DO SAME WITH PIN 16


void stopMotors() {
  digitalWrite(M1directionpin1, LOW);
  digitalWrite(M1directionpin2, LOW);
  digitalWrite(M2directionpin1, LOW);
  digitalWrite(M2directionpin2, LOW);
  analogWrite(motor1speedpin, 0);
  analogWrite(motor2speedpin, 0);
}

void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
  pinMode(motor1speedpin, OUTPUT);
  pinMode(M1directionpin1, OUTPUT);
  pinMode(M1directionpin2, OUTPUT);
  pinMode(motor2speedpin, OUTPUT);
  pinMode(M2directionpin1, OUTPUT);
  pinMode(M2directionpin2, OUTPUT);
}

void loop(){

  

  if (IrReceiver.decode()) 
  {
     receivedCode = IrReceiver.decodedIRData.decodedRawData;
    
    // If received code is non-zero, update lastCode and print it
    if (receivedCode != 0) {
      lastCode = receivedCode;
      Serial.println(lastCode, HEX);
    }
    // If received code is zero, reprint the last valid code
    else if (lastCode != 0) {
      Serial.println(lastCode, HEX);
    }
    
    IrReceiver.resume(); // Enable receiving of the next value
  }

  if(receivedCode == powerButton) {  // STOP
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,LOW);
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,LOW);
  }
  
    
  if(receivedCode == forwardButton) {  // GO FORWARDS (MOTORS SPIN BACK MAKE R2 GO FORWARD)
    digitalWrite(M1directionpin1,LOW); // FOR RIGHT WHEEL FORWARD M1D1 = LOW, M1D2 = HIGH
    digitalWrite(M1directionpin2,HIGH);
    digitalWrite(M2directionpin1,HIGH);  // FOR LEFT WHEEL FORWARD M2D1 = HIGH, M2D2 = LOW 
    digitalWrite(M2directionpin2,LOW);
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
  }

  if(receivedCode == backwardsButton) {  // GO BACKWARDS  (MOTORS SPIN FORWARD MAKE R2 GO BACK)
    digitalWrite(M1directionpin1,HIGH);
    digitalWrite(M1directionpin2,LOW);   // FOR RIGHT WHEEL BACKWARDS M1D1 = HIGH, M1D2 = LOW
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,HIGH);   // FOR LEFT WHEEL BACKWARDS M2D1 = LOW, M2D2 = HIGH
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
  }  

  if(receivedCode == zeroButton) {  // ROTATE LEFT 1.75 SEC
    startTime = millis();
    desiredTime = 1750;
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,HIGH);   
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,HIGH);
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
    Serial.println(startTime);
 }


  if(receivedCode == oneButton) {  // ROTATE LEFT 4 SEC
    startTime = millis();
    desiredTime = 4000;
    digitalWrite(M1directionpin1,LOW);
    digitalWrite(M1directionpin2,HIGH);   
    digitalWrite(M2directionpin1,LOW);
    digitalWrite(M2directionpin2,HIGH);
    analogWrite(motor1speedpin,motor1speed);
    analogWrite(motor2speedpin,motor2speed);
    
  } 

  if (millis() - startTime == desiredTime) {
    stopMotors();
    millis() == 0;
    
}

this doesn't set millis() to a value, == is a comparision that is ignored.

this is what fixes your code, calling stopMotors() when the timer expires

millis() may not hit the exact value of 1750 as it sometimes jumps by 2,

Better to use

if (millis() - startTime >= desiredTime) {
    stopMotors();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.