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
