I just got an arduino motor shield (http://arduino.cc/en/Main/ArduinoMotorShieldR3) but the brake does not seem to be turning on when I want it to. I know that the brake works because when I write a simple test code I can make it turn on and off but for some reason whenever I try to control it with a button it never turns on. here is my code, and any help would be really appreciated. Thanks
int motorADirectionPin = 12; //Boolean, high = forward
int motorABrakePin = 9; //boolean, low = allow to run
int motorASpeedPin = 3; //pwm output for speed, 0-255
const int buttonPin1 = 2; // the number of the pushbutton pin 2
const int buttonPin2 = 7; // the number of the pushbutton pin 3
boolean lastButton1 = LOW;
boolean currentButton1 = LOW;
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean brake = HIGH;
boolean dir = HIGH;
int currentSpeed;void setup() {
pinMode(motorADirectionPin,OUTPUT); //set up motor pin
pinMode(motorABrakePin,OUTPUT); //set up motor pin
pinMode(motorADirectionPin,OUTPUT); //set up motor pin
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
currentSpeed = 0;
}boolean debounce2(boolean last)
{
boolean current = digitalRead(buttonPin1);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin1);
}
return current;
}
boolean debounce1(boolean last)
{
boolean current = digitalRead(buttonPin2);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin2);
}
return current;
}void loop(){
currentButton1 = debounce1(lastButton1);
currentButton2 = debounce2(lastButton2);
if (lastButton1 == LOW && currentButton1 == HIGH && currentSpeed == 0)
{
currentSpeed = 100;
brake = LOW;
dir = HIGH;
}
else if (lastButton2 == LOW && currentButton2 == HIGH && currentSpeed == 0)
{
currentSpeed = 100;
brake = LOW;
dir = LOW;
}
else if (lastButton1 == LOW && currentButton1 == HIGH && currentSpeed > 1)
{
currentSpeed = 0;
brake = HIGH;}
else if (lastButton2 == LOW && currentButton2 == HIGH && currentSpeed > 1)
{
currentSpeed = 0;
brake = HIGH;}
analogWrite (motorASpeedPin, currentSpeed);
digitalWrite (motorABrakePin, brake);
digitalWrite (motorADirectionPin, dir);
lastButton1 = currentButton1;
lastButton2 = currentButton2;
}