Advertisement
Push28

stepper motor with a4988 driver and serial controlled

Jan 18th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | None | 0 0
  1. /* setup to control a stepper motor with a A4988 polulu driver and Serial controlled
  2.  Made by Luc Francoeur (push28@gmail.com). As usual feel free to contact me if you
  3.  have any question
  4.  
  5. direction       step precision      controller          reset       step count 
  6. F = forward     F = full            e= enabled          R=reset     data[4,5,6,7]
  7. R = reverse     H= half             d= disabled         N=null
  8.                 Q= quarter          S= sleep
  9.                 E= eight
  10.                 S= sixteen
  11.  
  12.             1.8 degree per step at full / 200steps
  13.             0,9 degree per step at half / 400steps
  14.             0,45 degree per step at quarter / 800steps
  15.             0,225 degree per step at eight / 1600steps
  16.             0,1025degree per step at sixteenth / 3200steps 
  17. */
  18.  
  19. #include <Wire.h>;
  20. char ds[8];
  21. char buffer[25];
  22. const int stepPin = 2;
  23. const int dirPin = 3;
  24. const int ms1 = 11;
  25. const int ms2 = 12;
  26. const int ms3 = 13;
  27. const int enable = A1;
  28. const int reset = A2;
  29. const int sleep = A3;
  30. int stepcount = 0;
  31. int var = 0;
  32.  
  33. void setup() {
  34.   pinMode(stepPin, OUTPUT);
  35.   pinMode(dirPin, OUTPUT);
  36.   pinMode(ms1, OUTPUT);
  37.   pinMode(ms2, OUTPUT);
  38.   pinMode(ms3, OUTPUT);
  39.   pinMode(enable, OUTPUT);
  40.   pinMode(reset, OUTPUT);
  41.   digitalWrite(reset,LOW);
  42.   digitalWrite(enable,HIGH);
  43.   digitalWrite(sleep,LOW);
  44.  
  45.   Serial.begin(19200); // Speed of serial port
  46.   Serial.flush();
  47.   Wire.begin(0x02); // join i2c bus (address optional for master)
  48.   Wire.onRequest(requestEvent); // register event
  49.  
  50. }
  51.  
  52.  
  53. void loop() {
  54.  
  55.   while (var < stepcount) {
  56.     digitalWrite(stepPin, HIGH);
  57.     delay(3);
  58.     digitalWrite(stepPin, LOW);
  59.     var++;
  60.   }
  61.  
  62.   if (Serial.available() > 0) {
  63.     byte index = 0;
  64.     delay(100); // let the buffer fill up
  65.     byte numChar = Serial.available();
  66.     if (numChar > 20) {
  67.       numChar = 20;
  68.     }
  69.     while (numChar--) {
  70.       buffer[index++] = Serial.read();
  71.     }
  72.     splitString(buffer);
  73.   }
  74.  
  75. }
  76.  
  77. void splitString(char* data) {
  78.  
  79.   char * parameter;
  80.   parameter = strtok (data, " ,");
  81.   while (parameter != NULL) {
  82.     setStepper(parameter);
  83.     calcStepper(parameter);
  84.     parameter = strtok (NULL, " ,");
  85.   }// Clear the text and serial buffers
  86.   for (byte x = 0; x < 20; x++) {
  87.     buffer[x] = '\0';
  88.   }
  89.   Serial.flush();
  90. }
  91.  
  92.  
  93. void setStepper(char *data) {
  94.  
  95.   ds[0] = data[0];
  96.   ds[1] = data[1];
  97.   ds[2] = data[2];
  98.   ds[3] = data[3];
  99.   ds[4] = data[4];
  100.   ds[5] = data[5];
  101.   ds[6] = data[6];
  102.   ds[7] = data[7];
  103.  
  104.   if ((data[0] == 'F') || (data[0] == 'f')) { //clockwise rotation
  105.     digitalWrite(dirPin, LOW);
  106.     Serial.flush();
  107.     Serial.print("clockwise rotation, ");
  108.   }
  109.  
  110.   if ((data[0] == 'R') || (data[0] == 'r')) { //counter-clockwise rotation
  111.     digitalWrite(dirPin, HIGH);
  112.     Serial.print("counter rotation, ");
  113.   }
  114.  
  115.  
  116.   if ((data[1] == 'F') || (data[1] == 'f')) { //step precision Full
  117.     digitalWrite(ms1, LOW);
  118.     digitalWrite(ms2, LOW);
  119.     digitalWrite(ms3, LOW);
  120.     Serial.print("full-step, ");
  121.   }
  122.  
  123.   if ((data[1] == 'H') || (data[1] == 'h')) { //step precision Half
  124.     digitalWrite(ms1, HIGH);
  125.     digitalWrite(ms2, LOW);
  126.     digitalWrite(ms3, LOW);
  127.     Serial.print("half-step, ");
  128.   }
  129.  
  130.   if ((data[1] == 'Q') || (data[1] == 'q')) { //step precision Quarter
  131.     digitalWrite(ms1, LOW);
  132.     digitalWrite(ms2, HIGH);
  133.     digitalWrite(ms3, LOW);
  134.     Serial.print("quarter-step, ");
  135.   }
  136.  
  137.   if ((data[1] == 'E') || (data[1] == 'e')) { //step precision Eight
  138.     digitalWrite(ms1, HIGH);
  139.     digitalWrite(ms2, HIGH);
  140.     digitalWrite(ms3, LOW);
  141.     Serial.print("eight-step, ");
  142.   }
  143.  
  144.   if ((data[1] == 'S') || (data[1] == 's')) { //step precision Sixteenth
  145.     digitalWrite(ms1, HIGH);
  146.     digitalWrite(ms2, HIGH);
  147.     digitalWrite(ms3, HIGH);
  148.     Serial.print("sixteenth-step, ");
  149.   }
  150.  
  151.  
  152.   if ((data[2] == 'E') || (data[2] == 'e')) { //controller Enabled
  153.     digitalWrite(enable, LOW);
  154.     digitalWrite(sleep,HIGH);
  155.     Serial.print("controller Enabled, ");
  156.   }
  157.  
  158.   if ((data[2] == 'D') || (data[2] == 'd')) { //controller Disabled
  159.     digitalWrite(enable, HIGH);
  160.     digitalWrite(sleep,HIGH);
  161.     Serial.print("controller Disabled, ");
  162.   }
  163.  
  164.   if ((data[2] == 'S') || (data[2] == 's')) { //controller in Sleep Mode
  165.     digitalWrite(enable, LOW);
  166.     digitalWrite(sleep,LOW);
  167.     Serial.print("controller Sleeping, ");
  168.   }
  169.  
  170.  
  171.   if ((data[3] == 'R') || (data[3] == 'r')) { //Reset Enabled
  172.     digitalWrite(reset, LOW);
  173.     delay(50);
  174.     digitalWrite(reset,HIGH);
  175.     Serial.println("resetting, ");
  176.   }
  177.  
  178.   if ((data[3] == 'N') || (data[3] == 'n')) { //Reset Disabled
  179.     digitalWrite(reset, HIGH);
  180.     Serial.println("reset Disabled, ");
  181.   }
  182. }
  183.  
  184.  
  185. void requestEvent() {
  186.   Wire.write(ds); // respond with message of 6 bytes
  187.   // as expected by master
  188. }
  189.  
  190.  
  191. void calcStepper(char *ds) {
  192.   Serial.flush();
  193.   int Ans = strtol(ds + 4, NULL, 10);
  194.   Ans = constrain(Ans, 0, 6400);
  195.   stepcount = Ans;
  196.   Serial.print("initiating ");
  197.   Serial.print(Ans);
  198.   Serial.println(" steps rotation");
  199.   Serial.print("DataStream received by Controller: ");
  200.   Serial.println(ds);
  201.   var = 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement