Guest User

Untitled

a guest
Jan 7th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <RFM69.h>
  3. #include <avr/sleep.h>
  4. #include <OneButton.h>
  5.  
  6.  
  7. // You will need to initialize the radio by telling it what ID it has and what network it's on
  8. // The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
  9. // The Network ID takes values from 0-255
  10. // 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}
  11. #define NODEID 2 //network ID used for this unit
  12. #define NETWORKID 99 //the network ID we are on
  13. #define GATEWAYID 1 //the node ID we're sending to
  14. //#define ACK_TIME 50 // # of ms to wait for an ack
  15.  
  16.  
  17.  
  18. //int interPacketDelay = 1000; //wait this many ms between sending packets
  19. char input = 0;
  20. int data = 0;
  21.  
  22. // Need an instance of the Radio Module
  23. RFM69 radio;
  24. byte sendSize=0;
  25. char payload[] = "1234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  26. bool requestACK=false;
  27.  
  28. // Setup OneButton
  29. OneButton playButton (A1, true);
  30. OneButton outButton (A2, true);
  31. OneButton inButton (A3, true);
  32. OneButton realTimebutton(A4, true);
  33.  
  34. //LEDs
  35. #define playLED 5 //PLay LED
  36. #define outLED 6 //Out LED
  37. #define inLED 7 //In LED
  38. #define realTimeLED 8 //Real Time LED
  39.  
  40. //Values for focus points
  41. volatile int inPoint = 0;
  42. volatile int outPoint = 3000;
  43.  
  44. //Blink without delay
  45. boolean ledState = LOW;
  46. long previousMillis = 0;
  47. int ledInterval = 75;
  48.  
  49. //Defining booleans
  50. boolean rClickedOnce = false;
  51. boolean rLongPress = false;
  52. boolean pClickedOnce = false;
  53. boolean highEndMark = false;
  54. boolean lowEndMark = false;
  55.  
  56. //Value to recieve from 2nd Xbee: when "play" or "rewind" has finished"
  57. int sum;
  58. int a;
  59.  
  60. //Modes
  61. int mode;
  62. #define Stop 1
  63. #define LENSCALIB 2
  64.  
  65. int encoder0PinA = 4;
  66. int encoder0PinB = 3;
  67. int encoderValue = 0;
  68. int encoder0PinALast = LOW;
  69. int n = LOW;
  70.  
  71. void setup() {
  72. pinMode (encoder0PinA,INPUT);
  73. pinMode (encoder0PinB,INPUT);
  74.  
  75. pinMode (realTimeLED, OUTPUT);
  76. pinMode (playLED, OUTPUT);
  77. pinMode (inLED, OUTPUT);
  78. pinMode (outLED, OUTPUT);
  79.  
  80. radio.initialize(NODEID, RF69_868MHZ, NETWORKID);
  81.  
  82. radio.sleep(); //sleep right away to save power
  83.  
  84. //Attach Click to Buttons
  85. realTimebutton.attachClick(Click);
  86. playButton.attachClick(ClickPlay);
  87. inButton.attachClick(ClickIn);
  88. outButton.attachClick(ClickOut);
  89.  
  90. //Attach Press to Real Time for calibrating lens and motor
  91. realTimebutton.attachPress(rPress);
  92.  
  93. //Start Up Light
  94. for (int x=0;x<3;x++)
  95. {
  96. for (int l=5;l<9;l++)
  97. {
  98. digitalWrite(l,HIGH);
  99. delay (100);
  100. digitalWrite(l,LOW);
  101. delay (100);
  102. }
  103. }
  104. }
  105.  
  106. void loop() {
  107.  
  108. n = digitalRead(encoder0PinA);
  109. if ((encoder0PinALast == LOW) && (n == HIGH)) {
  110. if (digitalRead(encoder0PinB) == LOW) {
  111. encoderValue--;
  112. Transmit(1);
  113. }
  114. else {
  115. encoderValue++;
  116. Transmit(2);
  117. }
  118. }
  119. encoder0PinALast = n;
  120.  
  121. // keep watching the push buttons:
  122. realTimebutton.tick();
  123. playButton.tick();
  124. inButton.tick();
  125. outButton.tick();
  126.  
  127. // filter signal from stepper motor
  128. //arduino when(play/rewind/In/Out) is done.
  129. if (radio.receiveDone())
  130. {
  131.  
  132. {
  133. sum = 0;
  134. for (byte i = 0; i < radio.DATALEN; i++)
  135. {
  136. a = (radio.DATA[i]);
  137. }
  138. sum+=a;
  139. dataSort ();
  140. }
  141. }
  142.  
  143. switch (mode)
  144. {
  145.  
  146. case LENSCALIB:
  147. {
  148. blinkFunction (realTimeLED);
  149.  
  150. if (lowEndMark == true && highEndMark == true ||rClickedOnce == true || pClickedOnce == true)
  151. {
  152. rLongPress = false;
  153. digitalWrite (realTimeLED, LOW);
  154. if (rClickedOnce == true)
  155. digitalWrite(realTimeLED, HIGH);
  156. if (pClickedOnce == true)
  157. digitalWrite (playLED, HIGH);
  158. mode = Stop;
  159. break;
  160. }
  161. }
  162. }
  163. }
  164.  
  165. //4 Buttons Click Functions
  166.  
  167. //Real Time Switch
  168. void Click() {
  169. digitalWrite(playLED, LOW);
  170. rLongPress = false;
  171. if (rClickedOnce == false )
  172. {
  173. rClickedOnce = true;
  174. digitalWrite(realTimeLED, HIGH);
  175. Transmit(3);
  176. }
  177. else
  178. {
  179. digitalWrite(realTimeLED, LOW);
  180. rClickedOnce = false;
  181. }
  182. }
  183. // Press Function - calibrating lens and stepper
  184.  
  185. void rPress() {
  186. if (rLongPress == false) {
  187. rLongPress = true;
  188. rClickedOnce = false;
  189. highEndMark = false;
  190. lowEndMark = false;
  191. Transmit(4);
  192. mode = LENSCALIB;
  193. }
  194. else
  195. {
  196. rLongPress = false;
  197. digitalWrite(realTimeLED, LOW);
  198. mode = Stop;
  199. }
  200. }
  201.  
  202. //Play Switch
  203. void ClickPlay () {
  204. //terminate realtime in case it wasn't stopped
  205. if (pClickedOnce == false)
  206. {
  207. pClickedOnce = true;
  208. rClickedOnce = false;
  209. digitalWrite(realTimeLED, LOW);
  210. digitalWrite(playLED, HIGH);
  211.  
  212. radio.send(GATEWAYID, payload, sendSize+(5));
  213. radio.sleep();
  214. }
  215. else
  216. {
  217. pClickedOnce = false;
  218. digitalWrite(playLED, LOW);
  219. }
  220. }
  221.  
  222. //In Switch
  223. void ClickIn () {
  224. // Saving In
  225. if (rClickedOnce == true)
  226. {
  227. inPoint = encoderValue;
  228. Transmit(6);
  229. }
  230. else if (rLongPress == true)
  231. {
  232. lowEndMark = true;
  233. Transmit(8);
  234. }
  235. }
  236.  
  237. //Out Switch
  238. void ClickOut() {
  239. //Saving Out
  240. if (rClickedOnce == true)
  241. {
  242. Transmit(7);
  243. outPoint = encoderValue;
  244. }
  245. else if (rLongPress == true)
  246. {
  247. highEndMark = true;
  248. Transmit(9);
  249. }
  250. }
  251. int blinkFunction (int y) // Blink without delay
  252. {
  253. unsigned long currentMillis = millis ();
  254. if (currentMillis - previousMillis>ledInterval)
  255. {
  256. previousMillis = currentMillis;
  257. if (ledState == LOW)
  258. ledState = HIGH;
  259. else
  260. ledState = LOW;
  261. digitalWrite (y, ledState);
  262. }
  263. }
  264.  
  265. int blinkMark (int y)
  266. {
  267. boolean b = HIGH;
  268. for (int i=0;i<6;i++)
  269. {
  270. digitalWrite(y, b);
  271. delay(75);
  272. b=!b;
  273. }
  274. }
  275.  
  276. int Transmit (int data)
  277. {
  278. //1.Send encoder value to move stepper
  279. if (rClickedOnce == true||rLongPress == true)
  280. {
  281.  
  282. radio.send(GATEWAYID, payload, sendSize+(data));
  283. radio.sleep();
  284. }
  285.  
  286. else if (pClickedOnce == false)
  287. {
  288. //2. Send speed value to move stepper
  289.  
  290. radio.send(GATEWAYID, payload, sendSize+(data+10));
  291. radio.sleep();
  292. }
  293. }
  294.  
  295. void dataSort ()
  296. {
  297. if (sum == 49)
  298. blinkMark(inLED);
  299.  
  300. else if (sum == 50)
  301. blinkMark (outLED);
  302.  
  303. else if (sum == 51)
  304. {
  305. pClickedOnce = false;
  306. rClickedOnce = false;
  307. digitalWrite (playLED, LOW);
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment