Advertisement
ambersy314

Untitled

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