Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Aidan Rafferty
- Object Lab 05
- Part 2
- */
- 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 pins 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() {
- // if the switch is high, motor will turn on one direction
- int switchVal = digitalRead(switchPin);
- if(switchVal == 1)
- {
- digitalWrite(motor1Pin, 1);
- digitalWrite(motor2Pin, 0);
- }
- else
- {
- digitalWrite(motor1Pin, 0);
- digitalWrite(motor2Pin, 1);
- }
- // set leg 1 of the H-bridge low
- // set leg 2 of the H-bridge high
- // else (which means the switch is low), motor will turn in the other direction
- // set leg 1 of the H-bridge high
- // set leg 2 of the H-bridge low
- }
Add Comment
Please, Sign In to add comment