Advertisement
britneybeatey

Motor w/ Buttons

Jun 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. const int controlPin1 =;
  2. const int controlPin2 = ;
  3. const int enablePin = ;
  4. const int directionSwitchPin = ;
  5. const onOffSwitchStateSwitchPin = ;
  6. const int potPin = A0;
  7.  
  8. int onOffSwitchState = 0;
  9. int previousOnOffSwitchState = 0;
  10. int directionSwitchState = 0;
  11. int previousDirectionSwitch State = 0;
  12. int motorEnabled = 0;
  13. int motorSpeed = 0;
  14. int motorDirection = 1;
  15.  
  16. void setup() {
  17. pinMode(directionSwitchPin, INPUT);
  18. pinMode(onOffSwitchStateSwitchPin, INPUT);
  19. pinMode(controlPin1, OUTPUT);
  20. pinMode(controlPin2, OUTPUT);
  21. pinMode(enablePin, OUTPUT);
  22. digitalWrite(enablePin, OUTPUT);
  23. }
  24.  
  25. void loop() {
  26. onOffSwitchState =
  27. digitalRead(onOffSwitchStateSwitchPin0;
  28. delay(1);
  29. directionSwitchState =
  30. digitalRead(directionSwitchPin);
  31. if(onOffSwitchState !=previousOnOffSwitchState {
  32. if(onOffSwitchState ==HIGH) {
  33. motorEnabled = !motorEnabled;
  34. }
  35. }
  36. if(directionSwitchState! =
  37. previousDirectionSwitchState) {
  38. if(directionSwitchState == HIGH){
  39. motorDirection = !motorDirection;
  40. }
  41. }
  42. if(motorDirection ==1) {
  43. digitalWrite(controlPin1, HIGH);
  44. digitalWrite(controlPin2, LOW);
  45. }
  46. else {
  47. digitalWrite (controlPin1, LOW);
  48. digitalWrite (controlPin2, HIGH);
  49. }
  50. if (motorEnabled == 1) {
  51. analogWrite(enablePin, motorSpeed);
  52. }
  53. else {
  54. analogWrite(enablePin, 0);
  55. }
  56. previousDirectionSwitchState =
  57. directionSwitchState;
  58. previousOnOffSwitchState = onOffSwitchState;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement