3rdWard_Arduino

class4_hbridge_control

Apr 1st, 2012
205
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  18. boolean spinForward = true;
  19. boolean hasSwitchBeenReleased = true;
  20.  
  21. void setup(){
  22.   Serial.begin(9600);
  23.  
  24.   // set pin modes:
  25.   //outs
  26.   pinMode( enablePin, OUTPUT);
  27.   pinMode( motorLogicAPin, OUTPUT);
  28.   pinMode( motorLogicBPin, OUTPUT);
  29.   pinMode( ledPin, OUTPUT);
  30.   //ins
  31.   pinMode( directionPin, INPUT);
  32.  
  33.   // set enable pin high
  34.   digitalWrite( enablePin, HIGH);
  35.   // set LED pin low
  36.   digitalWrite( ledPin, LOW);
  37. }
  38.  
  39. void loop(){
  40.   checkDirectionSwitch();
  41.  
  42.   driveMotor();
  43.  
  44. }
  45.  
  46. void checkDirectionSwitch(){
  47.   // read the directionPin, if it is pressed(HIGH)
  48.   if( digitalRead(directionPin) == HIGH ){
  49.     // then check to see if it has been freshly pressed
  50.     if( hasSwitchBeenReleased == true ){
  51.       // then make note that it has not been freshly pressed
  52.       hasSwitchBeenReleased = false;
  53.       Serial.println("switch has been pressed");
  54.       // and change the motor spinning direction to the opposite
  55.       // of the current spinning direction
  56.       if( spinForward == true ){
  57.         spinForward = false;
  58.       }
  59.       else{
  60.         spinForward = true;
  61.       }
  62.       // signal change in direction by turning on
  63.       // LED for a 10th of a second
  64.       digitalWrite( ledPin, HIGH);
  65.       delay(100);
  66.       digitalWrite( ledPin, LOW);
  67.     }
  68.   }
  69.   else{
  70.     // if the switch is released(LOW)
  71.     // make sure that it will read as being freshly pressed
  72.     // next time it is pressed
  73.     hasSwitchBeenReleased = true;
  74.     if(spinForward == true){
  75.       Serial.println("forward");
  76.     }else{
  77.       Serial.println("backward");
  78.     }
  79.   }
  80. }
  81.  
  82. void driveMotor(){
  83.   if( spinForward == true ){
  84.     // driving one logic pin high and the other low
  85.     // will make the motor spin a direction
  86.     digitalWrite( motorLogicAPin, HIGH);
  87.     digitalWrite( motorLogicBPin, LOW);
  88.   }
  89.   else {
  90.     // flip flop the polarity on the two logic pins to make
  91.     // the motor spin in the opposite direction
  92.     digitalWrite( motorLogicAPin, LOW);
  93.     digitalWrite( motorLogicBPin, HIGH);
  94.   }
  95. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×