Advertisement
3rdWard_Arduino

class3_hbridge_control_motorSpeed_PWM

Apr 1st, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. /*
  2. This sketch will allow you to control an H-Bridge IC
  3.  Controling an H-Bridge allows you to make a motor switch directions
  4.  
  5.  In our case, a switch will allow us to switch directions
  6.  
  7.  Here are a few links about H-Bridges:
  8.  http://itp.nyu.edu/physcomp/Labs/DCMotorControl
  9.  http://en.wikipedia.org/wiki/H_bridge
  10.  */
  11.  
  12. int enablePin = 9;// turns the h bridge on/off
  13. int motorLogicAPin = 3;
  14. int motorLogicBPin = 4;
  15. int ledPin = 13;
  16. int directionPin = 7;
  17. int potPin = A0;// pin for the potentiometer
  18.  
  19. int potVal = 0;// 0-1023
  20. int pwmVal = 0;// 0-255
  21.  
  22. boolean spinForward = true;
  23. boolean hasSwitchBeenReleased = true;
  24.  
  25. void setup(){
  26.   Serial.begin(9600);
  27.  
  28.   // set pin modes:
  29.   //outs
  30.   pinMode( enablePin, OUTPUT);
  31.   pinMode( motorLogicAPin, OUTPUT);
  32.   pinMode( motorLogicBPin, OUTPUT);
  33.   pinMode( ledPin, OUTPUT);
  34.   //ins
  35.   pinMode( directionPin, INPUT);
  36.  
  37.   // set enable pin high
  38.   digitalWrite( enablePin, HIGH);
  39.   // set LED pin low
  40.   digitalWrite( ledPin, LOW);
  41. }
  42.  
  43. void loop(){
  44.  
  45.   checkDirectionSwitch();
  46.  
  47.   motorSpeed();
  48.  
  49.   driveMotor();
  50.  
  51. }
  52.  
  53. void checkDirectionSwitch(){
  54.   // read the directionPin, if it is pressed(HIGH)
  55.   if( digitalRead(directionPin) == HIGH ){
  56.     // then check to see if it has been freshly pressed
  57.     if( hasSwitchBeenReleased == true ){
  58.       // then make note that it has not been freshly pressed
  59.       hasSwitchBeenReleased = false;
  60.       Serial.println("switch has been pressed");
  61.       // and change the motor spinning direction to the opposite
  62.       // of the current spinning direction
  63.       if( spinForward == true ){
  64.         spinForward = false;
  65.       }
  66.       else{
  67.         spinForward = true;
  68.       }
  69.       // signal change in direction by turning on
  70.       // LED for a 10th of a second
  71.       digitalWrite( ledPin, HIGH);
  72.       delay(100);
  73.       digitalWrite( ledPin, LOW);
  74.     }
  75.   }
  76.   else{
  77.     // if the switch is released(LOW)
  78.     // make sure that it will read as being freshly pressed
  79.     // next time it is pressed
  80.     hasSwitchBeenReleased = true;
  81.     if(spinForward == true){
  82.       Serial.println("forward");
  83.     }
  84.     else{
  85.       Serial.println("backward");
  86.     }
  87.   }
  88. }
  89.  
  90. void motorSpeed(){
  91.   // sets the motor speed based on the potentiometer
  92.   potVal = analogRead(potPin);
  93.   pwmVal = map( potVal, 0, 1023, 0, 255);
  94.  
  95.   // pwm enable pin using the pwmVal
  96.   analogWrite( enablePin, pwmVal);
  97. }
  98.  
  99. void driveMotor(){
  100.   if( spinForward == true ){
  101.     // driving one logic pin high and the other low
  102.     // will make the motor spin a direction
  103.     digitalWrite( motorLogicAPin, HIGH);
  104.     digitalWrite( motorLogicBPin, LOW);
  105.   }
  106.   else {
  107.     // flip flop the polarity on the two logic pins to make
  108.     // the motor spin in the opposite direction
  109.     digitalWrite( motorLogicAPin, LOW);
  110.     digitalWrite( motorLogicBPin, HIGH);
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement