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 = 5; // 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);
- Serial.begin(9600);
- }
- void loop() {
- // if the switch is high, motor will turn on one direction
- Serial.println(digitalRead(switchPin));
- if(digitalRead(switchPin) == HIGH){
- // set leg 1 of the H-bridge high
- digitalWrite(motor1Pin, HIGH);
- // set leg 2 of the H-bridge low
- analogWrite(motor2Pin, 0);
- }
- // else (which means the switch is low), motor will turn in the other direction
- else{
- // set leg 2 of the H-bridge low
- digitalWrite(motor1Pin, LOW);
- // set leg 1 of the H-bridge high
- analogWrite(motor2Pin, 100);
- }
- }
Add Comment
Please, Sign In to add comment