Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. // Stepper motor driver by Serial Terminal
  2. //
  3. // Send an INTEGER value through Serial Terminal
  4. // to the arduino to parse and execute a number of
  5. // steps.
  6. //
  7. // 2/19/2020
  8. // V1.2
  9.  
  10.  
  11. //We want:
  12. // - Directional control
  13. // - A "sine" movement
  14. // - "Absolute" step control/location
  15.  
  16. bool activation = false;
  17.  
  18. #define STEP_PIN 2 //by pulse
  19. #define DIR_PIN 3
  20. #define SLEEP_PIN 4
  21. #define ENABLE_PIN 5
  22.  
  23. #define S_MODE_M0_PIN 6
  24. #define S_MODE_M1_PIN 7
  25.  
  26. #define STEP_DELAY 50 //Used in step function. Change as needed
  27. static int step_delay = 50; //Used in step function. Change as needed
  28. static bool step_direction = true; //Used for direction of stepper
  29.  
  30.  
  31. typedef enum
  32. {
  33. no_case, step, dir, sine, speed
  34. } command_options;
  35.  
  36.  
  37.  
  38. void setup() {
  39. // put your setup code here, to run once:
  40.  
  41. Serial.begin(9600);
  42. Serial.println("Initialize the stepper");
  43.  
  44. //Enable
  45. // LOW = ENABLE
  46. // HIGH = DISABLE (Bruh)
  47. //disable the stepper driver during initialization.
  48. pinMode(ENABLE_PIN, OUTPUT);
  49. digitalWrite(ENABLE_PIN, LOW);
  50. Serial.println("Enable done");
  51. //step mode
  52. //00 Full-step
  53. //01 1/2 step
  54. //10 8 microstep
  55. //11 16 microstep
  56. pinMode(S_MODE_M0_PIN, OUTPUT);
  57. pinMode(S_MODE_M1_PIN, OUTPUT);
  58.  
  59. //Set to mode: FULL_STEP (0,0);
  60. digitalWrite(S_MODE_M0_PIN, LOW);
  61. digitalWrite(S_MODE_M1_PIN, LOW);
  62. //digitalWrite(S_MODE_M0_PIN, HIGH);
  63. //digitalWrite(S_MODE_M1_PIN, LOW);
  64.  
  65. Serial.println("Step mode done");
  66.  
  67.  
  68. //Direction (Step Mode M0/M1)
  69. // Clockwise = HIGH
  70. // CCWise = LOW
  71. //Set the stepper driver to clockwise rotation.
  72. pinMode(DIR_PIN, OUTPUT);
  73. digitalWrite(DIR_PIN, HIGH);
  74. Serial.println("Direction done");
  75.  
  76. //Sleep (NSLEEP)
  77. // LOW = SLEEP
  78. //Bring stepper driver out of "sleep" state if present
  79. pinMode(SLEEP_PIN, OUTPUT);
  80. digitalWrite(SLEEP_PIN, HIGH);
  81. Serial.println("Sleep done");
  82.  
  83. //Step pulse by pulse
  84. //NULL
  85. //Initialize step pin.
  86. pinMode(STEP_PIN, OUTPUT);
  87. digitalWrite(STEP_PIN, LOW);
  88. Serial.println("Step done");
  89.  
  90. //System is now initialized (hopefully)
  91.  
  92. Serial.flush();
  93. activation = true;
  94. }
  95.  
  96. void loop() {
  97. // put your main code here, to run repeatedly:
  98. static int stepNumber = 0;
  99. if (activation)
  100. {
  101. if (Serial.available() > 0)
  102. {
  103. String incomingData = Serial.readString();
  104.  
  105. int index_of_firstDelim = incomingData.indexOf(',');
  106. String commandParam = incomingData.substring(0, index_of_firstDelim);
  107. commandParam.trim();
  108. Serial.println(commandParam);
  109.  
  110.  
  111.  
  112.  
  113.  
  114. switch (castCommand(commandParam))
  115. {
  116. //{"step",13_int}
  117. case step:
  118. //commandParam = incomingData.substring(index_of_firstDelim + 1);
  119. //commandParam = command
  120. //commandParam.trim();
  121. //Serial.println(commandParam);
  122. stepNumber = (incomingData.substring(index_of_firstDelim + 1)).toInt();
  123. Serial.println(stepNumber);
  124. Serial.print("NUM of Steps requested: ");
  125. Serial.println(stepNumber);
  126. //should check if the incoming value is
  127. // ACTUALLY an integer of the correct type!
  128. stepSteps(stepNumber);
  129. break;
  130.  
  131. //{"dir","ccw"}
  132. case dir:
  133. typedef enum {ccw, cw} directions;
  134. commandParam = incomingData.substring(index_of_firstDelim + 1);
  135. Serial.println("Command Parameter: " + commandParam);
  136. commandParam.trim();
  137.  
  138. //Direction (Step Mode M0/M1)
  139. // Clockwise = HIGH
  140. // CCWise = LOW
  141. //bool direction_option = true; //true is CW
  142. if (commandParam.equalsIgnoreCase("ccw"))
  143. {
  144. digitalWrite(DIR_PIN, LOW);
  145. Serial.println("Direction: Counter Clock Wise");
  146. }
  147. else if (commandParam.equalsIgnoreCase("cw"))
  148. {
  149. digitalWrite(DIR_PIN, HIGH);
  150. Serial.println("Direction: Clock Wise");
  151. }
  152. else
  153. {
  154. Serial.println("ERROR: Unknown direction. Check spell/case/word?");
  155. }
  156.  
  157. break;
  158.  
  159. //{"sine","yes",startPosition_int}
  160. case sine:
  161.  
  162. break;
  163.  
  164. //{"speed", 32_int}s
  165. case speed:
  166. int thisSpeed = (incomingData.substring(index_of_firstDelim + 1)).toInt();
  167. Serial.print("New speed is: ");
  168. Serial.println(thisSpeed);
  169. step_delay = thisSpeed;
  170. break;
  171. }
  172.  
  173.  
  174. }
  175. }
  176.  
  177. }
  178.  
  179.  
  180. /*
  181. typedef enum
  182. {
  183. no_case, step, dir, sine, speed
  184. } command_options;
  185. */
  186.  
  187. command_options castCommand(String command)
  188. {
  189. command_options returnValue;
  190.  
  191. if(command.equalsIgnoreCase("step")) {returnValue = step;}
  192. else if(command.equalsIgnoreCase("dir")) {returnValue = dir;}
  193. else if(command.equalsIgnoreCase("sine")) {returnValue = sine;}
  194. else if(command.equalsIgnoreCase("speed")) {returnValue = speed;}
  195. else {returnValue = no_case;}
  196.  
  197. return returnValue;
  198. }
  199.  
  200. //Sends the number of steps at a #defined delay. Up to numSteps.
  201. void stepSteps(int numSteps)
  202. {
  203. //only called after initialization or undefined behavior.
  204. if (numSteps > 0)
  205. {
  206. for (int i = 1; i <= numSteps; i++)
  207. {
  208. digitalWrite(STEP_PIN, HIGH);
  209. //delay(STEP_DELAY);
  210. delay(step_delay);
  211. digitalWrite(STEP_PIN, LOW);
  212. //delay(STEP_DELAY);
  213. delay(step_delay);
  214.  
  215. Serial.print("Step #: ");
  216. Serial.println(i);
  217. }
  218. }
  219. else
  220. {
  221. }
  222.  
  223. return;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement