2 push button code difficulty

The standard "New to Arduino/coding" yada yada yada

I have two DC motors running through a L298N, each motor's voltage is controlled via a potentiometer and direction via a state change push button.

What works:

Both motors will spin through their varied voltages and "motorA" will change directions with it's push button.

What doesn't work:

MotorB will not change directions when it's corresponding button is pressed. However, motorA WILL change directions when motorB's button is pressed (in addition to it's own button).

I believe ("know") the error is something simple in the coding for the buttons--their coding is the same. But I do not know how differentiate the two from a nomenclature standpoint. I've spent time looking though this forum as well as other sites and at this point I'm cross eyed frustrated. Having these motors change polarity via a push button isn't the goal of my project but is a step in my learning what I need for the end goal and this would like to learn what I'm not getting about this problem.

/*

Arduino 2 DC motor control - PWM | L298N | Example 01

***3 SECOND DELAY TO START***
2 DC motors
2 potentiometers w/ serial readouts
2 buttons--motor direction change


//MOTOR A
#define enA 10
#define in1 9
#define in2 8
#define buttonA 4

//MOTOR B
#define enB 5
#define in3 7
#define in4 6
#define buttonB 3

int rotDirection = 0;
int pressed = false;

void setup()
{
  delay(3000);
  Serial.begin(9600);  // initialize serial communication at 9600 bits per second
  
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(buttonA, INPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(buttonB, INPUT);
  
// Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void loop() 
{
  int sensorValueA = analogRead(A0); // read the input on analog pin 0  
  int sensorValueB = analogRead(A1); // read the input on analog pin 1
  // print out the value you read every 2/10ths second:
  Serial.print(sensorValueA);
  Serial.print(" , ");
  Serial.println(sensorValueB);
  delay(200);   //delay between reads for stability
    
  int pwmOutputA = map(sensorValueA, 0, 1023, 80, 255); // Map the potentiometer value from 0 to 255
  int pwmOutputB = map(sensorValueB, 0, 1023, 95, 255); // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutputA); // Send PWM signal to L298N Enable pin A
  analogWrite(enB, pwmOutputB); // Send PWM signal to L298N Enable pin B

  
  // Read button - Debounce
  if (digitalRead(buttonA) == true) {
    pressed = !pressed;
    while (digitalRead(buttonA) == true);
    }
  if (digitalRead(buttonB) == true) {
    pressed = !pressed;
    while (digitalRead(buttonB) == true);
  }
    
 //MOTOR A
  // If button is pressed - change rotation direction
  if (pressed == true  & rotDirection == 0) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    rotDirection = 1;
  }
  //MOTOR A
  // If button is pressed - change rotation direction
  if (pressed == false & rotDirection == 1) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    rotDirection = 0;
  }
  
  //MOTOR B
  // If button is pressed - change rotation direction
  if (pressed == true  & rotDirection == 0) {
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    rotDirection = 1;
  }
  //MOTOR B
  // If button is pressed - change rotation direction  
  if (pressed == false & rotDirection == 1)  {
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    rotDirection = 0; 
  }
  }

Thank you!

Evan

You're using single variables 'pressed' and 'rotDirection' for both buttons and motors. You need separate ones.

Steve

Thank you slipstick! I’m up and running (both directions) now! Was pretty sure it was nomenclature but extra/fresh eyes frequently are the trick.

You should be using Logical AND (&&) instead of Bitwise AND (&) in your IF statements.

johnwasser:
You should be using Logical AND (&&) instead of Bitwise AND (&) in your IF statements.

Admittedly, I've only done a quick search (thusfar--just to know what you were referring to) of both Logical AND and Bitwise AND. I've added a second "&" to all singular "&"s and the sketch worked. That being said, I see there is more to it than just the difference in language. But at the risk of going down rabbit holes I have no business being down at this point, what is the significance between Logical AND and Bitwise AND? (I do also realize that probably cannot be summed up in a forum reply.)

Again, thank you for any/all help!

Evan

Logical AND works on two boolean values. If a value is zero it is treated as ‘false’. A non-zero value is treated as ‘true’. If both values are true, the result of the ‘&&’ is true. If either value is false the result of the ‘&&’ is false. Note: If the expression on the left is false, the expression on the right isn’t evaluated, since the result would be false in any case.

Boolean AND works on patterns of bits. The 1 bits are ‘true’ and the 0 bits are ‘false’ and the Logical AND is used to calculate the bits of the result: B0011 & B1010 results in the value B0010 since the second bit from the right is the only one where both input values are 1.