switches for motor control problem

Hi, I'm using the Arduino as a part of a uni project, i am using it to control a motor. (I have a motor shield). Using 3 switches I want to be able to either turn the motor on, reverse it or stop it, depending on the state of the switches. I have successfully used one switch to control the direction of the motor either forward or reverse. However i have been unable to incorporate more than one switch in controlling the motor.

Here is the code and the circuit, i am unsure as to why it is not working. If anyone has any idea why, or any suggestions on how to connect up the 3 switches it would be much appreciated.

#define PwmPinMotorA 10
#define DirectionPinMotorA 12
const int buttonPin2 = 2; 
const int buttonPin4 = 4;
const int buttonPin6 = 6;

int buttonState2 = 0;  
int buttonState4 = 0;   
int buttonState6 = 0;   

void setup() {

  pinMode(PwmPinMotorA, OUTPUT);
  pinMode(DirectionPinMotorA, OUTPUT);
  pinMode(buttonPin2, INPUT);    
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin6, INPUT); 
 
}

void loop(){
  
  buttonState2 = digitalRead(buttonPin2);
  buttonState4 = digitalRead(buttonPin4);
  buttonState6 = digitalRead(buttonPin6);

 
     if (buttonState2 == HIGH && buttonState4 == HIGH && buttonState6 == HIGH) {     
        //Car Moves forward
       analogWrite(PwmPinMotorA, 255);
       digitalWrite(DirectionPinMotorA, LOW);  
          } 
      else if(buttonState2 == LOW && buttonState4 == HIGH && buttonState6 == HIGH){
        // Car stops:
         analogWrite(PwmPinMotorA, 0);
         digitalWrite(DirectionPinMotorA, HIGH);
      }
    
      else if(buttonState2 == LOW && buttonState4 == LOW && buttonState6 == HIGH){
        // Car Reverses
         analogWrite(PwmPinMotorA, 255);
         digitalWrite(DirectionPinMotorA, HIGH);
      }
      else if(buttonState2 == HIGH && buttonState4 == LOW && buttonState6 == HIGH){
        // Car Reverses:       
         analogWrite(PwmPinMotorA, 255);
         digitalWrite(DirectionPinMotorA, HIGH);
      }
       else if(buttonState2 == HIGH && buttonState4 == LOW && buttonState6 == LOW){
        // Car Stops
         analogWrite(PwmPinMotorA, 0);
         digitalWrite(DirectionPinMotorA, HIGH); 
      }
      else {
         analogWrite(PwmPinMotorA, 255);
         digitalWrite(DirectionPinMotorA, HIGH);
      }
    }

please excuse my poor circuit diagram
All resistors are 1k

The first thing I would do is add a Serial.begin(9600); statement to setup(). Then, add this code after reading the button states:

if(buttonState2 == HIGH)
   Serial.println("Button 2 is HIGH");
else
   Serial.println("Button 2 is LOW");

if(buttonState4 == HIGH)
   Serial.println("Button 4 is HIGH");
else
   Serial.println("Button 4 is LOW");

if(buttonState6 == HIGH)
   Serial.println("Button 6 is HIGH");
else
   Serial.println("Button 6 is LOW");

Confirm that pressing a button makes it HIGH and that releasing it makes it LOW.

Don't forget to open the Serial Monitor window.

If that works, then the problem is in the remaining software. Otherwise, the problem is in the hardware. You've cut the problem scope in half with this simple step.

Thanks a heap, using this i have discovered that the problem is in the hardware, not sure what the problem is, i asked an electronical engineer but he could not see anything wrong with it. I've tried putting diodes in between the switches and ground but this hasnt worked anyway thank you very much for your help.

Post a sketch, a photo, or something that illustrates how the switches are connected to the Arduino. Resistors may be required, not diodes.

R2D2 was an electronical engineer. Perhaps you talked to an electrical engineer. If so, he/she probably shouldn't be practicing.

I often find that staements like these:

if (buttonState2 == HIGH && buttonState4 == HIGH && buttonState6 == HIGH) 
...
if(buttonState2 == LOW && buttonState4 == HIGH && buttonState6 == HIGH){
...
if(buttonState2 == LOW && buttonState4 == LOW && buttonState6 == HIGH){

can be more easily understood if you consider the states as binary numbers:

int buttonState = (digitalRead(buttonPin2) << 2) |
                  (digitalRead(buttonPin4) << 1) |
                   digitalRead(buttonPin6);

switch (buttonState) {
    case B111: 
        //Car Moves forward
       analogWrite(PwmPinMotorA, 255);
       digitalWrite(DirectionPinMotorA, LOW);  
    break;

    case B011:
    case B100:
        analogWrite(PwmPinMotorA, 0);
        digitalWrite(DirectionPinMotorA, HIGH);
    break;
    
    case B001:
    case B101:
        // Car Reverses
       analogWrite(PwmPinMotorA, 255);
       digitalWrite(DirectionPinMotorA, HIGH);
    break;
  
    default:
        analogWrite(PwmPinMotorA, 255);
        digitalWrite(DirectionPinMotorA, HIGH);
    break;   
}