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
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.
17
Any analog input sensor is attached to analog in pin 0.
19
http://www.arduino.cc/en/Tutorial/Graph
24
by Tom Igoe and Scott Fitzgerald
26
http://www.arduino.cc/en/Tutorial/Graph
30
// initialize the serial communication:
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:
42
/* Processing code for this example
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
51
// Created 20 Apr 2005
52
// Updated 18 Jan 2008
55
import processing.serial.*;
57
Serial myPort; // The serial port
58
int xPos = 1; // horizontal position of the graph
61
// set the window size:
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:
76
// everything happens in the serialEvent()
79
void serialEvent (Serial myPort) {
80
// get the ASCII string:
81
String inString = myPort.readStringUntil('\n');
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);
92
line(xPos, height, xPos, height - inByte);
94
// at the edge of the screen, go back to the beginning:
100
// increment the horizontal position:
108
/* Max/MSP v5 patch for this example
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",
115
"patching_rect" : [ 479.0, 6.0, 344.0, 144.0 ],
119
"fontname" : "Arial",
126
"maxclass" : "newobj",
127
"text" : "select 0 1",
128
"patching_rect" : [ 327.0, 80.0, 62.0, 20.0 ],
131
"outlettype" : [ "bang", "bang", "" ],
133
"fontname" : "Arial",
140
"maxclass" : "comment",
141
"text" : "click here to close the serial port",
142
"patching_rect" : [ 412.0, 231.0, 206.0, 20.0 ],
146
"fontname" : "Arial",
153
"maxclass" : "comment",
154
"text" : "click here to open the serial port",
155
"patching_rect" : [ 412.0, 205.0, 206.0, 20.0 ],
159
"fontname" : "Arial",
166
"maxclass" : "message",
168
"patching_rect" : [ 327.0, 231.0, 39.0, 18.0 ],
171
"outlettype" : [ "" ],
173
"fontname" : "Arial",
180
"maxclass" : "message",
182
"patching_rect" : [ 349.0, 205.0, 41.0, 18.0 ],
185
"outlettype" : [ "" ],
187
"fontname" : "Arial",
194
"maxclass" : "multislider",
195
"candicane7" : [ 0.878431, 0.243137, 0.145098, 1.0 ],
196
"patching_rect" : [ 302.0, 450.0, 246.0, 167.0 ],
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 ],
205
"candicane6" : [ 0.733333, 0.035294, 0.788235, 1.0 ],
207
"bgcolor" : [ 0.231373, 0.713726, 1.0, 1.0 ],
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 ],
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 ],
225
"fontname" : "Arial",
232
"maxclass" : "comment",
233
"text" : "Here's the number from Arduino's analog input",
235
"patching_rect" : [ 153.0, 409.0, 138.0, 34.0 ],
239
"fontname" : "Arial",
246
"maxclass" : "comment",
247
"text" : "Convert ASCII to symbol",
248
"patching_rect" : [ 379.0, 378.0, 147.0, 20.0 ],
252
"fontname" : "Arial",
259
"maxclass" : "comment",
260
"text" : "Convert integer to ASCII",
261
"patching_rect" : [ 379.0, 355.0, 147.0, 20.0 ],
265
"fontname" : "Arial",
272
"maxclass" : "number",
273
"patching_rect" : [ 302.0, 414.0, 37.0, 20.0 ],
276
"outlettype" : [ "int", "bang" ],
277
"bgcolor" : [ 0.866667, 0.866667, 0.866667, 1.0 ],
280
"fontname" : "Arial",
281
"htextcolor" : [ 0.870588, 0.870588, 0.870588, 1.0 ],
288
"maxclass" : "newobj",
289
"text" : "fromsymbol",
290
"patching_rect" : [ 302.0, 378.0, 74.0, 20.0 ],
293
"outlettype" : [ "" ],
295
"fontname" : "Arial",
296
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
303
"maxclass" : "newobj",
305
"patching_rect" : [ 302.0, 355.0, 46.0, 20.0 ],
308
"outlettype" : [ "int" ],
310
"fontname" : "Arial",
311
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
318
"maxclass" : "newobj",
319
"text" : "zl group 4",
320
"patching_rect" : [ 302.0, 332.0, 64.0, 20.0 ],
323
"outlettype" : [ "", "" ],
325
"fontname" : "Arial",
332
"maxclass" : "newobj",
333
"text" : "select 10 13",
334
"patching_rect" : [ 244.0, 281.0, 77.0, 20.0 ],
337
"outlettype" : [ "bang", "bang", "" ],
339
"fontname" : "Arial",
346
"maxclass" : "toggle",
347
"patching_rect" : [ 244.0, 43.0, 15.0, 15.0 ],
349
"outlettype" : [ "int" ],
357
"maxclass" : "newobj",
358
"text" : "qmetro 10",
359
"patching_rect" : [ 244.0, 80.0, 65.0, 20.0 ],
362
"outlettype" : [ "bang" ],
364
"fontname" : "Arial",
371
"maxclass" : "message",
373
"patching_rect" : [ 369.0, 179.0, 36.0, 18.0 ],
376
"outlettype" : [ "" ],
378
"fontname" : "Arial",
385
"maxclass" : "newobj",
386
"text" : "serial a 9600",
387
"patching_rect" : [ 244.0, 255.0, 84.0, 20.0 ],
390
"outlettype" : [ "int", "" ],
392
"fontname" : "Arial",
399
"maxclass" : "comment",
400
"text" : "Read serial input buffer every 10 milliseconds",
402
"patching_rect" : [ 53.0, 72.0, 185.0, 34.0 ],
406
"fontname" : "Arial",
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",
416
"patching_rect" : [ 332.0, 269.0, 320.0, 48.0 ],
420
"fontname" : "Arial",
427
"maxclass" : "comment",
428
"text" : "Click to open/close serial port and start/stop patch",
430
"patching_rect" : [ 271.0, 32.0, 199.0, 34.0 ],
434
"fontname" : "Arial",
442
"source" : [ "obj-6", 0 ],
443
"destination" : [ "obj-1", 0 ],
451
"source" : [ "obj-7", 0 ],
452
"destination" : [ "obj-6", 0 ],
460
"source" : [ "obj-8", 0 ],
461
"destination" : [ "obj-7", 0 ],
469
"source" : [ "obj-9", 0 ],
470
"destination" : [ "obj-8", 0 ],
478
"source" : [ "obj-10", 0 ],
479
"destination" : [ "obj-9", 0 ],
481
"midpoints" : [ 253.5, 308.0, 311.5, 308.0 ]
487
"source" : [ "obj-10", 2 ],
488
"destination" : [ "obj-9", 0 ],
490
"midpoints" : [ 311.5, 320.0, 311.5, 320.0 ]
496
"source" : [ "obj-14", 0 ],
497
"destination" : [ "obj-10", 0 ],
505
"source" : [ "obj-12", 0 ],
506
"destination" : [ "obj-14", 0 ],
514
"source" : [ "obj-11", 0 ],
515
"destination" : [ "obj-12", 0 ],
523
"source" : [ "obj-13", 0 ],
524
"destination" : [ "obj-14", 0 ],
526
"midpoints" : [ 378.5, 200.5, 253.5, 200.5 ]
532
"source" : [ "obj-19", 0 ],
533
"destination" : [ "obj-14", 0 ],
535
"midpoints" : [ 358.5, 228.5, 253.5, 228.5 ]
541
"source" : [ "obj-21", 0 ],
542
"destination" : [ "obj-14", 0 ],
544
"midpoints" : [ 336.5, 251.5, 253.5, 251.5 ]
550
"source" : [ "obj-30", 0 ],
551
"destination" : [ "obj-21", 0 ],
559
"source" : [ "obj-30", 1 ],
560
"destination" : [ "obj-19", 0 ],
568
"source" : [ "obj-11", 0 ],
569
"destination" : [ "obj-30", 0 ],
571
"midpoints" : [ 253.0, 71.0, 336.5, 71.0 ]