Millis() Timing For Motor

So basically I’d like to turn a motor on for a second, and then have it turn it.

I can turn it on no problem, however it’s failing to turn off

Any advice would be appreciated

#include <IRremote.h>
#define LeftMotorForward 10 // Pin 10 has Left Motor connected on Arduino boards.
#define RightMotorForward 9 // Pin 12 has Right Motor connected on Arduino boards.
#define Receiver A3 // Signal Pin of IR receiver to Arduino Digital Pin 6
IRrecv irrecv(Receiver); // create instance of 'irrecv'
decode_results results;


unsigned long currentTime;
int Ontime = 1000;

void setup() {
  pinMode(LeftMotorForward, OUTPUT);  // initialize the  pin as an output.
  pinMode(RightMotorForward, OUTPUT);  // initialize the  pin as an output.
  irrecv.enableIRIn();

}
void loop() {
  //----------------------------FORWARD--------------------------

  if (irrecv.decode(&results))
  {
    currentTime = millis();
    if (results.value == 0xFF18E7)  // 2 button pressed
    {
      digitalWrite(LeftMotorForward, HIGH);   // turn the Left Motor OFF
      digitalWrite(RightMotorForward, HIGH);   // turn the Left Motor OFF
      if ((millis() - currentTime) >= Ontime) {
        digitalWrite(LeftMotorForward, LOW);   // turn the Left Motor OFF
        digitalWrite(RightMotorForward, LOW);   // turn the Left Motor OFF
      }
    }
    irrecv.resume();
  }
}

Put this lower in loop()

if ((millis() - currentTime) >= Ontime) {
        digitalWrite(LeftMotorForward, LOW);   // turn the Left Motor OFF
        digitalWrite(RightMotorForward, LOW);   // turn the Left Motor OFF
      }

You look at millis - current Time right after you set current time to millis. So it is never going to be different by more than 1ms and usually will differ by 0.

You don't want to compare the current time to millis. Millis IS the current time. You want to compare to the time you turned the motor on. So set a variable called motorOnTime or something at the point in the code where you turn the motor on and compare that to millis to turn it off.

#include <IRremote.h>
#define LeftMotorForward 10 // Pin 10 has Left Motor connected on Arduino boards.
#define RightMotorForward 9 // Pin 12 has Right Motor connected on Arduino boards.
#define Receiver A3 // Signal Pin of IR receiver to Arduino Digital Pin 6
IRrecv irrecv(Receiver); // create instance of 'irrecv'
decode_results results;

unsigned long currentTime;
int Ontime = 1000;

void setup()
{
    pinMode(LeftMotorForward, OUTPUT);  // initialize the  pin as an output.
    pinMode(RightMotorForward, OUTPUT);  // initialize the  pin as an output.
    irrecv.enableIRIn();

}
void loop()
{
    //----------------------------FORWARD--------------------------

    if (irrecv.decode(&results))
    {
        if (results.value == 0xFF18E7)  // 2 button pressed
        {
            digitalWrite(LeftMotorForward, HIGH);   // turn the Left Motor ON
            digitalWrite(RightMotorForward, HIGH);   // turn the Left Motor ON
            currentTime = millis();
        }
        irrecv.resume();
    }
    
    if ((millis() - currentTime) >= Ontime)
    {
        digitalWrite(LeftMotorForward, LOW);   // turn the Left Motor OFF
        digitalWrite(RightMotorForward, LOW);   // turn the Left Motor OFF
    }
}

You guys are awesome. Makes a bit more sense ahah

The demo Several Things at a Time is an exended illustration of the use of millis()

...R