~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to examples/Communication/Graph/Graph.pde

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Graph
 
3
 
 
4
 A simple example of communication from the Arduino board to the computer:
 
5
 the value of analog input 0 is sent out the serial port.  We call this "serial"
 
6
 communication because the connection appears to both the Arduino and the
 
7
 computer as a serial port, even though it may actually use
 
8
 a USB cable. Bytes are sent one after another (serially) from the Arduino
 
9
 to the computer.
 
10
 
 
11
 You can use the Arduino serial monitor to view the sent data, or it can
 
12
 be read by Processing, PD, Max/MSP, or any other program capable of reading 
 
13
 data from a serial port.  The Processing code below graphs the data received 
 
14
 so you can see the value of the analog input changing over time.
 
15
 
 
16
 The circuit:
 
17
 Any analog input sensor is attached to analog in pin 0.
 
18
 
 
19
 http://www.arduino.cc/en/Tutorial/Graph
 
20
 
 
21
 created 2006
 
22
 by David A. Mellis
 
23
 modified 14 Apr 2009
 
24
 by Tom Igoe and Scott Fitzgerald
 
25
 
 
26
 http://www.arduino.cc/en/Tutorial/Graph
 
27
 */
 
28
 
 
29
void setup() {
 
30
  // initialize the serial communication:
 
31
  Serial.begin(9600);
 
32
}
 
33
 
 
34
void loop() {
 
35
  // send the value of analog input 0:
 
36
  Serial.println(analogRead(0));
 
37
  // wait a bit for the analog-to-digital converter 
 
38
  // to stabilize after the last reading:
 
39
  delay(10);
 
40
}
 
41
 
 
42
/* Processing code for this example
 
43
 
 
44
 // Graphing sketch
 
45
 
 
46
 
 
47
 // This program takes ASCII-encoded strings
 
48
 // from the serial port at 9600 baud and graphs them. It expects values in the
 
49
 // range 0 to 1023, followed by a newline, or newline and carriage return
 
50
 
 
51
 // Created 20 Apr 2005
 
52
 // Updated 18 Jan 2008
 
53
 // by Tom Igoe
 
54
 
 
55
 import processing.serial.*;
 
56
 
 
57
 Serial myPort;        // The serial port
 
58
 int xPos = 1;         // horizontal position of the graph
 
59
 
 
60
 void setup () {
 
61
 // set the window size:
 
62
 size(400, 300);        
 
63
 
 
64
 // List all the available serial ports
 
65
 println(Serial.list());
 
66
 // I know that the first port in the serial list on my mac
 
67
 // is always my  Arduino, so I open Serial.list()[0].
 
68
 // Open whatever port is the one you're using.
 
69
 myPort = new Serial(this, Serial.list()[0], 9600);
 
70
 // don't generate a serialEvent() unless you get a newline character:
 
71
 myPort.bufferUntil('\n');
 
72
 // set inital background:
 
73
 background(0);
 
74
 }
 
75
 void draw () {
 
76
 // everything happens in the serialEvent()
 
77
 }
 
78
 
 
79
 void serialEvent (Serial myPort) {
 
80
 // get the ASCII string:
 
81
 String inString = myPort.readStringUntil('\n');
 
82
 
 
83
 if (inString != null) {
 
84
 // trim off any whitespace:
 
85
 inString = trim(inString);
 
86
 // convert to an int and map to the screen height:
 
87
 float inByte = float(inString); 
 
88
 inByte = map(inByte, 0, 1023, 0, height);
 
89
 
 
90
 // draw the line:
 
91
 stroke(127,34,255);
 
92
 line(xPos, height, xPos, height - inByte);
 
93
 
 
94
 // at the edge of the screen, go back to the beginning:
 
95
 if (xPos >= width) {
 
96
 xPos = 0;
 
97
 background(0); 
 
98
 } 
 
99
 else {
 
100
 // increment the horizontal position:
 
101
 xPos++;
 
102
 }
 
103
 }
 
104
 }
 
105
 
 
106
 */
 
107
 
 
108
/* Max/MSP v5 patch for this example
 
109
 {
 
110
        "boxes" : [             {
 
111
                        "box" :                         {
 
112
                                "maxclass" : "comment",
 
113
                                "text" : "Graph\n\nThis patch takes a string, containing ASCII formatted number from 0 to 1023, with a carriage return and linefeed at the end.  It converts the string to an integer and graphs it.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
 
114
                                "linecount" : 10,
 
115
                                "patching_rect" : [ 479.0, 6.0, 344.0, 144.0 ],
 
116
                                "numoutlets" : 0,
 
117
                                "fontsize" : 12.0,
 
118
                                "id" : "obj-32",
 
119
                                "fontname" : "Arial",
 
120
                                "numinlets" : 1
 
121
                        }
 
122
 
 
123
                }
 
124
 ,              {
 
125
                        "box" :                         {
 
126
                                "maxclass" : "newobj",
 
127
                                "text" : "select 0 1",
 
128
                                "patching_rect" : [ 327.0, 80.0, 62.0, 20.0 ],
 
129
                                "numoutlets" : 3,
 
130
                                "fontsize" : 12.0,
 
131
                                "outlettype" : [ "bang", "bang", "" ],
 
132
                                "id" : "obj-30",
 
133
                                "fontname" : "Arial",
 
134
                                "numinlets" : 1
 
135
                        }
 
136
 
 
137
                }
 
138
 ,              {
 
139
                        "box" :                         {
 
140
                                "maxclass" : "comment",
 
141
                                "text" : "click here to close the serial port",
 
142
                                "patching_rect" : [ 412.0, 231.0, 206.0, 20.0 ],
 
143
                                "numoutlets" : 0,
 
144
                                "fontsize" : 12.0,
 
145
                                "id" : "obj-26",
 
146
                                "fontname" : "Arial",
 
147
                                "numinlets" : 1
 
148
                        }
 
149
 
 
150
                }
 
151
 ,              {
 
152
                        "box" :                         {
 
153
                                "maxclass" : "comment",
 
154
                                "text" : "click here to open the serial port",
 
155
                                "patching_rect" : [ 412.0, 205.0, 206.0, 20.0 ],
 
156
                                "numoutlets" : 0,
 
157
                                "fontsize" : 12.0,
 
158
                                "id" : "obj-27",
 
159
                                "fontname" : "Arial",
 
160
                                "numinlets" : 1
 
161
                        }
 
162
 
 
163
                }
 
164
 ,              {
 
165
                        "box" :                         {
 
166
                                "maxclass" : "message",
 
167
                                "text" : "close",
 
168
                                "patching_rect" : [ 327.0, 231.0, 39.0, 18.0 ],
 
169
                                "numoutlets" : 1,
 
170
                                "fontsize" : 12.0,
 
171
                                "outlettype" : [ "" ],
 
172
                                "id" : "obj-21",
 
173
                                "fontname" : "Arial",
 
174
                                "numinlets" : 2
 
175
                        }
 
176
 
 
177
                }
 
178
 ,              {
 
179
                        "box" :                         {
 
180
                                "maxclass" : "message",
 
181
                                "text" : "port a",
 
182
                                "patching_rect" : [ 349.0, 205.0, 41.0, 18.0 ],
 
183
                                "numoutlets" : 1,
 
184
                                "fontsize" : 12.0,
 
185
                                "outlettype" : [ "" ],
 
186
                                "id" : "obj-19",
 
187
                                "fontname" : "Arial",
 
188
                                "numinlets" : 2
 
189
                        }
 
190
 
 
191
                }
 
192
 ,              {
 
193
                        "box" :                         {
 
194
                                "maxclass" : "multislider",
 
195
                                "candicane7" : [ 0.878431, 0.243137, 0.145098, 1.0 ],
 
196
                                "patching_rect" : [ 302.0, 450.0, 246.0, 167.0 ],
 
197
                                "contdata" : 1,
 
198
                                "numoutlets" : 2,
 
199
                                "peakcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
 
200
                                "slidercolor" : [ 0.066667, 0.058824, 0.776471, 1.0 ],
 
201
                                "candicane8" : [ 0.027451, 0.447059, 0.501961, 1.0 ],
 
202
                                "outlettype" : [ "", "" ],
 
203
                                "setminmax" : [ 0.0, 1023.0 ],
 
204
                                "settype" : 0,
 
205
                                "candicane6" : [ 0.733333, 0.035294, 0.788235, 1.0 ],
 
206
                                "setstyle" : 3,
 
207
                                "bgcolor" : [ 0.231373, 0.713726, 1.0, 1.0 ],
 
208
                                "id" : "obj-1",
 
209
                                "candicane4" : [ 0.439216, 0.619608, 0.070588, 1.0 ],
 
210
                                "candicane5" : [ 0.584314, 0.827451, 0.431373, 1.0 ],
 
211
                                "candicane2" : [ 0.145098, 0.203922, 0.356863, 1.0 ],
 
212
                                "candicane3" : [ 0.290196, 0.411765, 0.713726, 1.0 ],
 
213
                                "numinlets" : 1
 
214
                        }
 
215
 
 
216
                }
 
217
 ,              {
 
218
                        "box" :                         {
 
219
                                "maxclass" : "comment",
 
220
                                "text" : "Click here to get a list of serial ports",
 
221
                                "patching_rect" : [ 412.0, 179.0, 207.0, 20.0 ],
 
222
                                "numoutlets" : 0,
 
223
                                "fontsize" : 12.0,
 
224
                                "id" : "obj-2",
 
225
                                "fontname" : "Arial",
 
226
                                "numinlets" : 1
 
227
                        }
 
228
 
 
229
                }
 
230
 ,              {
 
231
                        "box" :                         {
 
232
                                "maxclass" : "comment",
 
233
                                "text" : "Here's the number from Arduino's analog input",
 
234
                                "linecount" : 2,
 
235
                                "patching_rect" : [ 153.0, 409.0, 138.0, 34.0 ],
 
236
                                "numoutlets" : 0,
 
237
                                "fontsize" : 12.0,
 
238
                                "id" : "obj-3",
 
239
                                "fontname" : "Arial",
 
240
                                "numinlets" : 1
 
241
                        }
 
242
 
 
243
                }
 
244
 ,              {
 
245
                        "box" :                         {
 
246
                                "maxclass" : "comment",
 
247
                                "text" : "Convert ASCII to symbol",
 
248
                                "patching_rect" : [ 379.0, 378.0, 147.0, 20.0 ],
 
249
                                "numoutlets" : 0,
 
250
                                "fontsize" : 12.0,
 
251
                                "id" : "obj-4",
 
252
                                "fontname" : "Arial",
 
253
                                "numinlets" : 1
 
254
                        }
 
255
 
 
256
                }
 
257
 ,              {
 
258
                        "box" :                         {
 
259
                                "maxclass" : "comment",
 
260
                                "text" : "Convert integer to ASCII",
 
261
                                "patching_rect" : [ 379.0, 355.0, 147.0, 20.0 ],
 
262
                                "numoutlets" : 0,
 
263
                                "fontsize" : 12.0,
 
264
                                "id" : "obj-5",
 
265
                                "fontname" : "Arial",
 
266
                                "numinlets" : 1
 
267
                        }
 
268
 
 
269
                }
 
270
 ,              {
 
271
                        "box" :                         {
 
272
                                "maxclass" : "number",
 
273
                                "patching_rect" : [ 302.0, 414.0, 37.0, 20.0 ],
 
274
                                "numoutlets" : 2,
 
275
                                "fontsize" : 12.0,
 
276
                                "outlettype" : [ "int", "bang" ],
 
277
                                "bgcolor" : [ 0.866667, 0.866667, 0.866667, 1.0 ],
 
278
                                "id" : "obj-6",
 
279
                                "triscale" : 0.9,
 
280
                                "fontname" : "Arial",
 
281
                                "htextcolor" : [ 0.870588, 0.870588, 0.870588, 1.0 ],
 
282
                                "numinlets" : 1
 
283
                        }
 
284
 
 
285
                }
 
286
 ,              {
 
287
                        "box" :                         {
 
288
                                "maxclass" : "newobj",
 
289
                                "text" : "fromsymbol",
 
290
                                "patching_rect" : [ 302.0, 378.0, 74.0, 20.0 ],
 
291
                                "numoutlets" : 1,
 
292
                                "fontsize" : 12.0,
 
293
                                "outlettype" : [ "" ],
 
294
                                "id" : "obj-7",
 
295
                                "fontname" : "Arial",
 
296
                                "color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
 
297
                                "numinlets" : 1
 
298
                        }
 
299
 
 
300
                }
 
301
 ,              {
 
302
                        "box" :                         {
 
303
                                "maxclass" : "newobj",
 
304
                                "text" : "itoa",
 
305
                                "patching_rect" : [ 302.0, 355.0, 46.0, 20.0 ],
 
306
                                "numoutlets" : 1,
 
307
                                "fontsize" : 12.0,
 
308
                                "outlettype" : [ "int" ],
 
309
                                "id" : "obj-8",
 
310
                                "fontname" : "Arial",
 
311
                                "color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
 
312
                                "numinlets" : 3
 
313
                        }
 
314
 
 
315
                }
 
316
 ,              {
 
317
                        "box" :                         {
 
318
                                "maxclass" : "newobj",
 
319
                                "text" : "zl group 4",
 
320
                                "patching_rect" : [ 302.0, 332.0, 64.0, 20.0 ],
 
321
                                "numoutlets" : 2,
 
322
                                "fontsize" : 12.0,
 
323
                                "outlettype" : [ "", "" ],
 
324
                                "id" : "obj-9",
 
325
                                "fontname" : "Arial",
 
326
                                "numinlets" : 2
 
327
                        }
 
328
 
 
329
                }
 
330
 ,              {
 
331
                        "box" :                         {
 
332
                                "maxclass" : "newobj",
 
333
                                "text" : "select 10 13",
 
334
                                "patching_rect" : [ 244.0, 281.0, 77.0, 20.0 ],
 
335
                                "numoutlets" : 3,
 
336
                                "fontsize" : 12.0,
 
337
                                "outlettype" : [ "bang", "bang", "" ],
 
338
                                "id" : "obj-10",
 
339
                                "fontname" : "Arial",
 
340
                                "numinlets" : 1
 
341
                        }
 
342
 
 
343
                }
 
344
 ,              {
 
345
                        "box" :                         {
 
346
                                "maxclass" : "toggle",
 
347
                                "patching_rect" : [ 244.0, 43.0, 15.0, 15.0 ],
 
348
                                "numoutlets" : 1,
 
349
                                "outlettype" : [ "int" ],
 
350
                                "id" : "obj-11",
 
351
                                "numinlets" : 1
 
352
                        }
 
353
 
 
354
                }
 
355
 ,              {
 
356
                        "box" :                         {
 
357
                                "maxclass" : "newobj",
 
358
                                "text" : "qmetro 10",
 
359
                                "patching_rect" : [ 244.0, 80.0, 65.0, 20.0 ],
 
360
                                "numoutlets" : 1,
 
361
                                "fontsize" : 12.0,
 
362
                                "outlettype" : [ "bang" ],
 
363
                                "id" : "obj-12",
 
364
                                "fontname" : "Arial",
 
365
                                "numinlets" : 2
 
366
                        }
 
367
 
 
368
                }
 
369
 ,              {
 
370
                        "box" :                         {
 
371
                                "maxclass" : "message",
 
372
                                "text" : "print",
 
373
                                "patching_rect" : [ 369.0, 179.0, 36.0, 18.0 ],
 
374
                                "numoutlets" : 1,
 
375
                                "fontsize" : 12.0,
 
376
                                "outlettype" : [ "" ],
 
377
                                "id" : "obj-13",
 
378
                                "fontname" : "Arial",
 
379
                                "numinlets" : 2
 
380
                        }
 
381
 
 
382
                }
 
383
 ,              {
 
384
                        "box" :                         {
 
385
                                "maxclass" : "newobj",
 
386
                                "text" : "serial a 9600",
 
387
                                "patching_rect" : [ 244.0, 255.0, 84.0, 20.0 ],
 
388
                                "numoutlets" : 2,
 
389
                                "fontsize" : 12.0,
 
390
                                "outlettype" : [ "int", "" ],
 
391
                                "id" : "obj-14",
 
392
                                "fontname" : "Arial",
 
393
                                "numinlets" : 1
 
394
                        }
 
395
 
 
396
                }
 
397
 ,              {
 
398
                        "box" :                         {
 
399
                                "maxclass" : "comment",
 
400
                                "text" : "Read serial input buffer every 10 milliseconds",
 
401
                                "linecount" : 2,
 
402
                                "patching_rect" : [ 53.0, 72.0, 185.0, 34.0 ],
 
403
                                "numoutlets" : 0,
 
404
                                "fontsize" : 12.0,
 
405
                                "id" : "obj-15",
 
406
                                "fontname" : "Arial",
 
407
                                "numinlets" : 1
 
408
                        }
 
409
 
 
410
                }
 
411
 ,              {
 
412
                        "box" :                         {
 
413
                                "maxclass" : "comment",
 
414
                                "text" : "If you get newline (ASCII 10), send the list. If you get return (ASCII 13) do nothing. Any other value, add to the list",
 
415
                                "linecount" : 3,
 
416
                                "patching_rect" : [ 332.0, 269.0, 320.0, 48.0 ],
 
417
                                "numoutlets" : 0,
 
418
                                "fontsize" : 12.0,
 
419
                                "id" : "obj-16",
 
420
                                "fontname" : "Arial",
 
421
                                "numinlets" : 1
 
422
                        }
 
423
 
 
424
                }
 
425
 ,              {
 
426
                        "box" :                         {
 
427
                                "maxclass" : "comment",
 
428
                                "text" : "Click to open/close serial port and start/stop patch",
 
429
                                "linecount" : 2,
 
430
                                "patching_rect" : [ 271.0, 32.0, 199.0, 34.0 ],
 
431
                                "numoutlets" : 0,
 
432
                                "fontsize" : 12.0,
 
433
                                "id" : "obj-17",
 
434
                                "fontname" : "Arial",
 
435
                                "numinlets" : 1
 
436
                        }
 
437
 
 
438
                }
 
439
 ],
 
440
        "lines" : [             {
 
441
                        "patchline" :                   {
 
442
                                "source" : [ "obj-6", 0 ],
 
443
                                "destination" : [ "obj-1", 0 ],
 
444
                                "hidden" : 0,
 
445
                                "midpoints" : [  ]
 
446
                        }
 
447
 
 
448
                }
 
449
 ,              {
 
450
                        "patchline" :                   {
 
451
                                "source" : [ "obj-7", 0 ],
 
452
                                "destination" : [ "obj-6", 0 ],
 
453
                                "hidden" : 0,
 
454
                                "midpoints" : [  ]
 
455
                        }
 
456
 
 
457
                }
 
458
 ,              {
 
459
                        "patchline" :                   {
 
460
                                "source" : [ "obj-8", 0 ],
 
461
                                "destination" : [ "obj-7", 0 ],
 
462
                                "hidden" : 0,
 
463
                                "midpoints" : [  ]
 
464
                        }
 
465
 
 
466
                }
 
467
 ,              {
 
468
                        "patchline" :                   {
 
469
                                "source" : [ "obj-9", 0 ],
 
470
                                "destination" : [ "obj-8", 0 ],
 
471
                                "hidden" : 0,
 
472
                                "midpoints" : [  ]
 
473
                        }
 
474
 
 
475
                }
 
476
 ,              {
 
477
                        "patchline" :                   {
 
478
                                "source" : [ "obj-10", 0 ],
 
479
                                "destination" : [ "obj-9", 0 ],
 
480
                                "hidden" : 0,
 
481
                                "midpoints" : [ 253.5, 308.0, 311.5, 308.0 ]
 
482
                        }
 
483
 
 
484
                }
 
485
 ,              {
 
486
                        "patchline" :                   {
 
487
                                "source" : [ "obj-10", 2 ],
 
488
                                "destination" : [ "obj-9", 0 ],
 
489
                                "hidden" : 0,
 
490
                                "midpoints" : [ 311.5, 320.0, 311.5, 320.0 ]
 
491
                        }
 
492
 
 
493
                }
 
494
 ,              {
 
495
                        "patchline" :                   {
 
496
                                "source" : [ "obj-14", 0 ],
 
497
                                "destination" : [ "obj-10", 0 ],
 
498
                                "hidden" : 0,
 
499
                                "midpoints" : [  ]
 
500
                        }
 
501
 
 
502
                }
 
503
 ,              {
 
504
                        "patchline" :                   {
 
505
                                "source" : [ "obj-12", 0 ],
 
506
                                "destination" : [ "obj-14", 0 ],
 
507
                                "hidden" : 0,
 
508
                                "midpoints" : [  ]
 
509
                        }
 
510
 
 
511
                }
 
512
 ,              {
 
513
                        "patchline" :                   {
 
514
                                "source" : [ "obj-11", 0 ],
 
515
                                "destination" : [ "obj-12", 0 ],
 
516
                                "hidden" : 0,
 
517
                                "midpoints" : [  ]
 
518
                        }
 
519
 
 
520
                }
 
521
 ,              {
 
522
                        "patchline" :                   {
 
523
                                "source" : [ "obj-13", 0 ],
 
524
                                "destination" : [ "obj-14", 0 ],
 
525
                                "hidden" : 0,
 
526
                                "midpoints" : [ 378.5, 200.5, 253.5, 200.5 ]
 
527
                        }
 
528
 
 
529
                }
 
530
 ,              {
 
531
                        "patchline" :                   {
 
532
                                "source" : [ "obj-19", 0 ],
 
533
                                "destination" : [ "obj-14", 0 ],
 
534
                                "hidden" : 0,
 
535
                                "midpoints" : [ 358.5, 228.5, 253.5, 228.5 ]
 
536
                        }
 
537
 
 
538
                }
 
539
 ,              {
 
540
                        "patchline" :                   {
 
541
                                "source" : [ "obj-21", 0 ],
 
542
                                "destination" : [ "obj-14", 0 ],
 
543
                                "hidden" : 0,
 
544
                                "midpoints" : [ 336.5, 251.5, 253.5, 251.5 ]
 
545
                        }
 
546
 
 
547
                }
 
548
 ,              {
 
549
                        "patchline" :                   {
 
550
                                "source" : [ "obj-30", 0 ],
 
551
                                "destination" : [ "obj-21", 0 ],
 
552
                                "hidden" : 0,
 
553
                                "midpoints" : [  ]
 
554
                        }
 
555
 
 
556
                }
 
557
 ,              {
 
558
                        "patchline" :                   {
 
559
                                "source" : [ "obj-30", 1 ],
 
560
                                "destination" : [ "obj-19", 0 ],
 
561
                                "hidden" : 0,
 
562
                                "midpoints" : [  ]
 
563
                        }
 
564
 
 
565
                }
 
566
 ,              {
 
567
                        "patchline" :                   {
 
568
                                "source" : [ "obj-11", 0 ],
 
569
                                "destination" : [ "obj-30", 0 ],
 
570
                                "hidden" : 0,
 
571
                                "midpoints" : [ 253.0, 71.0, 336.5, 71.0 ]
 
572
                        }
 
573
 
 
574
                }
 
575
 ]
 
576
 }
 
577
 
 
578
 */