Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int switchPin = 2; // switch input
- const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
- const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
- const int enablePin = 9; // H-bridge enable pin
- void setup() {
- // set the switch as an input:
- pinMode(switchPin, INPUT);
- // set all the other pin,s you're using as outputs:
- pinMode(motor1Pin, OUTPUT);
- pinMode(motor2Pin, OUTPUT);
- pinMode(enablePin, OUTPUT);
- // set enablePin high so that motor can turn on:
- digitalWrite(enablePin, HIGH);
- }
- void loop() {
- int button = digitalRead(switchPin);
- // if the switch is high, motor will turn on one direction
- if (button == HIGH){
- // set leg 1 of the H-bridge low and set leg 2 of the H-bridge high
- digitalWrite(motor1Pin, LOW);
- digitalWrite(motor2Pin, HIGH);
- } else {
- // else (which means the switch is low), motor will turn in the other direction
- // set leg 1 of the H-bridge high and set leg 2 of the H-bridge low
- digitalWrite(motor1Pin, HIGH);
- digitalWrite(motor2Pin, LOW);
- }
- }
Add Comment
Please, Sign In to add comment