Advertisement
Guest User

DC Motors Day 3

a guest
Jan 28th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 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. bool oldDir=false;
  11. bool newDir=false;
  12. bool enable=false;
  13. bool dir=false;
  14. int motorSpeed;
  15. void setup() {
  16. pinMode(enablePin,OUTPUT);
  17. pinMode(controlPin1,OUTPUT);
  18. pinMode(controlPin2,OUTPUT);
  19. pinMode(directionPin,INPUT);
  20. pinMode(switchOnOff,INPUT);
  21. pinMode(potPin,INPUT);
  22. Serial.begin (9600);
  23. }
  24.  
  25. void loop() {
  26. newPressON=digitalRead(switchOnOff);
  27. Serial.println (newPressON);
  28. delay (5);
  29. if(newPressON==1 && newPressON !=oldPressON)
  30. {
  31. enable=!enable;
  32. }
  33.  
  34. newDir=digitalRead(directionPin);
  35. delay (5);
  36. if (newDir ==1 && newDir != oldDir)
  37. {
  38. dir=!dir;
  39. }
  40. if (enable)
  41. {
  42. motorSpeed=analogRead(potPin);
  43. motorSpeed=map(motorSpeed, 0, 1023, 0, 255);
  44. if (dir)
  45. {
  46. digitalWrite(controlPin1, HIGH);
  47. digitalWrite(controlPin2, LOW);
  48. analogWrite(enablePin, motorSpeed);
  49. }
  50. else
  51. {
  52. digitalWrite(controlPin1, LOW);
  53. digitalWrite(controlPin2, HIGH);
  54. analogWrite(enablePin, motorSpeed);
  55. }
  56. }
  57. else
  58. {
  59. analogWrite(enablePin, 0);
  60. }
  61. oldPressON=newPressON;
  62. oldDir=newDir;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement