PageRenderTime 132ms CodeModel.GetById 46ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/examples/Topics/Other software/SerialReadMaxMSP/SerialReadMaxMSP.pde

https://bitbucket.org/bhagman/wiring
Processing | 523 lines | 23 code | 5 blank | 495 comment | 6 complexity | 54be673eae12c6979e9c1649fd3f660f MD5 | raw file
  1. /**
  2. * Serial Read and MaxMSP
  3. * by BARRAGAN http://barraganstudio.com
  4. *
  5. * Demonstrates how to read data from the serial port.
  6. * If the data received is an 'H', the light turns ON,
  7. * if it is an 'L', the light turns OFF.
  8. * The MaxMSP patch is included as comented text below.
  9. * Copy the text of the patch open a new patch in MaxMSP and paste.
  10. * For the Wiring boards v1 the on-board LED is on pin 48,
  11. * on Wiring S the on-board LED is on pin 15.
  12. */
  13. int val; // variable to receive data from the serial port
  14. int ledpin = WLED; // Wiring board LED
  15. void setup()
  16. {
  17. pinMode(ledpin, OUTPUT); // pin WLED (on-board LED) as OUTPUT
  18. Serial.begin(9600); // start serial communication at 9600bps
  19. }
  20. void loop()
  21. {
  22. if (Serial.available()) // if data is available to read
  23. {
  24. val = Serial.read(); // read it and store it in 'val'
  25. if (val == 'H') // if 'H' was received
  26. {
  27. digitalWrite(ledpin, HIGH); // turn ON the LED
  28. }
  29. else if (val == 'L')
  30. {
  31. digitalWrite(ledpin, LOW); // otherwise turn it OFF
  32. }
  33. }
  34. delay(100); // wait 100ms for next reading
  35. }
  36. /* Processing code for this example
  37. // mouseover serial
  38. // by BARRAGAN <http://barraganstudio.com>
  39. // Demonstrates how to send data to the Wiring I/O board, in order to
  40. // turn ON a light if the mouse is over a rectangle and turn it off
  41. // if the mouse is not.
  42. // created 13 May 2004
  43. // revised 6 November 2009
  44. import processing.serial.*;
  45. Serial port;
  46. boolean firstTime = true;
  47. void setup()
  48. {
  49. size(200, 200);
  50. noStroke();
  51. // List all the available serial ports in the output pane.
  52. // You will need to choose the port that the Wiring board is
  53. // connected to from this list. The first port in the list is
  54. // port #0 and the third port in the list is port #2.
  55. println(Serial.list());
  56. // Open the port that the Wiring board is connected to (in this case #1)
  57. // Make sure to open the port at the same speed Wiring is using (9600bps)
  58. port = new Serial(this, Serial.list()[1], 9600);
  59. }
  60. // function to test if mouse is over square
  61. boolean mouseOverRect()
  62. {
  63. return ((mouseX >= 50)&&(mouseX <= 150)&&(mouseY >= 50)&(mouseY <= 150));
  64. }
  65. void draw()
  66. {
  67. // Wait 3 seconds for the Wiring board to start
  68. if(firstTime) {
  69. delay(3000);
  70. firstTime = false;
  71. }
  72. background(#222222);
  73. if(mouseOverRect()) // if mouse is over square
  74. {
  75. fill(#BBBBB0); // change color
  76. port.write('H'); // send an 'H' to indicate mouse is over square
  77. } else {
  78. fill(#666660); // change color
  79. port.write('L'); // send an 'L' otherwise
  80. }
  81. rect(50, 50, 100, 100); // draw square
  82. }
  83. */
  84. /* MaxMSP patch copy, go to Max open a new patch and paste
  85. {
  86. "boxes" : [ {
  87. "box" : {
  88. "maxclass" : "comment",
  89. "text" : "atoi function to convert the characters H or L to integer befor sending them",
  90. "patching_rect" : [ 209.0, 283.0, 405.0, 20.0 ],
  91. "id" : "obj-27",
  92. "fontname" : "Arial",
  93. "numinlets" : 1,
  94. "numoutlets" : 0,
  95. "fontsize" : 12.0
  96. }
  97. }
  98. , {
  99. "box" : {
  100. "maxclass" : "comment",
  101. "text" : "toggle to blink the LED every second",
  102. "linecount" : 3,
  103. "patching_rect" : [ 26.0, 12.0, 93.0, 48.0 ],
  104. "id" : "obj-26",
  105. "fontname" : "Arial",
  106. "numinlets" : 1,
  107. "numoutlets" : 0,
  108. "fontsize" : 12.0
  109. }
  110. }
  111. , {
  112. "box" : {
  113. "maxclass" : "comment",
  114. "text" : "toggle to send an H or an L to turn On or OFF the LED",
  115. "linecount" : 4,
  116. "patching_rect" : [ 147.0, 68.0, 93.0, 62.0 ],
  117. "id" : "obj-25",
  118. "fontname" : "Arial",
  119. "numinlets" : 1,
  120. "numoutlets" : 0,
  121. "fontsize" : 12.0
  122. }
  123. }
  124. , {
  125. "box" : {
  126. "maxclass" : "toggle",
  127. "patching_rect" : [ 113.0, 69.0, 20.0, 20.0 ],
  128. "id" : "obj-24",
  129. "numinlets" : 1,
  130. "numoutlets" : 1,
  131. "outlettype" : [ "int" ]
  132. }
  133. }
  134. , {
  135. "box" : {
  136. "maxclass" : "newobj",
  137. "text" : "atoi",
  138. "patching_rect" : [ 112.0, 290.0, 46.0, 20.0 ],
  139. "id" : "obj-23",
  140. "fontname" : "Arial",
  141. "numinlets" : 3,
  142. "numoutlets" : 1,
  143. "fontsize" : 12.0,
  144. "outlettype" : [ "list" ]
  145. }
  146. }
  147. , {
  148. "box" : {
  149. "maxclass" : "message",
  150. "text" : "H",
  151. "patching_rect" : [ 156.0, 252.0, 32.5, 18.0 ],
  152. "id" : "obj-22",
  153. "fontname" : "Arial",
  154. "numinlets" : 2,
  155. "numoutlets" : 1,
  156. "fontsize" : 12.0,
  157. "outlettype" : [ "" ]
  158. }
  159. }
  160. , {
  161. "box" : {
  162. "maxclass" : "message",
  163. "text" : "L",
  164. "patching_rect" : [ 112.0, 252.0, 32.5, 18.0 ],
  165. "id" : "obj-19",
  166. "fontname" : "Arial",
  167. "numinlets" : 2,
  168. "numoutlets" : 1,
  169. "fontsize" : 12.0,
  170. "outlettype" : [ "" ]
  171. }
  172. }
  173. , {
  174. "box" : {
  175. "maxclass" : "newobj",
  176. "text" : "select 0 1",
  177. "patching_rect" : [ 113.0, 220.0, 62.0, 20.0 ],
  178. "id" : "obj-14",
  179. "fontname" : "Arial",
  180. "numinlets" : 1,
  181. "numoutlets" : 3,
  182. "fontsize" : 12.0,
  183. "outlettype" : [ "bang", "bang", "" ]
  184. }
  185. }
  186. , {
  187. "box" : {
  188. "maxclass" : "toggle",
  189. "patching_rect" : [ 26.0, 69.0, 20.0, 20.0 ],
  190. "id" : "obj-13",
  191. "numinlets" : 1,
  192. "numoutlets" : 1,
  193. "outlettype" : [ "int" ]
  194. }
  195. }
  196. , {
  197. "box" : {
  198. "maxclass" : "toggle",
  199. "patching_rect" : [ 25.0, 139.0, 20.0, 20.0 ],
  200. "id" : "obj-12",
  201. "numinlets" : 1,
  202. "numoutlets" : 1,
  203. "outlettype" : [ "int" ]
  204. }
  205. }
  206. , {
  207. "box" : {
  208. "maxclass" : "newobj",
  209. "text" : "metro 1000",
  210. "patching_rect" : [ 25.0, 104.0, 71.0, 20.0 ],
  211. "id" : "obj-6",
  212. "fontname" : "Arial",
  213. "numinlets" : 2,
  214. "numoutlets" : 1,
  215. "fontsize" : 12.0,
  216. "outlettype" : [ "bang" ]
  217. }
  218. }
  219. , {
  220. "box" : {
  221. "maxclass" : "comment",
  222. "text" : "toggle object to open/close the serial and start/stop the patch",
  223. "linecount" : 3,
  224. "patching_rect" : [ 196.0, 12.0, 150.0, 48.0 ],
  225. "id" : "obj-18",
  226. "fontname" : "Arial",
  227. "numinlets" : 1,
  228. "numoutlets" : 0,
  229. "fontsize" : 12.0
  230. }
  231. }
  232. , {
  233. "box" : {
  234. "maxclass" : "toggle",
  235. "patching_rect" : [ 166.0, 28.0, 20.0, 20.0 ],
  236. "id" : "obj-17",
  237. "numinlets" : 1,
  238. "numoutlets" : 1,
  239. "outlettype" : [ "int" ],
  240. "bgcolor" : [ 1.0, 0.337255, 0.619608, 1.0 ]
  241. }
  242. }
  243. , {
  244. "box" : {
  245. "maxclass" : "comment",
  246. "text" : "select object with two options, to trigger messages, close and port ",
  247. "linecount" : 2,
  248. "patching_rect" : [ 315.0, 148.0, 227.0, 34.0 ],
  249. "id" : "obj-16",
  250. "fontname" : "Arial",
  251. "numinlets" : 1,
  252. "numoutlets" : 0,
  253. "fontsize" : 12.0
  254. }
  255. }
  256. , {
  257. "box" : {
  258. "maxclass" : "newobj",
  259. "text" : "select 0 1",
  260. "patching_rect" : [ 222.0, 153.0, 62.0, 20.0 ],
  261. "id" : "obj-15",
  262. "fontname" : "Arial",
  263. "numinlets" : 1,
  264. "numoutlets" : 3,
  265. "fontsize" : 12.0,
  266. "outlettype" : [ "bang", "bang", "" ]
  267. }
  268. }
  269. , {
  270. "box" : {
  271. "maxclass" : "comment",
  272. "text" : "The serial object, port at 9600bps",
  273. "patching_rect" : [ 209.0, 354.0, 188.0, 20.0 ],
  274. "id" : "obj-11",
  275. "fontname" : "Arial",
  276. "numinlets" : 1,
  277. "numoutlets" : 0,
  278. "fontsize" : 12.0
  279. }
  280. }
  281. , {
  282. "box" : {
  283. "maxclass" : "message",
  284. "text" : "close",
  285. "patching_rect" : [ 251.0, 252.0, 39.0, 18.0 ],
  286. "id" : "obj-10",
  287. "fontname" : "Arial",
  288. "numinlets" : 2,
  289. "numoutlets" : 1,
  290. "fontsize" : 12.0,
  291. "outlettype" : [ "" ]
  292. }
  293. }
  294. , {
  295. "box" : {
  296. "maxclass" : "comment",
  297. "text" : "add some message boxes and type their names for the basic communication, \"print\" (will print a list of serial ports), \"port a\" (to open the serial port) and \"close\" to close the serial port",
  298. "linecount" : 6,
  299. "patching_rect" : [ 315.0, 190.0, 175.0, 89.0 ],
  300. "id" : "obj-9",
  301. "fontname" : "Arial",
  302. "numinlets" : 1,
  303. "numoutlets" : 0,
  304. "fontsize" : 12.0
  305. }
  306. }
  307. , {
  308. "box" : {
  309. "maxclass" : "message",
  310. "text" : "print",
  311. "patching_rect" : [ 251.0, 190.0, 34.0, 18.0 ],
  312. "id" : "obj-8",
  313. "fontname" : "Arial",
  314. "numinlets" : 2,
  315. "numoutlets" : 1,
  316. "fontsize" : 12.0,
  317. "outlettype" : [ "" ]
  318. }
  319. }
  320. , {
  321. "box" : {
  322. "maxclass" : "message",
  323. "text" : "port a",
  324. "patching_rect" : [ 251.0, 220.0, 41.0, 18.0 ],
  325. "id" : "obj-7",
  326. "fontname" : "Arial",
  327. "numinlets" : 2,
  328. "numoutlets" : 1,
  329. "fontsize" : 12.0,
  330. "outlettype" : [ "" ]
  331. }
  332. }
  333. , {
  334. "box" : {
  335. "maxclass" : "newobj",
  336. "text" : "serial a 9600",
  337. "patching_rect" : [ 112.0, 354.0, 79.0, 20.0 ],
  338. "id" : "obj-4",
  339. "fontname" : "Arial",
  340. "numinlets" : 1,
  341. "numoutlets" : 2,
  342. "fontsize" : 12.0,
  343. "outlettype" : [ "int", "" ]
  344. }
  345. }
  346. ],
  347. "lines" : [ {
  348. "patchline" : {
  349. "source" : [ "obj-17", 0 ],
  350. "destination" : [ "obj-15", 0 ],
  351. "hidden" : 0,
  352. "midpoints" : [ 175.5, 63.0, 249.0, 63.0, 249.0, 147.0, 231.5, 147.0 ]
  353. }
  354. }
  355. , {
  356. "patchline" : {
  357. "source" : [ "obj-23", 0 ],
  358. "destination" : [ "obj-4", 0 ],
  359. "hidden" : 0,
  360. "midpoints" : [ ]
  361. }
  362. }
  363. , {
  364. "patchline" : {
  365. "source" : [ "obj-19", 0 ],
  366. "destination" : [ "obj-23", 0 ],
  367. "hidden" : 0,
  368. "midpoints" : [ ]
  369. }
  370. }
  371. , {
  372. "patchline" : {
  373. "source" : [ "obj-22", 0 ],
  374. "destination" : [ "obj-23", 0 ],
  375. "hidden" : 0,
  376. "midpoints" : [ ]
  377. }
  378. }
  379. , {
  380. "patchline" : {
  381. "source" : [ "obj-14", 0 ],
  382. "destination" : [ "obj-19", 0 ],
  383. "hidden" : 0,
  384. "midpoints" : [ 122.5, 240.0, 121.5, 240.0 ]
  385. }
  386. }
  387. , {
  388. "patchline" : {
  389. "source" : [ "obj-14", 1 ],
  390. "destination" : [ "obj-22", 0 ],
  391. "hidden" : 0,
  392. "midpoints" : [ 144.0, 249.0, 165.5, 249.0 ]
  393. }
  394. }
  395. , {
  396. "patchline" : {
  397. "source" : [ "obj-24", 0 ],
  398. "destination" : [ "obj-14", 0 ],
  399. "hidden" : 0,
  400. "midpoints" : [ ]
  401. }
  402. }
  403. , {
  404. "patchline" : {
  405. "source" : [ "obj-12", 0 ],
  406. "destination" : [ "obj-14", 0 ],
  407. "hidden" : 0,
  408. "midpoints" : [ 34.5, 207.0, 122.5, 207.0 ]
  409. }
  410. }
  411. , {
  412. "patchline" : {
  413. "source" : [ "obj-6", 0 ],
  414. "destination" : [ "obj-12", 0 ],
  415. "hidden" : 0,
  416. "midpoints" : [ 34.5, 126.0, 34.5, 126.0 ]
  417. }
  418. }
  419. , {
  420. "patchline" : {
  421. "source" : [ "obj-13", 0 ],
  422. "destination" : [ "obj-6", 0 ],
  423. "hidden" : 0,
  424. "midpoints" : [ 35.5, 90.0, 34.5, 90.0 ]
  425. }
  426. }
  427. , {
  428. "patchline" : {
  429. "source" : [ "obj-8", 0 ],
  430. "destination" : [ "obj-4", 0 ],
  431. "hidden" : 0,
  432. "midpoints" : [ ]
  433. }
  434. }
  435. , {
  436. "patchline" : {
  437. "source" : [ "obj-7", 0 ],
  438. "destination" : [ "obj-4", 0 ],
  439. "hidden" : 0,
  440. "midpoints" : [ ]
  441. }
  442. }
  443. , {
  444. "patchline" : {
  445. "source" : [ "obj-10", 0 ],
  446. "destination" : [ "obj-4", 0 ],
  447. "hidden" : 0,
  448. "midpoints" : [ ]
  449. }
  450. }
  451. , {
  452. "patchline" : {
  453. "source" : [ "obj-15", 1 ],
  454. "destination" : [ "obj-7", 0 ],
  455. "hidden" : 0,
  456. "midpoints" : [ 253.0, 186.0, 246.0, 186.0, 246.0, 213.0, 260.5, 213.0 ]
  457. }
  458. }
  459. , {
  460. "patchline" : {
  461. "source" : [ "obj-15", 0 ],
  462. "destination" : [ "obj-10", 0 ],
  463. "hidden" : 0,
  464. "midpoints" : [ 231.5, 249.0, 260.5, 249.0 ]
  465. }
  466. }
  467. ]
  468. }
  469. */