Advertisement
Guest User

Untitled

a guest
Mar 4th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. class Stepper {
  2. public:
  3. // constructors:
  4. Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
  5. Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);
  6.  
  7. // speed setter method:
  8. void setSpeed(long whatSpeed);
  9.  
  10. // mover method:
  11. void step(int number_of_steps);
  12.  
  13. int version(void);
  14.  
  15. private:
  16. void stepMotor(int this_step);
  17.  
  18. int direction; // Direction of rotation
  19. int speed; // Speed in RPMs
  20. unsigned long step_delay; // delay between steps, in ms, based on speed
  21. int number_of_steps; // total number of steps this motor can take
  22. int pin_count; // whether you're driving the motor with 2 or 4 pins
  23. int step_number; // which step the motor is on
  24.  
  25. // motor pin numbers:
  26. int motor_pin_1;
  27. int motor_pin_2;
  28. int motor_pin_3;
  29. int motor_pin_4;
  30.  
  31. long last_step_time; // time stamp in ms of when the last step was taken
  32. };
  33.  
  34. //#endif
  35.  
  36.  
  37.  
  38. /*
  39. Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
  40.  
  41. Original library (0.1) by Tom Igoe.
  42. Two-wire modifications (0.2) by Sebastian Gassner
  43. Combination version (0.3) by Tom Igoe and David Mellis
  44. Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
  45.  
  46. Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
  47.  
  48. When wiring multiple stepper motors to a microcontroller,
  49. you quickly run out of output pins, with each motor requiring 4 connections.
  50.  
  51. By making use of the fact that at any time two of the four motor
  52. coils are the inverse of the other two, the number of
  53. control connections can be reduced from 4 to 2.
  54.  
  55. A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
  56. connects to only 2 microcontroler pins, inverts the signals received,
  57. and delivers the 4 (2 plus 2 inverted ones) output signals required
  58. for driving a stepper motor.
  59.  
  60. The sequence of control signals for 4 control wires is as follows:
  61.  
  62. Step C0 C1 C2 C3
  63. 1 1 0 1 0
  64. 2 0 1 1 0
  65. 3 0 1 0 1
  66. 4 1 0 0 1
  67.  
  68. The sequence of controls signals for 2 control wires is as follows
  69. (columns C1 and C2 from above):
  70.  
  71. Step C0 C1
  72. 1 0 1
  73. 2 1 1
  74. 3 1 0
  75. 4 0 0
  76.  
  77. The circuits can be found at
  78.  
  79. http://www.arduino.cc/en/Tutorial/Stepper
  80.  
  81.  
  82. */
  83.  
  84.  
  85. //#include "Arduino.h"
  86. // https://community.sparkdevices.com/t/fix-for-include-arduino-h/953
  87. #define ARDUINO_H
  88. #include <stdint.h>
  89. #include <stddef.h>
  90. #include <stdlib.h>
  91.  
  92. //#include "Stepper.h"
  93.  
  94. /*
  95. * two-wire constructor.
  96. * Sets which wires should control the motor.
  97. */
  98. Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
  99. {
  100. this->step_number = 0; // which step the motor is on
  101. this->speed = 0; // the motor speed, in revolutions per minute
  102. this->direction = 0; // motor direction
  103. this->last_step_time = 0; // time stamp in ms of the last step taken
  104. this->number_of_steps = number_of_steps; // total number of steps for this motor
  105.  
  106. // Arduino pins for the motor control connection:
  107. this->motor_pin_1 = motor_pin_1;
  108. this->motor_pin_2 = motor_pin_2;
  109.  
  110. // setup the pins on the microcontroller:
  111. pinMode(this->motor_pin_1, OUTPUT);
  112. pinMode(this->motor_pin_2, OUTPUT);
  113.  
  114. // When there are only 2 pins, set the other two to 0:
  115. this->motor_pin_3 = 0;
  116. this->motor_pin_4 = 0;
  117.  
  118. // pin_count is used by the stepMotor() method:
  119. this->pin_count = 2;
  120. }
  121.  
  122.  
  123. /*
  124. * constructor for four-pin version
  125. * Sets which wires should control the motor.
  126. */
  127.  
  128. Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
  129. {
  130. this->step_number = 0; // which step the motor is on
  131. this->speed = 0; // the motor speed, in revolutions per minute
  132. this->direction = 0; // motor direction
  133. this->last_step_time = 0; // time stamp in ms of the last step taken
  134. this->number_of_steps = number_of_steps; // total number of steps for this motor
  135.  
  136. // Arduino pins for the motor control connection:
  137. this->motor_pin_1 = motor_pin_1;
  138. this->motor_pin_2 = motor_pin_2;
  139. this->motor_pin_3 = motor_pin_3;
  140. this->motor_pin_4 = motor_pin_4;
  141.  
  142. // setup the pins on the microcontroller:
  143. pinMode(this->motor_pin_1, OUTPUT);
  144. pinMode(this->motor_pin_2, OUTPUT);
  145. pinMode(this->motor_pin_3, OUTPUT);
  146. pinMode(this->motor_pin_4, OUTPUT);
  147.  
  148. // pin_count is used by the stepMotor() method:
  149. this->pin_count = 4;
  150. }
  151.  
  152. /*
  153. Sets the speed in revs per minute
  154.  
  155. */
  156. void Stepper::setSpeed(long whatSpeed)
  157. {
  158. this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
  159. }
  160.  
  161. /*
  162. Moves the motor steps_to_move steps. If the number is negative,
  163. the motor moves in the reverse direction.
  164. */
  165. void Stepper::step(int steps_to_move)
  166. {
  167. int steps_left = abs(steps_to_move); // how many steps to take
  168.  
  169. // determine direction based on whether steps_to_mode is + or -:
  170. if (steps_to_move > 0) {this->direction = 1;}
  171. if (steps_to_move < 0) {this->direction = 0;}
  172.  
  173.  
  174. // decrement the number of steps, moving one step each time:
  175. while(steps_left > 0) {
  176. // move only if the appropriate delay has passed:
  177. if (millis() - this->last_step_time >= this->step_delay) {
  178. // get the timeStamp of when you stepped:
  179. this->last_step_time = millis();
  180. // increment or decrement the step number,
  181. // depending on direction:
  182. if (this->direction == 1) {
  183. this->step_number++;
  184. if (this->step_number == this->number_of_steps) {
  185. this->step_number = 0;
  186. }
  187. }
  188. else {
  189. if (this->step_number == 0) {
  190. this->step_number = this->number_of_steps;
  191. }
  192. this->step_number--;
  193. }
  194. // decrement the steps left:
  195. steps_left--;
  196. // step the motor to step number 0, 1, 2, or 3:
  197. stepMotor(this->step_number % 4);
  198. }
  199. }
  200. }
  201.  
  202. /*
  203. * Moves the motor forward or backwards.
  204. */
  205. void Stepper::stepMotor(int thisStep)
  206. {
  207. if (this->pin_count == 2) {
  208. switch (thisStep) {
  209. case 0: /* 01 */
  210. digitalWrite(motor_pin_1, LOW);
  211. digitalWrite(motor_pin_2, HIGH);
  212. break;
  213. case 1: /* 11 */
  214. digitalWrite(motor_pin_1, HIGH);
  215. digitalWrite(motor_pin_2, HIGH);
  216. break;
  217. case 2: /* 10 */
  218. digitalWrite(motor_pin_1, HIGH);
  219. digitalWrite(motor_pin_2, LOW);
  220. break;
  221. case 3: /* 00 */
  222. digitalWrite(motor_pin_1, LOW);
  223. digitalWrite(motor_pin_2, LOW);
  224. break;
  225. }
  226. }
  227. if (this->pin_count == 4) {
  228. switch (thisStep) {
  229. case 0: // 1010
  230. // digitalWrite(motor_pin_1, HIGH);
  231. // digitalWrite(motor_pin_2, LOW);
  232. // digitalWrite(motor_pin_3, HIGH);
  233. // digitalWrite(motor_pin_4, LOW);
  234. // digitalWrite(motor_pin_1, HIGH);
  235. // digitalWrite(motor_pin_2, LOW);
  236. // digitalWrite(motor_pin_3, LOW);
  237. // digitalWrite(motor_pin_4, HIGH);
  238. digitalWrite(motor_pin_1, HIGH);
  239. digitalWrite(motor_pin_2, LOW);
  240. digitalWrite(motor_pin_3, LOW);
  241. digitalWrite(motor_pin_4, LOW);
  242. break;
  243. case 1: // 0110
  244. // digitalWrite(motor_pin_1, LOW);
  245. // digitalWrite(motor_pin_2, HIGH);
  246. // digitalWrite(motor_pin_3, HIGH);
  247. // digitalWrite(motor_pin_4, LOW);
  248. // digitalWrite(motor_pin_1, LOW);
  249. // digitalWrite(motor_pin_2, HIGH);
  250. // digitalWrite(motor_pin_3, LOW);
  251. // digitalWrite(motor_pin_4, HIGH);
  252. digitalWrite(motor_pin_1, LOW);
  253. digitalWrite(motor_pin_2, HIGH);
  254. digitalWrite(motor_pin_3, LOW);
  255. digitalWrite(motor_pin_4, LOW);
  256. break;
  257. case 2: //0101
  258. // digitalWrite(motor_pin_1, LOW);
  259. // digitalWrite(motor_pin_2, HIGH);
  260. // digitalWrite(motor_pin_3, LOW);
  261. // digitalWrite(motor_pin_4, HIGH);
  262. // digitalWrite(motor_pin_1, LOW);
  263. // digitalWrite(motor_pin_2, HIGH);
  264. // digitalWrite(motor_pin_3, HIGH);
  265. // digitalWrite(motor_pin_4, LOW);
  266.  
  267. digitalWrite(motor_pin_1, LOW);
  268. digitalWrite(motor_pin_2, LOW);
  269. digitalWrite(motor_pin_3, HIGH);
  270. digitalWrite(motor_pin_4, LOW);
  271. break;
  272. case 3: //1001
  273. // digitalWrite(motor_pin_1, HIGH);
  274. // digitalWrite(motor_pin_2, LOW);
  275. // digitalWrite(motor_pin_3, LOW);
  276. // digitalWrite(motor_pin_4, HIGH);
  277. // digitalWrite(motor_pin_1, HIGH);
  278. // digitalWrite(motor_pin_2, LOW);
  279. // digitalWrite(motor_pin_3, HIGH);
  280. // digitalWrite(motor_pin_4, LOW);
  281.  
  282. digitalWrite(motor_pin_1, LOW);
  283. digitalWrite(motor_pin_2, LOW);
  284. digitalWrite(motor_pin_3, LOW);
  285. digitalWrite(motor_pin_4, HIGH);
  286. break;
  287. }
  288. }
  289. }
  290.  
  291. /*
  292. version() returns the version of the library:
  293. */
  294. int Stepper::version(void)
  295. {
  296. return 4;
  297. }
  298.  
  299. /*
  300. * MotorKnob
  301. *
  302. * A stepper motor follows the turns of a potentiometer
  303. * (or other sensor) on analog input 0.
  304. *
  305. * http://www.arduino.cc/en/Reference/Stepper
  306. * This example code is in the public domain.
  307. */
  308.  
  309. //#include <Stepper.h>
  310.  
  311. // change this to the number of steps on your motor
  312. #define STEPS 100
  313.  
  314. // create an instance of the stepper class, specifying
  315. // the number of steps of the motor and the pins it's
  316. // attached to
  317. Stepper stepper(STEPS, D2, D3, D4, D5);
  318.  
  319. // the previous reading from the analog input
  320. int previous = 0;
  321.  
  322. void setup()
  323. {
  324. // set the speed of the motor to 30 RPMs
  325. stepper.setSpeed(100);
  326. Spark.function("step", step2);
  327. Serial.begin(9600);
  328. }
  329.  
  330. void loop()
  331. {
  332. // get the sensor value
  333. // int val = analogRead(A0);
  334.  
  335. // move a number of steps equal to the change in the
  336. // sensor reading
  337. // stepper.step(val - previous);
  338.  
  339. // remember the previous value of the sensor
  340. // previous = val;
  341. }
  342.  
  343. int step2(String command) {
  344. command.trim();
  345. command.toUpperCase();
  346.  
  347. Serial.println("Data received");
  348. Serial.print('\n');
  349. Serial.println(command);
  350.  
  351. if(command.equals("LOCK")) {
  352.  
  353. stepper.step(-375);
  354. Serial.println("Locking...");
  355. Spark.sleep(SLEEP_MODE_DEEP,20);
  356.  
  357. } else if(command.equals("UNLOCK")) {
  358. Spark.sleep(20);
  359. stepper.step(375);
  360. Serial.println("Unlocking...");
  361. }
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement