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.
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
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.
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)
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
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.
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)
}