Hello, this is my first time using a motor shield and the design simply required the motor to turn clockwise and then anticlockwise when a switch is pressed.
When there is no switch, the motor works fine with the following code:
void setup() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
}
void loop() {
Serial.begin (9600);
analogWrite(9,250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(1000);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(1000);
analogWrite(9,0);
delay(1000);
}
The problem now is that it doesn't work after adding in a push button.
int switchState = 0;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
}
void loop()
{
switchState = digitalRead (2);
if (switchState == HIGH)
{
analogWrite(9,230);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(1000);
analogWrite(9,230);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(1000);
analogWrite(9,0);
delay(1000);
}
}
Please help me to figure out how to make this thing work. Thanks.