Beginner 2 two motor setup

We are using a SparkFun redboard programmed with arduino for this project...
The board has 2 inputs, and the motor plugged into the first works moving both forward and backward. However, the motor plugged into the second input works only moving with the negative pin movement but starts when the positive movement should start. It completely ignores the Positive motion code.

//PIN VARIABLES
//the motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13;
const int AIN2 = 12;           //control pin 1 on the motor driver for the right motor
const int PWMA = 11;  
const int BIN1 = 10;
const int BIN2 = 9;            //control pin 2 on the motor driver for the right motor
const int PWMB = 8;

          //speed control pin on the motor driver for the right motor  
//VARIABLES
int motorSpeed = 0;       //starting speed for the motor

void setup() {
  //set the motor control pins as outputs
  pinMode(AIN1, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
 
}
// put for loop here
void loop () {
    //drive motor forward (positive speed)
    digitalWrite(AIN1, HIGH);                        
    digitalWrite(AIN2, LOW);
    analogWrite(PWMA, 255);
    delay(3000);
    digitalWrite(BIN1, HIGH);
    digitalWrite(BIN2, LOW);                            
    analogWrite(PWMB, 255);                  
    delay(3000);
     //drive motor backward (negative speed)
    digitalWrite(AIN1, LOW);                        
    digitalWrite(AIN2, HIGH);                          
    analogWrite(PWMA, 255);            
    digitalWrite(BIN1, LOW);
    digitalWrite(BIN2, HIGH);
    analogWrite(PWMB, 255);  
    delay(3000);
}

I'm not following this. Please post a wiring diagram.

You never say what you WANT the code to do but here is what it does...

It will drive motor A forward for 3 seconds, then motor A and B both forward for 3 seconds, then motor A and B backward for 3 seconds. When loop() restarts motor B will still be moving backwards when motor A starts moving forward.

Do you need to be writing 0 to the PWMs to stop the motors before moving to the next operation? Or perhaps you have too many or too few delays? I can't tell because I don't know what you want the code to do.

Note that Pin 8 is not a PWM pin.

2 Likes

thanks this was the main issue , now the b motor actually works as intended

thanks for the reply your explanation of what the code was doing was helpful, we werent really too sure as to what we wanted the code to do before anyway, fixed now

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