Guest User

Untitled

a guest
Apr 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. //The first step was to initiallize my pins.
  2. //They are named, and are addressed to the pins which I use on the board
  3.  
  4. const int switchPin = 2; // switch input, I am using a button
  5. const int motor1Pin = 3; // For the H-bridge first pin
  6. const int motor2Pin = 4; // For the H-bridge second pin
  7. const int enablePin = 9; // Enable pin
  8.  
  9. void setup() {
  10. //The first step was to initialize which pins were inputs and
  11. //which were outputs.
  12. pinMode(switchPin, INPUT);//pin 2
  13. pinMode(motor1Pin, OUTPUT);//pin 3
  14. pinMode(motor2Pin, OUTPUT);//pin 4
  15. pinMode(enablePin, OUTPUT);// pin 9
  16. //Next, I want to set the enable pin, the on. This means, as soon as
  17. //I m connected to power, the motor will turn on, and spin in one
  18. //direction
  19. digitalWrite(enablePin, HIGH);
  20. }
  21.  
  22. void loop() {
  23. //Now I want my board to read if the button is being pushed or not.
  24. // if it IS, then the switchpin is HIGH, and it will enter this loop
  25. if (digitalRead(switchPin) == HIGH) {
  26. //first I set the first H-bridge pin to HIGH, or sending power
  27. //to that side so it will spin in one direction
  28. digitalWrite(motor1Pin, HIGH);
  29. //Then I set the second H-bridge pin to LOW, so it will on spin
  30. //one way.
  31. digitalWrite(motor2Pin, LOW);
  32. }
  33.  
  34. // if the button is not being pressed, I was the motor to spin the
  35. //opposite direction, So I just need to switch which H-bridge pin
  36. //is HIGH.
  37. else {
  38. digitalWrite(motor2Pin, HIGH);
  39. digitalWrite(motor1Pin, LOW);
  40. }
  41. }
Add Comment
Please, Sign In to add comment