Guest User

Untitled

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