Advertisement
Guest User

ard

a guest
Mar 7th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.50 KB | None | 0 0
  1. #include <Servo.h>
  2. /* Analog and Digital Input and Output Server for MATLAB */
  3. /* Giampiero Campa, Copyright 2009 The MathWorks, Inc */
  4.  
  5. /* This file is meant to be used with the MATLAB arduino IO
  6. package, however, it can be used from the IDE environment
  7. (or any other serial terminal) by typing commands like:
  8.  
  9. 0e0 : assigns digital pin #4 (e) as input
  10. 0f1 : assigns digital pin #5 (f) as output
  11. 0n1 : assigns digital pin #13 (n) as output
  12.  
  13. 1c : reads digital pin #2 (c)
  14. 1e : reads digital pin #4 (e)
  15. 2n0 : sets digital pin #13 (n) low
  16. 2n1 : sets digital pin #13 (n) high
  17. 2f1 : sets digital pin #5 (f) high
  18. 2f0 : sets digital pin #5 (f) low
  19. 4j2 : sets digital pin #9 (j) to 50=ascii(2) over 255
  20. 4jz : sets digital pin #9 (j) to 122=ascii(z) over 255
  21. 3a : reads analog pin #0 (a)
  22. 3f : reads analog pin #5 (f)
  23.  
  24. R0 : sets analog reference to DEFAULT
  25. R1 : sets analog reference to INTERNAL
  26. R2 : sets analog reference to EXTERNAL
  27.  
  28. 99 : returns script type (1 basic, 2 motor, 3 general) */
  29.  
  30. /* define internal for the MEGA as 1.1V (as as for the 328) */
  31. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  32. #define INTERNAL INTERNAL1V1
  33. #endif
  34.  
  35. Servo servo1;
  36. Servo servo2;
  37.  
  38. void setup() {
  39. /* Make sure all pins are put in high impedence state and
  40. that their registers are set as low before doing anything.
  41. This puts the board in a known (and harmless) state */
  42. int i;
  43. for (i=0;i<20;i++) {
  44. pinMode(i,INPUT);
  45. digitalWrite(i,0);
  46. }
  47. /* initialize serial */
  48. Serial.begin(115200);
  49. }
  50.  
  51.  
  52. void loop() {
  53.  
  54. /* variables declaration and initialization */
  55.  
  56. static int s = -1; /* state */
  57. static int pin = 13; /* generic pin number */
  58.  
  59. int val = 0; /* generic value read from serial */
  60. int agv = 0; /* generic analog value */
  61. int dgv = 0; /* generic digital value */
  62. static int srv = 2;
  63. /* The following instruction constantly checks if anything
  64. is available on the serial port. Nothing gets executed in
  65. the loop if nothing is available to be read, but as soon
  66. as anything becomes available, then the part coded after
  67. the if statement (that is the real stuff) gets executed */
  68.  
  69. if (Serial.available() >0) {
  70.  
  71. /* whatever is available from the serial is read here */
  72. val = Serial.read();
  73.  
  74. /* This part basically implements a state machine that
  75. reads the serial port and makes just one transition
  76. to a new state, depending on both the previous state
  77. and the command that is read from the serial port.
  78. Some commands need additional inputs from the serial
  79. port, so they need 2 or 3 state transitions (each one
  80. happening as soon as anything new is available from
  81. the serial port) to be fully executed. After a command
  82. is fully executed the state returns to its initial
  83. value s=-1 */
  84.  
  85. switch (s) {
  86.  
  87.  
  88. /* s=-1 means NOTHING RECEIVED YET ******************* */
  89. case -1:
  90.  
  91. /* calculate next state */
  92. if (val>47 && val<90) {
  93. /* the first received value indicates the mode
  94. 49 is ascii for 1, ... 90 is ascii for Z
  95. s=0 is change-pin mode
  96. s=10 is DI; s=20 is DO; s=30 is AI; s=40 is AO;
  97. s=50 is servo status; s=60 is aervo attach/detach;
  98. s=70 is servo read; s=80 is servo write
  99. s=90 is query script type (1 basic, 2 motor)
  100. s=340 is change analog reference
  101. */
  102. s=10*(val-48);
  103. }
  104.  
  105. /* the following statements are needed to handle
  106. unexpected first values coming from the serial (if
  107. the value is unrecognized then it defaults to s=-1) */
  108. if ((s>40 && s<90) || (s>90 && s!=340)) {
  109. s=-1;
  110. }
  111.  
  112. /* the break statements gets out of the switch-case, so
  113. /* we go back to line 97 and wait for new serial data */
  114. break; /* s=-1 (initial state) taken care of */
  115.  
  116.  
  117.  
  118. /* s=0 or 1 means CHANGE PIN MODE */
  119.  
  120. case 0:
  121. /* the second received value indicates the pin
  122. from abs('c')=99, pin 2, to abs('t')=116, pin 19 */
  123. if (val>98 && val<117) {
  124. pin=val-97; /* calculate pin */
  125. s=1; /* next we will need to get 0 or 1 from serial */
  126. }
  127. else {
  128. s=-1; /* if value is not a pin then return to -1 */
  129. }
  130. break; /* s=0 taken care of */
  131.  
  132.  
  133. case 1:
  134. /* the third received value indicates the value 0 or 1 */
  135. if (val>47 && val<50) {
  136. /* set pin mode */
  137. if (val==48) {
  138. pinMode(pin,INPUT);
  139. }
  140. else {
  141. pinMode(pin,OUTPUT);
  142. }
  143. }
  144. s=-1; /* we are done with CHANGE PIN so go to -1 */
  145. break; /* s=1 taken care of */
  146.  
  147.  
  148.  
  149. /* s=10 means DIGITAL INPUT ************************** */
  150.  
  151. case 10:
  152. /* the second received value indicates the pin
  153. from abs('c')=99, pin 2, to abs('t')=116, pin 19 */
  154. if (val>98 && val<117) {
  155. pin=val-97; /* calculate pin */
  156. dgv=digitalRead(pin); /* perform Digital Input */
  157. Serial.println(dgv); /* send value via serial */
  158. }
  159. s=-1; /* we are done with DI so next state is -1 */
  160. break; /* s=10 taken care of */
  161.  
  162.  
  163.  
  164. /* s=20 or 21 means DIGITAL OUTPUT ******************* */
  165.  
  166. case 20:
  167. /* the second received value indicates the pin
  168. from abs('c')=99, pin 2, to abs('t')=116, pin 19 */
  169. if (val>98 && val<117) {
  170. pin=val-97; /* calculate pin */
  171. s=21; /* next we will need to get 0 or 1 from serial */
  172. }
  173. else {
  174. s=-1; /* if value is not a pin then return to -1 */
  175. }
  176. break; /* s=20 taken care of */
  177.  
  178. case 21:
  179. /* the third received value indicates the value 0 or 1 */
  180. if (val>47 && val<50) {
  181. dgv=val-48; /* calculate value */
  182. digitalWrite(pin,dgv); /* perform Digital Output */
  183. }
  184. s=-1; /* we are done with DO so next state is -1 */
  185. break; /* s=21 taken care of */
  186.  
  187.  
  188.  
  189. /* s=30 means ANALOG INPUT *************************** */
  190.  
  191. case 30:
  192. /* the second received value indicates the pin
  193. from abs('a')=97, pin 0, to abs('f')=102, pin 6,
  194. note that these are the digital pins from 14 to 19
  195. located in the lower right part of the board */
  196. if (val>96 && val<103) {
  197. pin=val-97; /* calculate pin */
  198. agv=analogRead(pin); /* perform Analog Input */
  199. Serial.println(agv); /* send value via serial */
  200. }
  201. s=-1; /* we are done with AI so next state is -1 */
  202. break; /* s=30 taken care of */
  203.  
  204.  
  205.  
  206. /* s=40 or 41 means ANALOG OUTPUT ******************** */
  207.  
  208. case 40:
  209. /* the second received value indicates the pin
  210. from abs('c')=99, pin 2, to abs('t')=116, pin 19 */
  211. if (val>98 && val<117) {
  212. pin=val-97; /* calculate pin */
  213. s=41; /* next we will need to get value from serial */
  214. }
  215. else {
  216. s=-1; /* if value is not a pin then return to -1 */
  217. }
  218. break; /* s=40 taken care of */
  219.  
  220.  
  221. case 41:
  222. /* the third received value indicates the analog value */
  223. analogWrite(pin,val); /* perform Analog Output */
  224. s=-1; /* we are done with AO so next state is -1 */
  225. break; /* s=41 taken care of */
  226.  
  227. /* s=50 means SERVO STATUS (ATTACHED/DETACHED) ******* */
  228.  
  229. case 50:
  230. /* the second received value indicates the servo number
  231. from abs('a')=97, servo1, on top, uses digital pin 10
  232. to abs('b')=98, servo2, bottom, uses digital pin 9 */
  233. if (val>96 && val<99) {
  234. srv=val-96; /* calculate srv */
  235. if (srv==1) dgv=servo1.attached(); /* read status */
  236. if (srv==2) dgv=servo2.attached();
  237. Serial.println(dgv); /* send value via serial */
  238. }
  239. s=-1; /* we are done with servo status so return to -1*/
  240. break; /* s=50 taken care of */
  241.  
  242.  
  243. /* s=60 or 61 means SERVO ATTACH/DETACH ************** */
  244.  
  245. case 60:
  246. /* the second received value indicates the servo number
  247. from abs('a')=97, servo1, on top, uses digital pin 10
  248. to abs('b')=98, servo2, bottom, uses digital pin 9 */
  249. if (val>96 && val<99) {
  250. srv=val-96; /* calculate srv */
  251. s=61; /* next we will need to get 0 or 1 from serial */
  252. }
  253. else {
  254. s=-1; /* if value is not a servo then return to -1 */
  255. }
  256. break; /* s=60 taken care of */
  257.  
  258. case 61:
  259. /* the third received value indicates the value 0 or 1
  260. 0 for detach and 1 for attach */
  261. if (val>47 && val<50) {
  262. dgv=val-48; /* calculate value */
  263. if (srv==1) {
  264. if (dgv) servo1.attach(val); /* attach servo 1 */
  265. else servo1.detach(); /* detach servo 1 */
  266. }
  267. if (srv==2) {
  268. if (dgv) servo2.attach(9); /* attach servo 2 */
  269. else servo2.detach(); /* detach servo 2 */
  270. }
  271. }
  272. s=-1; /* we are done with servo attach/detach so -1 */
  273. break; /* s=61 taken care of */
  274.  
  275.  
  276. /* s=70 means SERVO READ ***************************** */
  277.  
  278. case 70:
  279. /* the second received value indicates the servo number
  280. from abs('a')=97, servo1, on top, uses digital pin 10
  281. to abs('b')=98, servo2, bottom, uses digital pin 9 */
  282. if (val>96 && val<99) {
  283. srv=val-96; /* calculate servo number */
  284. if (srv==1) agv=servo1.read(); /* read value */
  285. if (srv==2) agv=servo2.read();
  286. Serial.println(agv); /* send value via serial */
  287. }
  288. s=-1; /* we are done with servo read so go to -1 next */
  289. break; /* s=70 taken care of */
  290.  
  291.  
  292. /* s=80 or 81 means SERVO WRITE ******************** */
  293.  
  294. case 80:
  295. /* the second received value indicates the servo number
  296. from abs('a')=97, servo1, on top, uses digital pin 10
  297. to abs('b')=98, servo2, bottom, uses digital pin 9 */
  298. if (val>96 && val<99) {
  299. srv=val-96; /* calculate servo number */
  300. s=81; /* next we will need to get value from serial */
  301. }
  302. else {
  303. s=-1; /* if value is not a servo then return to -1 */
  304. }
  305. break; /* s=80 taken care of */
  306.  
  307.  
  308. case 81:
  309. /* the third received value indicates the servo angle */
  310. if (srv==1) servo1.write(val); /* write value */
  311. if (srv==2) servo2.write(val);
  312. s=-1; /* we are done with servo write so go to -1 next*/
  313. break; /* s=81 taken care of */
  314.  
  315.  
  316. /* s=90 means Query Script Type (1 basic, 2 motor) */
  317. case 90:
  318. if (val==57) {
  319. /* if string sent is 99 send script type via serial */
  320. Serial.println(1);
  321. }
  322. s=-1; /* we are done with this so next state is -1 */
  323. break; /* s=90 taken care of */
  324.  
  325.  
  326.  
  327. /* s=340 or 341 means ANALOG REFERENCE *************** */
  328.  
  329. case 340:
  330. /* the second received value indicates the reference,
  331. which is encoded as is 0,1,2 for DEFAULT, INTERNAL
  332. and EXTERNAL, respectively */
  333.  
  334. switch (val) {
  335.  
  336. case 48:
  337. analogReference(DEFAULT);
  338. break;
  339.  
  340. case 49:
  341. analogReference(INTERNAL);
  342. break;
  343.  
  344. case 50:
  345. analogReference(EXTERNAL);
  346. break;
  347.  
  348. default: /* unrecognized, no action */
  349. break;
  350. }
  351.  
  352. s=-1; /* we are done with this so next state is -1 */
  353. break; /* s=341 taken care of */
  354.  
  355.  
  356.  
  357. /* ******* UNRECOGNIZED STATE, go back to s=-1 ******* */
  358.  
  359. default:
  360. /* we should never get here but if we do it means we
  361. are in an unexpected state so whatever is the second
  362. received value we get out of here and back to s=-1 */
  363.  
  364. s=-1; /* go back to the initial state, break unneeded */
  365.  
  366.  
  367.  
  368. } /* end switch on state s */
  369.  
  370. } /* end if serial available */
  371.  
  372. } /* end loop statement */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement