Pages: [1]   Go Down
Author Topic: Arduino motor Shield brake problem  (Read 298 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Quote
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;
}



Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok this seems to happen to me a lot, but right after I think I cant figure it out and ask for help I find the solution. So if anyone is having this problem, you cant control the motor speed and the brake at the same time. I thought it would be better if I set the speed to 0 and then turn the brake on while its coasting, but the hardware is designed so that if you want to stop you have to just hit the brake. so now my if statements look more like this.

Code:
if (lastButton1 == LOW && currentButton1 == HIGH && brake == HIGH)
   {
     currentSpeed = 255;
     brake = LOW;
     dir = HIGH;
   }   
  else if (lastButton2 == LOW && currentButton2 == HIGH && brake == HIGH)
  {
    currentSpeed = 255;
    brake = LOW;
     dir = LOW;
  }
  else if (lastButton1 == LOW && currentButton1 == HIGH && currentSpeed > 1)
  {
    brake = HIGH;
   
  }
  else if (lastButton2 == LOW && currentButton2 == HIGH && currentSpeed > 1)
  {
    brake = HIGH;
   
  }
  analogWrite (motorASpeedPin, currentSpeed);
  digitalWrite (motorABrakePin, brake);
  digitalWrite (motorADirectionPin, dir);
  lastButton1 = currentButton1;
  lastButton2 = currentButton2;
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok this seems to happen to me a lot, but right after I think I cant figure it out and ask for help I find the solution.       

Happens to me frequently too!
Logged

Pages: [1]   Go Up
Jump to: