Advertisement
Vendrick-Xander

H Bridge Motor Board Comp Code

Jan 27th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. const int onOffB = 2;
  2. const int rotateB = 3;
  3. const int potIn = A0;
  4. const int speedOutPin = 6;
  5. const int directionPinOut = 4;
  6. const int onOffOut = 5;
  7. bool onOff = false;
  8. bool CW_CCW = true;
  9. bool debugOff = false;
  10. void setup() {
  11. pinMode(onOffB, INPUT);
  12. pinMode(rotateB, INPUT);
  13. pinMode(potIn, INPUT);
  14. pinMode(speedOutPin, OUTPUT);
  15. pinMode(directionPinOut, OUTPUT);
  16. pinMode(onOffOut, OUTPUT);
  17. Serial.begin(9600);
  18. do
  19. {
  20. Serial.println("Direction true is CW");
  21. }
  22. while(!Serial);
  23. }
  24.  
  25. void loop() {
  26. onOff = debounce(onOff, onOffB);
  27. CW_CCW = debounce(CW_CCW, rotateB);
  28. int speedVal = analogRead(potIn);
  29. if(onOff)
  30. {
  31. debugOff = false;
  32. digitalWrite(onOffOut, HIGH);
  33. motorRun(CW_CCW, speedVal);
  34. Serial.println("Entered IF");
  35. }
  36. else
  37. {
  38. digitalWrite(onOffOut, LOW);
  39. debugOff = true;
  40. motorRun(CW_CCW, 0);
  41. }
  42. }
  43. void motorRun(bool dir, int speedVal)
  44. {
  45. speedVal = map(speedVal, 0, 1023, 0 , 255);
  46. digitalWrite(directionPinOut, dir);
  47. analogWrite(speedOutPin, speedVal);
  48. if(!debugOff)
  49. {
  50. Serial.println(dir);
  51. Serial.println(speedVal);
  52. }
  53. }
  54. bool debounce(bool prev, int pin)
  55. {
  56. bool currB = digitalRead(pin);
  57. delay(30);
  58. bool current = digitalRead(pin);
  59. if(currB != current)
  60. {
  61. prev = !prev;
  62. }
  63. return prev;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement