How do i get the both motors to work at the same time with a timer?

Hello, guys! I have a problem. I am new to Arduino and am trying to build a car with two wheels and a bunch of other stuff, but I don't understand how to make both wheels work at the same time. This is my code

const unsigned X_AXIS_PIN = 0;
const unsigned Y_AXIS_PIN = 1;
const unsigned Z_AXIS_PIN = 2;
const int motor1 = 3;
const int motor2 = 5;
const int motor3 = 10;
int motorState = LOW;
long previousMillis = 0;
long interval = 5000;

void setup() 
{
  Serial.begin(9600);
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
}

int x,
    y,
    z;
    
void loop()
{
    Serial.print(analogRead(X_AXIS_PIN));
    Serial.print(" ");
    x = analogRead(X_AXIS_PIN);
    Serial.print(analogRead(Y_AXIS_PIN));
    Serial.print(" ");
    y = analogRead(Y_AXIS_PIN);
    Serial.println(analogRead(Z_AXIS_PIN));
    z = analogRead(Z_AXIS_PIN);
    delay(5000);
    
    if (x < 700 && y < 700 && z < 700)
    {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis > interval) 
            previousMillis = currentMillis;
        if (motorState == LOW)
            motorState = HIGH;
        else
            motorState = LOW;
        
        digitalWrite(motor1, motorState);
        digitalWrite(motor2, motorState);
        digitalWrite(motor3, motorState);
    }
}

Is there a way to make both wheels go backward and forward for like 5 sec? If so, please write the code or point it out. I am grateful for any answer.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

const int motor1 = 3;
const int motor2 = 5;
const int motor3 = 10;

Has it got 3 motors ?

As for

Start by looking at the delay() function but, be warned, it will block code execution for the delay period. If anything else, such as detection of inputs needs to happen during the 5 second period then you will need to use a non blocking technique for the timing

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

1 Like

Hello, it has 2 motors, not 3. I forgot why I put motor3, but my main problem is the code. I want both wheels to go forward. Right now, I can only make one wheel or the other wheel go forward and backward, but not both (without the timer). I think the problem is in the code, but I can't figure it out.

please edit (with the little pencil at the bottom of the post) your post #1 and add code tags.
Do your part to keep the forum tidy

1 Like

Is my question text good now?

thanks for fixing this

1 Like

your code likely reads a joystick to control the motors
just to make sure your wiring is fine, if you test this code, what happens ? (assuming motor1 and motor2 are the right pins)

const byte motor1DirPin = 3;
const byte motor2DirPin = 5;

void setup()
{
  Serial.begin(115200);
  pinMode(motor1DirPin, OUTPUT);
  pinMode(motor2DirPin, OUTPUT);
}

void loop() {

  digitalWrite(motor1DirPin, LOW);
  digitalWrite(motor2DirPin, LOW);
  delay(5000)

  digitalWrite(motor1DirPin, HIGH);
  digitalWrite(motor2DirPin, HIGH);
  delay(5000)

}

if that works then you should explore how to deal with timing:

i tested the code and only one of the wheels goes forward nonstop, i can only make the only of the 2 wheels go forwad and i dont know how to make it so that the both of the wheels go forward and backwords for every 5 seconds

OK - then draw the exact circuit on a piece of paper and post a picture of it

just to be sure - you have a motor driver, right ? you did not connect the pins directly to the + pole of your motor ?

does this work?

nope... we don't see clearly how things are wired and powered

Seems you have an L298N module

you should clarify how exactly things are wired and how the jumper are set

I fixed my code but now the booth wheels goes forward and backwards but the 5 second thing dosent work, i want it so that the booth wheels goes backwards for 5 secs and forward for 5 second until i want it to stop.

const byte motor1DirPin = 3;
const byte motor2DirPin = 5;

void setup() {
  pinMode(motor1DirPin, OUTPUT);
  pinMode(motor2DirPin, OUTPUT);
}

void loop() {

  digitalWrite(motor1DirPin, LOW);
  digitalWrite(motor2DirPin, LOW);
  delay(5000);


  digitalWrite(motor1DirPin, HIGH);
  digitalWrite(motor2DirPin, HIGH);
  delay(5000);
}

compilation error: Was not declared in scope

What’s that ?

it was a error code i got i dont know how to fix it

it seems incomplete usually it tells you what was not declared

if you used exactly

const byte motor1DirPin = 3;
const byte motor2DirPin = 5;

void setup() {
  pinMode(motor1DirPin, OUTPUT);
  pinMode(motor2DirPin, OUTPUT);
}

void loop() {

  digitalWrite(motor1DirPin, LOW);
  digitalWrite(motor2DirPin, LOW);
  delay(5000);


  digitalWrite(motor1DirPin, HIGH);
  digitalWrite(motor2DirPin, HIGH);
  delay(5000);
}

in the Arduino IDE then there should be no such error

1 Like

Hi, yeah, I fixed the code now. I got some help from you and some tutorials on YouTube. Thanks a bunch for helping; it works now.

int leftf1=2;
int leftf2=3;
int rightf1=5;
int rightf2=4;
void setup()
{
  // initialize digital pin LED_BUILTIN as an output.
 
    pinMode(leftf1, OUTPUT);
    pinMode(leftf2, OUTPUT);
    pinMode(rightf1, OUTPUT);
    pinMode(rightf2, OUTPUT);
 
}


// the loop function runs over and over again forever
void loop()


  {
 forward();
 delay(5000);
 reverse();
 delay(5000);  
   }


void forward()
{
   
  digitalWrite(leftf1,LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(leftf2, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(rightf1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(rightf2, LOW);   // turn the LED on (HIGH is the voltage level)    // wait for a second
     
  }
  void reverse()
  {
   
  digitalWrite(leftf1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(leftf2, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(rightf1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(rightf2, HIGH);   // turn the LED on (HIGH is the voltage level)


    }

good have fun!

1 Like

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