Engrinovations

Working BTS760 Sketch for testing motor control with a D1 Mini

Mar 21st, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <BTS7960.h>
  2. // Working with new pins 03/26/21 G Liebig. Note: you must modify Library to increase PWM from 255 to 1023
  3. /*
  4. Pinouts
  5. WeMos IDE Nano BTS7960
  6. D0 16
  7. D1 5 A5 SCL (Serial Clock)Reseve D1
  8. D2 4 A4 SDA (Serial Data)Reserve D1
  9. D3 0 8 R_EN (3)
  10. D4 2 9 L_PWM (2)
  11. D5 14 7 L_EN (4)
  12. D6 12 6 R_PWM (1)
  13. D7 13
  14. D8 15
  15. TX 1
  16. RX 3
  17. Pin Order Reference
  18. BTS7960(uint8_t RPWM,uint8_t LPWM,uint8_t R_EN,uint8_t L_EN);
  19. */
  20.  
  21. BTS7960 m1(12,2,0,14); //Pins used for D1 Mini
  22. //BTS7960 m1(6,9,8,7); //Pins used for Nano
  23.  
  24. int speed = 0;
  25. float dutyCycle;
  26.  
  27. void setup()
  28. {
  29. Serial.begin(9600);
  30. Serial.println("Be sure to enable 'No line ending' in Monitor window");
  31. Serial.println("Enter Pump Speed Negative for Left, Positive for Right");
  32. Serial.println("between -1023 and 1023. Anything else to stop");
  33. }
  34.  
  35. void loop()
  36. {
  37. while (Serial.available() == 0)
  38. {
  39. }
  40. speed = Serial.parseInt();
  41. dutyCycle=abs(float(speed)/1023*100);
  42. if (speed <0){
  43. Serial.print("Setting Motor Speed to left (CW) with Speed = ");
  44. }
  45. else {
  46. Serial.print("Setting Motor Speed to right (CCW) with Speed = ");
  47. }
  48. Serial.print(speed);
  49. Serial.print(" Duty Cycle % = ");
  50. Serial.print(dutyCycle);
  51. Serial.println();
  52.  
  53. if (speed >= -1023 && speed <= 1023){
  54. m1.enable();
  55. m1.setSpeed(speed);
  56. }
  57. else
  58. {
  59. Serial.println("PUMP STOPPED");
  60. m1.stop();
  61. m1.disable();
  62. delay(2000);
  63. Serial.println();
  64. Serial.println("Enter Pump Speed Negative for Left, Positive for Right");
  65. Serial.println("between -1023 and 1023. Anything else to stop");
  66. }
  67. }
Add Comment
Please, Sign In to add comment