Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This sketch will allow you to control an H-Bridge IC
- Controling an H-Bridge allows you to make a motor switch directions
- In our case, a switch will allow us to switch directions
- Here are a few links about H-Bridges:
- http://itp.nyu.edu/physcomp/Labs/DCMotorControl
- http://en.wikipedia.org/wiki/H_bridge
- */
- int enablePin = 9;// turns the h bridge on/off
- int motorLogicAPin = 3;
- int motorLogicBPin = 4;
- int ledPin = 13;
- int directionPin = 7;
- boolean spinForward = true;
- boolean hasSwitchBeenReleased = true;
- void setup(){
- Serial.begin(9600);
- // set pin modes:
- //outs
- pinMode( enablePin, OUTPUT);
- pinMode( motorLogicAPin, OUTPUT);
- pinMode( motorLogicBPin, OUTPUT);
- pinMode( ledPin, OUTPUT);
- //ins
- pinMode( directionPin, INPUT);
- // set enable pin high
- digitalWrite( enablePin, HIGH);
- // set LED pin low
- digitalWrite( ledPin, LOW);
- }
- void loop(){
- checkDirectionSwitch();
- driveMotor();
- }
- void checkDirectionSwitch(){
- // read the directionPin, if it is pressed(HIGH)
- if( digitalRead(directionPin) == HIGH ){
- // then check to see if it has been freshly pressed
- if( hasSwitchBeenReleased == true ){
- // then make note that it has not been freshly pressed
- hasSwitchBeenReleased = false;
- Serial.println("switch has been pressed");
- // and change the motor spinning direction to the opposite
- // of the current spinning direction
- if( spinForward == true ){
- spinForward = false;
- }
- else{
- spinForward = true;
- }
- // signal change in direction by turning on
- // LED for a 10th of a second
- digitalWrite( ledPin, HIGH);
- delay(100);
- digitalWrite( ledPin, LOW);
- }
- }
- else{
- // if the switch is released(LOW)
- // make sure that it will read as being freshly pressed
- // next time it is pressed
- hasSwitchBeenReleased = true;
- if(spinForward == true){
- Serial.println("forward");
- }else{
- Serial.println("backward");
- }
- }
- }
- void driveMotor(){
- if( spinForward == true ){
- // driving one logic pin high and the other low
- // will make the motor spin a direction
- digitalWrite( motorLogicAPin, HIGH);
- digitalWrite( motorLogicBPin, LOW);
- }
- else {
- // flip flop the polarity on the two logic pins to make
- // the motor spin in the opposite direction
- digitalWrite( motorLogicAPin, LOW);
- digitalWrite( motorLogicBPin, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement