Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. const int controlPin1 =2;
  2. const int controlPin2 =3;
  3. const int enablePin =9;
  4. const int directionPin =4;
  5. const int switchOnOff =5;
  6. const int potPin =A0;
  7. int readVal;
  8. bool oldPressON =false;
  9. bool newPressON = false;
  10. // used to distinguish btw button presses - if the old and the new values are different, it's a new press, if it is the same value, then it is not a new press
  11. //booleans are either true or false, 0 or 1 -- on or off
  12. //need to define two new boolean vars for the direction button too
  13. bool oldDir = false;
  14. bool newDir = false;
  15. bool enable = false;
  16. bool dir =false;
  17. int motorSpeed;
  18. // to make motor on or off
  19.  
  20. void setup() {
  21. // put your setup code here, to run once:
  22. pinMode (enablePin, OUTPUT);
  23. pinMode (controlPin1, OUTPUT);
  24. pinMode (controlPin2, OUTPUT);
  25. pinMode (directionPin, INPUT);
  26. pinMode (switchOnOff, INPUT);
  27. pinMode (potPin, INPUT);
  28. Serial.begin (9600);
  29.  
  30. }
  31.  
  32. void loop() {
  33.  
  34. newPressON=digitalRead (switchOnOff);
  35. delay (5);
  36. //tested button that controls on or off
  37. if (newPressON ==1 && newPressON != oldPressON)
  38. {
  39. enable = !enable;
  40. //! means opposite (true to false, and false to true)
  41. //booleans are digital inputs
  42. }
  43.  
  44. newDir = digitalRead (directionPin);
  45. delay (5);
  46. if(newDir == 1 && newDir != oldDir)
  47. {
  48. dir = !dir;
  49. }
  50. if (enable)
  51. {
  52. motorSpeed = analogRead(potPin);
  53. motorSpeed =map(motorSpeed, 0, 1023, 0, 255);
  54. if (dir)
  55. {
  56. digitalWrite(controlPin1, HIGH);
  57. digitalWrite(controlPin2, LOW);
  58. analogWrite(enablePin, motorSpeed);
  59. }
  60. else
  61. {
  62. digitalWrite(controlPin1, LOW);
  63. digitalWrite(controlPin2, HIGH);
  64. analogWrite(enablePin, motorSpeed);
  65. }
  66. }
  67. else
  68. {
  69. analogWrite(enablePin,0);
  70. }
  71. oldPressON =newPressON;
  72. oldDir =newDir;
  73. //tested button that controls direction
  74. if (newDir ==1)
  75. {
  76. enable = !enable;
  77. }
  78.  
  79. readVal = analogRead(potPin);
  80. readVal = map(readVal, 0, 1023, 0, 255);
  81. }
  82. //tested potentiometer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement