I am trying to get the following sequence forward, left,right,backward but it's moving first to the right and then to the left, I create the sequence of event by hand and I think that I have the right configuration, if there is somebody that knows what I've been failing please let me know thank you,
PD: I checked the connections and everything is good.
// Define motor control pins
#define PWMA 5 // Motor A Speed (PWM)
#define AIN1 7 // Motor A Direction
#define PWMB 6 // Motor B Speed (PWM)
#define BIN1 8 // Motor B Direction
#define STBY 3 // Standby Pin
// Function to move forward
void moveForward(int speed) {
digitalWrite(AIN1, HIGH); // Motor A Forward
digitalWrite(BIN1, HIGH); // Motor B Forward
analogWrite(PWMA, speed); // Set speed (0-255)
analogWrite(PWMB, speed);
}
// Function to move backward
void moveBackward(int speed) {
digitalWrite(AIN1, LOW); // Motor A Reverse
digitalWrite(BIN1, LOW); // Motor B Reverse
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
// Function to turn left
void turnLeft(int speed) {
digitalWrite(AIN1, LOW); // Motor A Reverse
digitalWrite(BIN1, HIGH); // Motor B Forward
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
// Function to turn right
void turnRight(int speed) {
digitalWrite(AIN1, HIGH); // Motor A Forward
digitalWrite(BIN1, LOW); // Motor B Reverse
analogWrite(PWMA, speed);
analogWrite(PWMB, speed);
}
// Function to stop the motors
void stopMotors() {
analogWrite(PWMA, 0);
analogWrite(PWMB, 0);
delay(200);
digitalWrite(STBY, LOW);
}
void setup() {
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(STBY, OUTPUT); // Enable TB6612
digitalWrite(STBY, HIGH); // Activate motor driver
}
void loop() {
moveForward(200); // Move forward at speed 200
delay(2000);
turnLeft(200); // Turn Left at speed 200
delay(1000);
turnRight(200); // Turn Right at speed 200
delay(1000);
moveBackward(200); // Move backward at speed 200
delay(2000);
stopMotors(); // Stop the motors
delay(1000);
}