Switches to turn motors controlled by dual VNH3SP30 on & off

I am trying to use switches to turn motors that are controlled by dual VNH3SP30 motor driver on and off but there may be problems with the Arduino code. I have Arduino Mega 2560 board. When I press a switch button, the motor runs in one direction. When I press other switch button, the motor runs in other direction. When I don’t press either switch button, the motor is supposed to stop but it doesn’t stop. How can I fix the problem?

int InA1 = 22;
int InB1 = 24;
int PWM1 = 4;
int InA2 = 26;
int InB2 = 28;
int PWM2 = 5;
int InA3 = 30;
int InB3 = 32;
int PWM3 = 6;
int InA4 = 34;
int InB4 = 36;
int PWM4 = 7;
int switch01 = 38;
int switch02 = 39;
int switchstate01 = 0;
int switchstate02 = 0;
void setup() 
{
  Serial.begin(115200);
  pinMode(InA1,OUTPUT);
  pinMode(InB1,OUTPUT);
  pinMode(PWM1,OUTPUT);
  pinMode(InA2,OUTPUT);
  pinMode(InB2,OUTPUT);
  pinMode(PWM2,OUTPUT);
  pinMode(InA3,OUTPUT);
  pinMode(InB3,OUTPUT);
  pinMode(PWM3,OUTPUT);
  pinMode(InA4,OUTPUT);
  pinMode(InB4,OUTPUT);
  pinMode(PWM4,OUTPUT);
  pinMode(switch01,INPUT);
  pinMode(switch02,INPUT);
}
void loop() 
{
  switchstate01 = digitalRead(switch01);
  switchstate02 = digitalRead(switch02);
  if (switchstate01 == HIGH)
  {
    digitalWrite(InA1,LOW);
    digitalWrite(InB1,HIGH);
    analogWrite(PWM1,255);
  }
  if (switchstate02 == HIGH)
  {
    digitalWrite(InA1,HIGH);
    digitalWrite(InB1,LOW);
    analogWrite(PWM1,255);
  }
/*
  digitalWrite(InA2,LOW);
  digitalWrite(InB2,HIGH);
  analogWrite(PWM2,0);
  digitalWrite(InA3,LOW);
  digitalWrite(InB3,HIGH);
  analogWrite(PWM3,0);
  digitalWrite(InA4,LOW);
  digitalWrite(InB4,HIGH);
  analogWrite(PWM4,0);
  */    
}

Hello.

You don’t actually have any code to stop the motors:

if switch 1 pressed
run motor in one direction
else if switch 2 pressed
run motor in the other direction
else
stop motor

- Ben

I got the code working now. I got confused by a photo of Arduino Mega 2560 R3 board with labeled pinouts. Are pins 23, 25, 27, and 29 digital pins, not 5V pins? I think that two pins over pins 22 and 23 on the double header on the right side of the board are 5V pins.

int InA1 = 22;
int InB1 = 24;
int PWM1 = 4;
int InA2 = 26;
int InB2 = 28;
int PWM2 = 5;
int InA3 = 30;
int InB3 = 32;
int PWM3 = 6;
int InA4 = 34;
int InB4 = 36;
int PWM4 = 7;
int switch01 = 38;
int switch02 = 39;
int switch03 = 40;
int switch04 = 41;
int switch05 = 42;
int switch06 = 43;
int switch07 = 44;
int switch08 = 45;
int switch09 = 46;
int switch10 = 47;
int switchstate01 = 0;
int switchstate02 = 0;
int switchstate03 = 0;
int switchstate04 = 0;
int switchstate05 = 0;
int switchstate06 = 0;
int switchstate07 = 0;
int switchstate08 = 0;
int switchstate09 = 0;
int switchstate10 = 0;
void setup() 
{
  Serial.begin(115200);
  pinMode(InA1,OUTPUT);
  pinMode(InB1,OUTPUT);
  pinMode(PWM1,OUTPUT);
  pinMode(InA2,OUTPUT);
  pinMode(InB2,OUTPUT);
  pinMode(PWM2,OUTPUT);
  pinMode(InA3,OUTPUT);
  pinMode(InB3,OUTPUT);
  pinMode(PWM3,OUTPUT);
  pinMode(InA4,OUTPUT);
  pinMode(InB4,OUTPUT);
  pinMode(PWM4,OUTPUT);
  pinMode(switch01,INPUT);
  pinMode(switch02,INPUT);
  pinMode(switch03,INPUT);
  pinMode(switch04,INPUT);
  pinMode(switch05,INPUT);
  pinMode(switch06,INPUT);
  pinMode(switch07,INPUT);
  pinMode(switch08,INPUT);
  pinMode(switch09,INPUT);
  pinMode(switch10,INPUT);
}
void loop() 
{
  switchstate01 = digitalRead(switch01);
  switchstate02 = digitalRead(switch02);
  switchstate03 = digitalRead(switch03);
  switchstate04 = digitalRead(switch04);
  switchstate05 = digitalRead(switch05);
  switchstate06 = digitalRead(switch06);
  switchstate07 = digitalRead(switch07);
  switchstate08 = digitalRead(switch08);
  switchstate09 = digitalRead(switch09);
  switchstate10 = digitalRead(switch10);
  if (switchstate01 == HIGH)
  {
    digitalWrite(InA1,HIGH);
    digitalWrite(InB1,LOW);
    analogWrite(PWM1,255);
    Serial.print("Switch 1 HIGH ");
  }
  if (switchstate02 == HIGH)
  {
    digitalWrite(InA1,LOW);
    digitalWrite(InB1,HIGH);
    analogWrite(PWM1,255);
    Serial.print("Switch 2 HIGH ");
  }
  if (switchstate01 == LOW && switchstate02 == LOW)
  {
    analogWrite(PWM1,0);
    Serial.print("Switch 1 & 2 LOW ");
  }
  if (switchstate03 == HIGH)
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 3 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 3 LOW ");
  }
  if (switchstate04 == HIGH)
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.print("Switch 4 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.print("Switch 4 LOW ");
  }
  if (switchstate05 == HIGH)
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 5 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 5 LOW ");
  }
  if (switchstate06 == HIGH)
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.print("Switch 6 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.print("Switch 6 LOW ");
  }
  if (switchstate07 == HIGH)
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 7 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 7 LOW ");
  }
  if (switchstate08 == HIGH)
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.print("Switch 8 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.print("Switch 8 LOW ");
  }
  if (switchstate09 == HIGH)
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 9 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,LOW);
    //digitalWrite(InB1,HIGH);
    //analogWrite(PWM1,255);
    Serial.print("Switch 9 LOW ");
  }
  if (switchstate10 == HIGH)
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.println("Switch 10 HIGH ");
  }
  else
  {
    //digitalWrite(InA1,HIGH);
    //digitalWrite(InB1,LOW);
    //analogWrite(PWM1,255);
    Serial.println("Switch 10 LOW ");
  }
  
/*
  digitalWrite(InA2,LOW);
  digitalWrite(InB2,HIGH);
  analogWrite(PWM2,0);
  digitalWrite(InA3,LOW);
  digitalWrite(InB3,HIGH);
  analogWrite(PWM3,0);
  digitalWrite(InA4,LOW);
  digitalWrite(InB4,HIGH);
  analogWrite(PWM4,0);
  */    
}

I’m glad you got it working.

I do not know what you are asking here. It sounds like you might be trying to ask in a really bad way whether those pins are hardware PWM outputs. That should be documented fairly clearly on the Arduino web site.

- Ben

The photo is at arduino.cc/forum/index.php/topic,125908.0.html
I was confused by the labels for the dual header on the right side of the board.