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