Question about how to reverse a dc motor using a motor controller

The forward function works properly but I can not get the reverse to happen. Im assuming the format for the motor pins are correct


int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 5;
int buttonPinForward = 6;
int buttonPinReverse = 9;

int enA = 10;
int enB = 11 ;
void setup() {
  // put your setup code here, to run once:

  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);
  pinMode(buttonPinForward, INPUT_PULLUP);
  pinMode(buttonPinReverse, INPUT_PULLUP);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);

}

void loop() {
  if (digitalRead(buttonPinForward) == HIGH)
  {
    digitalWrite(motor2pin1, LOW);
    digitalWrite(motor2pin2, LOW);
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, LOW);


  }

  else {
    digitalWrite(enA, HIGH);
    digitalWrite (enB, HIGH);
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
    digitalWrite(motor2pin1, HIGH);
    digitalWrite(motor2pin2, LOW);
    for (int i = 0; i < 256; i++) {
      analogWrite(10, i);
      analogWrite(11, i);

    }
    if (digitalRead(buttonPinReverse) == HIGH)
    {
      digitalWrite(motor2pin1, LOW);
      digitalWrite(motor2pin2, LOW);
      digitalWrite(motor1pin1, LOW);
      digitalWrite(motor1pin2, LOW);


    }
    else {
      digitalWrite(enA, HIGH);
      digitalWrite (enB, HIGH);
      digitalWrite(motor1pin1, LOW);
      digitalWrite(motor1pin2, HIGH);
      digitalWrite(motor2pin1, HIGH);
      digitalWrite(motor2pin2, LOW);
      for (int i = 0; i < 256; i++) {
        analogWrite(enA, i);
        analogWrite(enB, i);

      }
    }
  }
}

Who can tell?
What driver are You using? Schematics please.

Please use the autoformat function in the IDE. That often points out mistakes.

Why pin numbers, not the names given?
Do You know what // does, following the end of a code line?

The L298N
The pin number works the same as if the name was there

Please post a hand drawn wiring diagram, showing how you have connected the Arduino and the motor power supply to the motor driver, with pins and parts clearly labeled.

Tell us about the motor power supply and post a link to the motor product page.

Why do you have L298 pin 1 (ISEN A) grounded? Could be your problem, remove it.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

It sure does but it tells about Your approach to programming. Start using comments! There will come a day when You Yourself will benefit from it.

The schematics show Vcc. What is that? From what is it coming? Know that insufficient powering is a very usual reason for trouble, therefore the question.

It could be but a checkup is advised.

Or maybe you need to ground pin 15 (ISEN B)?

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