Advertisement
Guest User

Arduino sketch Dual Wind motors

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