Guest User

Untitled

a guest
Apr 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. const int switchPin = 2; // switch input
  2. const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
  3. const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
  4. const int enablePin = 9; // H-bridge enable pin
  5.  
  6. void setup() {
  7. // set the switch as an input:
  8. pinMode(switchPin, INPUT);
  9. // set all the other pin,s you're using as outputs:
  10. pinMode(motor1Pin, OUTPUT);
  11. pinMode(motor2Pin, OUTPUT);
  12. pinMode(enablePin, OUTPUT);
  13. // set enablePin high so that motor can turn on:
  14. digitalWrite(enablePin, HIGH);
  15. }
  16.  
  17.  
  18. void loop() {
  19. int button = digitalRead(switchPin);
  20.  
  21. // if the switch is high, motor will turn on one direction
  22. if (button == HIGH){
  23. // set leg 1 of the H-bridge low and set leg 2 of the H-bridge high
  24. digitalWrite(motor1Pin, LOW);
  25. digitalWrite(motor2Pin, HIGH);
  26. } else {
  27. // else (which means the switch is low), motor will turn in the other direction
  28. // set leg 1 of the H-bridge high and set leg 2 of the H-bridge low
  29. digitalWrite(motor1Pin, HIGH);
  30. digitalWrite(motor2Pin, LOW);
  31. }
  32.  
  33. }
Add Comment
Please, Sign In to add comment