Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include "DualMC33926MotorShield.h" //Pololu 3A motor shield
  2. // speed input vales 0-400
  3.  
  4. DualMC33926MotorShield md; // fans are 24V 4" SEAFLO 3A Max
  5.  
  6. #define MOTORTHRESHOLD 50 // where the fans start spinning
  7. #define PRINTCHANGES false
  8.  
  9. // pins used by mc33926
  10. // A0 - M1FB
  11. // A1 - M2FB
  12. // 4 - D2
  13. // 7 - m1DIR
  14. // 8 - m2DIR
  15. // 9 - m1PWM
  16. // 10 - m2PWM
  17. // 12 - SF
  18.  
  19. int serialpin = 2; // SPDT center terminal to ground (14)
  20. int manualpin = 3; // serial-manual switch
  21. int knobpin = A2; // pot knob pin for manual speed or max serial speed (also GND, +5V)
  22.  
  23. int knobpos;
  24. int switchpos;
  25. int prev_switch = 0;
  26. int prev_knob = 0;
  27.  
  28. int LSpeed;
  29. int RSpeed;
  30.  
  31. byte bytesBuffer[6] = {0};
  32.  
  33.  
  34. // SETUP ***********************************************
  35. void setup()
  36. {
  37. Serial.begin(115200);
  38. // Serial.println("Dual MC33926 Motor Shield");
  39. md.init();
  40.  
  41. pinMode(serialpin, INPUT_PULLUP);
  42. pinMode(manualpin, INPUT_PULLUP);
  43. }
  44.  
  45. // MAIN LOOP ***********************************************
  46. void loop() {
  47.  
  48. switch (getSwitch()) {
  49. case 1 : {
  50. LSpeed = ReadKnob();
  51. RSpeed = LSpeed;
  52. blow(LSpeed, RSpeed);
  53. break;
  54. }
  55. case -1 : {
  56. ReadData();
  57. blow(LSpeed, RSpeed);
  58. break;
  59. }
  60. case 0 : {
  61. blow(0, 0);
  62. break;
  63. }
  64. }
  65. // delay (500);
  66. }
  67.  
  68. // CHECK SWITCH ***********************************************
  69. int getSwitch() {
  70. // 0 is off
  71. // 1 is manual knob
  72. // -1 is serial
  73. int value = 0;
  74. // get switch position
  75. int manualpos = digitalRead( manualpin );
  76. int serialpos = digitalRead( serialpin );
  77. delay (1);
  78. if (manualpos == LOW && serialpos == HIGH ) value = 1;
  79. if (manualpos == HIGH && serialpos == LOW ) value = -1;
  80.  
  81. if (value != prev_switch && PRINTCHANGES) {
  82. Serial.println(value);
  83. }
  84. prev_switch = value;
  85. return value;
  86. }
  87.  
  88. // BLOW ***********************************************
  89. void blow( int LSpeedIn, int RSpeedIn) {
  90. // dont spin until threshold
  91. if (LSpeedIn <= (MOTORTHRESHOLD)) {
  92. LSpeed = 0;
  93. }
  94. else {
  95. LSpeed = LSpeedIn;
  96. }
  97. if (RSpeedIn <= (MOTORTHRESHOLD)) {
  98. RSpeed = 0;
  99. }
  100. else {
  101. RSpeed = RSpeedIn;
  102. }
  103. md.setM1Speed(LSpeed);
  104. // stopIfFault();
  105.  
  106. md.setM2Speed(RSpeed);
  107. // stopIfFault();
  108. printcurrent();
  109. }
  110.  
  111.  
  112. // READ KNOB ********************************************
  113. int ReadKnob() {
  114. int rawvalue = analogRead( knobpin );
  115. knobpos = MOTORTHRESHOLD + map(rawvalue, 0, 1023, 0, 400-MOTORTHRESHOLD);
  116. delay (1);
  117. if (knobpos != prev_knob && PRINTCHANGES) {
  118. Serial.print("Knob ");
  119. Serial.println(knobpos);
  120. }
  121. prev_knob = knobpos;
  122. return knobpos;
  123. }
  124.  
  125. // READ SERIAL ********************************************
  126. void ReadData() {
  127.  
  128. int maxspeed = ReadKnob();
  129. if (Serial.available())
  130. {
  131. // Read new byte and put it at the end of the buffer
  132. bytesBuffer[0] = bytesBuffer[1];
  133. bytesBuffer[1] = bytesBuffer[2];
  134. bytesBuffer[2] = bytesBuffer[3];
  135. bytesBuffer[3] = bytesBuffer[4];
  136. bytesBuffer[4] = bytesBuffer[5];
  137. bytesBuffer[5] = Serial.read();
  138. // counter--;
  139. Serial.println(bytesBuffer[3]);
  140. // Verify if we have a valid sequence "WIND"
  141. if (bytesBuffer[0] == 87 && bytesBuffer[1] == 73 && bytesBuffer[2] == 78 && bytesBuffer[3] == 68 )
  142. {
  143. Serial.print("valid: L:");
  144. Serial.print(bytesBuffer[4]);
  145. Serial.print(" R: ");
  146. Serial.println(bytesBuffer[5]);
  147.  
  148. // Set the pins values
  149. LSpeed = MOTORTHRESHOLD + map(bytesBuffer[4], 0, 255, 0, 400-MOTORTHRESHOLD);
  150. RSpeed = MOTORTHRESHOLD + map(bytesBuffer[5], 0, 255, 0, 400-MOTORTHRESHOLD);
  151.  
  152. // knob sets maximum
  153.  
  154. LSpeed = map(LSpeed, MOTORTHRESHOLD, 400, MOTORTHRESHOLD, maxspeed);
  155. RSpeed = map(RSpeed, MOTORTHRESHOLD, 400, MOTORTHRESHOLD, maxspeed);
  156. }
  157. // get disconnect "STOP"
  158. if (bytesBuffer[0] == 83 && bytesBuffer[1] == 84 && bytesBuffer[2] == 79 && bytesBuffer[3] == 80 )
  159. {
  160. Serial.end();
  161. setup();
  162. }
  163.  
  164. }
  165. }
  166.  
  167.  
  168. // FAULT ***********************************************
  169.  
  170. void stopIfFault()
  171. {
  172. if (md.getFault())
  173. {
  174. md.setM1Speed(0);
  175. md.setM2Speed(0);
  176. // Serial.println("fault");
  177. while (1);
  178. }
  179. }
  180.  
  181.  
  182. // PRINT DEBUGGING***********************************************
  183.  
  184. void printcurrent()
  185. {
  186. switch (getSwitch()) {
  187. case 0 : {
  188. Serial.println("OFF: ");
  189. break;
  190. }
  191.  
  192. case 1 : {
  193. Serial.print("Manual: ");
  194. printdata();
  195. break;
  196. }
  197. case -1 : {
  198. Serial.print("Serial: ");
  199. printdata();
  200. break;
  201. }
  202. }
  203. }
  204.  
  205. void printdata() {
  206. Serial.print(knobpos);
  207. Serial.print(" M1: ");
  208. Serial.print(LSpeed);
  209. Serial.print(" mA:");
  210. Serial.print(md.getM1CurrentMilliamps());
  211. Serial.print(" M2: ");
  212. Serial.print(RSpeed);
  213. Serial.print(" mA:");
  214. Serial.println(md.getM2CurrentMilliamps());
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement