Guest User

Untitled

a guest
Jan 5th, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1.  
  2.  
  3. #include <RFM69.h>
  4. #include <AccelStepper.h>
  5. #include <avr/sleep.h>
  6.  
  7. //encoder/motor/driver setup
  8. int easyDriverMicroSteps = 1;
  9. int rotaryEncoderSteps = 75;
  10. int motorStepsPerRev = 200;
  11. int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down
  12.  
  13. int easyDriverStepPin = 15;
  14. int easyDriverDirPin = 16;
  15. int enablePin = 17;
  16.  
  17. volatile long encoderValue = 0;
  18. long lastencoderValue = 0;
  19.  
  20. //Values for focus points
  21. int inPoint;
  22. int outPoint;
  23.  
  24. //value for variable speed
  25. int speedValue = 1000;
  26.  
  27. //Values for Lens Calibration
  28. int lowEndMark = -50000;
  29. int highEndMark = 50000;
  30.  
  31. boolean lowEndSwitch = false;
  32. boolean highEndSwitch = false;
  33.  
  34.  
  35. //ON LED
  36. #define onLed 12
  37.  
  38. int mode;
  39. #define Rewind 1
  40. #define Play 2
  41. // standBy mode is meant to enable changes in speed
  42. #define standBy 3
  43. #define realTime 4
  44. #define Stop 5
  45. #define LENSCALIB 6
  46.  
  47. AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);
  48.  
  49. //Sleep Function - to disable ED when not active
  50. long previousMillis = 0;
  51. int sleepTimer = 5000;
  52.  
  53. // You will need to initialize the radio by telling it what ID it has and what network it's on
  54. // The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
  55. // The Network ID takes values from 0-255
  56. // By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
  57. #define NODEID 1 //network ID used for this unit
  58. #define NETWORKID 99 //the network ID we are on
  59. #define GATEWAYID 2 //the node ID we're sending to
  60. #define SERIAL_BAUD 115200
  61.  
  62. //encryption is OPTIONAL
  63. //to enable encryption you will need to:
  64. // - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
  65. // - to call .Encrypt(KEY) to start encrypting
  66. // - to stop encrypting call .Encrypt(NULL)
  67. uint8_t KEY[] = "ABCDABCDABCDABCD";
  68.  
  69. //int for incoming radio data
  70. int a;
  71. int sum;
  72.  
  73. //variables for outgoing radio data
  74. char input = 0;
  75. int data = 0;
  76.  
  77. // Need an instance of the Radio Module
  78. RFM69 radio;
  79. byte sendSize=0;
  80. char payload[] = "1234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  81. bool requestACK=false;
  82.  
  83.  
  84. void setup()
  85. {
  86. radio.Initialize(NODEID, RF69_868MHZ, NETWORKID);
  87. radio.Encrypt(KEY); //comment this out to disable encryption
  88.  
  89. stepper.setMinPulseWidth(MinPulseWidth);
  90. stepper.setMaxSpeed(speedValue); //variable to later determine speed play/rewind
  91. stepper.setAcceleration(100000);
  92. stepper.setSpeed(50000);
  93.  
  94. pinMode(enablePin, OUTPUT);
  95. }
  96.  
  97. void loop()
  98. {
  99. stepper.run();
  100.  
  101. if (radio.ReceiveComplete())
  102. {
  103. lastencoderValue = encoderValue;
  104. digitalWrite (enablePin, LOW);
  105. previousMillis = millis();
  106.  
  107. sum = 0;
  108. for (byte i = 0; i < radio.GetDataLen(); i++)
  109. {
  110. a = (radio.Data[i]);
  111. }
  112. sum+=a;
  113. dataSort ();
  114. stepperMove();
  115.  
  116. }
  117. else
  118. {
  119. //Stepper sleep after 5sec of no data
  120. unsigned long currentMillis = millis ();
  121. if (currentMillis - previousMillis>sleepTimer)
  122. digitalWrite (enablePin, HIGH);
  123. }
  124.  
  125. switch (mode)
  126. {
  127.  
  128. case standBy:
  129. break;
  130.  
  131. case Stop:
  132. break;
  133.  
  134. case realTime:
  135.  
  136. dataSort();
  137. stepperMove();
  138.  
  139. break;
  140.  
  141. case Rewind:
  142. //take care of variable speed
  143. stepper.setMaxSpeed(speedValue);
  144. stepper.moveTo(inPoint);
  145. stepper.run();
  146. digitalWrite (enablePin, LOW);
  147. if(stepper.currentPosition()==inPoint)
  148. {
  149. Transmit (3);
  150. mode=standBy;
  151. }
  152.  
  153. break;
  154.  
  155. case LENSCALIB:
  156. highEndMark = 50000;
  157. lowEndMark = -50000;
  158. lowEndSwitch = false;
  159. highEndSwitch = false;
  160. mode = Stop;
  161. break;
  162.  
  163. case Play:
  164. //take care of variable speed
  165. stepper.setMaxSpeed(speedValue);
  166. stepper.moveTo(outPoint);
  167. stepper.run();
  168. digitalWrite (enablePin, LOW);
  169. if(stepper.currentPosition()==outPoint)
  170. {
  171. Transmit (3);
  172. mode=standBy;
  173. }
  174. break;
  175. }
  176. }
  177.  
  178. void dataSort()
  179. {
  180.  
  181. if (sum == 54)
  182. {
  183. inPoint = stepper.currentPosition();
  184. Transmit (1);
  185. }
  186. else if (sum == 55)
  187. {
  188. outPoint = stepper.currentPosition();
  189. Transmit (2);
  190. }
  191. else if (sum == 49)
  192. {
  193. encoderValue++;
  194. }
  195. else if (sum == 50)
  196. {
  197. encoderValue--;
  198. }
  199. else if (sum == 56)
  200. {
  201. lowEndMark = stepper.currentPosition();
  202. lowEndSwitch = true;
  203. Transmit (1);
  204. }
  205. else if (sum == 57)
  206. {
  207. highEndMark = stepper.currentPosition();
  208. highEndSwitch = true;
  209. Transmit (2);
  210. }
  211. else if (sum == 52)
  212. {
  213. mode = LENSCALIB;
  214. }
  215.  
  216. else if (sum == 51)
  217. {
  218. mode = Stop;
  219. }
  220. else if (sum == 53)
  221. {
  222. //function to make Play or Rewind
  223. defineDirection();
  224. }
  225. else if (sum == 58)
  226. {
  227. speedValue -=30;
  228. if (speedValue <100)
  229. speedValue = 100;
  230. }
  231. else if (sum = 59)
  232. {
  233. speedValue+=30;
  234. if (speedValue>5500)
  235. speedValue = 6000;
  236. }
  237. }
  238.  
  239. void defineDirection()
  240. {
  241. if (stepper.currentPosition()!=inPoint)
  242. {
  243. mode = Rewind;
  244. }
  245. else if (stepper.currentPosition() == inPoint)
  246. {
  247. mode = Play;
  248. }
  249. }
  250.  
  251. void stepperMove ()
  252. {
  253. int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
  254. int motorMove = (encoderValue * stepsPerRotaryStep);
  255. if (mode==standBy)return;
  256. //Lens Calibration
  257. if (motorMove >lowEndMark)
  258. motorMove = lowEndMark;
  259. if (motorMove<highEndMark)
  260. motorMove = highEndMark;
  261.  
  262. stepper.run();
  263. stepper.setMaxSpeed(1000);
  264. stepper.moveTo(motorMove);
  265. }
  266.  
  267. int Transmit (int data)
  268. {
  269. radio.Wakeup ();
  270. radio.Send(GATEWAYID, payload, sendSize+(data));
  271. radio.Sleep();
  272. }
Advertisement
Add Comment
Please, Sign In to add comment