| 1 |
/* |
| 2 |
Graph |
| 3 |
|
| 4 |
A simple example of communication from the Arduino board to the computer: The |
| 5 |
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 a USB cable. Bytes |
| 8 |
are sent one after another (serially) from the Arduino to the computer. |
| 9 |
|
| 10 |
You can use the Arduino Serial Monitor to view the sent data, or it can be |
| 11 |
read by Processing, PD, Max/MSP, or any other program capable of reading data |
| 12 |
from a serial port. The Processing code below graphs the data received so you |
| 13 |
can see the value of the analog input changing over time. |
| 14 |
|
| 15 |
The circuit: |
| 16 |
- any analog input sensor attached to analog in pin 0 |
| 17 |
|
| 18 |
created 2006 |
| 19 |
by David A. Mellis |
| 20 |
modified 9 Apr 2012 |
| 21 |
by Tom Igoe and Scott Fitzgerald |
| 22 |
|
| 23 |
This example code is in the public domain. |
| 24 |
|
| 25 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Graph |
| 26 |
*/ |
| 27 |
|
| 28 |
void setup() { |
| 29 |
// initialize the serial communication: |
| 30 |
Serial.begin(9600); |
| 31 |
} |
| 32 |
|
| 33 |
void loop() { |
| 34 |
// send the value of analog input 0: |
| 35 |
Serial.println(analogRead(A0)); |
| 36 |
// wait a bit for the analog-to-digital converter to stabilize after the last |
| 37 |
// reading: |
| 38 |
delay(2); |
| 39 |
} |
| 40 |
|
| 41 |
/* Processing code for this example |
| 42 |
|
| 43 |
// Graphing sketch |
| 44 |
|
| 45 |
// This program takes ASCII-encoded strings from the serial port at 9600 baud |
| 46 |
// and graphs them. It expects values in the range 0 to 1023, followed by a |
| 47 |
// newline, or newline and carriage return |
| 48 |
|
| 49 |
// created 20 Apr 2005 |
| 50 |
// updated 24 Nov 2015 |
| 51 |
// by Tom Igoe |
| 52 |
// This example code is in the public domain. |
| 53 |
|
| 54 |
import processing.serial.*; |
| 55 |
|
| 56 |
Serial myPort; // The serial port |
| 57 |
int xPos = 1; // horizontal position of the graph |
| 58 |
float inByte = 0; |
| 59 |
|
| 60 |
void setup () { |
| 61 |
// set the window size: |
| 62 |
size(400, 300); |
| 63 |
|
| 64 |
// List all the available serial ports |
| 65 |
// if using Processing 2.1 or later, use Serial.printArray() |
| 66 |
println(Serial.list()); |
| 67 |
|
| 68 |
// I know that the first port in the serial list on my Mac is always my |
| 69 |
// Arduino, so I open Serial.list()[0]. |
| 70 |
// Open whatever port is the one you're using. |
| 71 |
myPort = new Serial(this, Serial.list()[0], 9600); |
| 72 |
|
| 73 |
// don't generate a serialEvent() unless you get a newline character: |
| 74 |
myPort.bufferUntil('\n'); |
| 75 |
|
| 76 |
// set initial background: |
| 77 |
background(0); |
| 78 |
} |
| 79 |
|
| 80 |
void draw () { |
| 81 |
// draw the line: |
| 82 |
stroke(127, 34, 255); |
| 83 |
line(xPos, height, xPos, height - inByte); |
| 84 |
|
| 85 |
// at the edge of the screen, go back to the beginning: |
| 86 |
if (xPos >= width) { |
| 87 |
xPos = 0; |
| 88 |
background(0); |
| 89 |
} else { |
| 90 |
// increment the horizontal position: |
| 91 |
xPos++; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void serialEvent (Serial myPort) { |
| 96 |
// get the ASCII string: |
| 97 |
String inString = myPort.readStringUntil('\n'); |
| 98 |
|
| 99 |
if (inString != null) { |
| 100 |
// trim off any whitespace: |
| 101 |
inString = trim(inString); |
| 102 |
// convert to an int and map to the screen height: |
| 103 |
inByte = float(inString); |
| 104 |
println(inByte); |
| 105 |
inByte = map(inByte, 0, 1023, 0, height); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
*/ |
| 110 |
|
| 111 |
/* Max/MSP v5 patch for this example |
| 112 |
|
| 113 |
----------begin_max5_patcher---------- |
| 114 |
1591.3oc0YszbaaCD9r7uBL5RalQUAO3CvdyS5zVenWZxs5NcfHgjPCIfJIT |
| 115 |
RTxj+6AOHkoTDooroUs0AQPR73a+1cwtK3WtZxzEpOwqlB9YveAlL4KWMYh6 |
|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XOLgv1n0d2iEsCxR.eQsNOZ4FGF7whofgfI3kES |
| 148 |
1kCeOX5L2rifbdu0A9ae2X.V33B1Z+.Bj1FrP5iFrCYCG5EUWSG.hhunHJd. |
| 149 |
HJ5hhnng3h9HPj4lud02.1bxGw. |
| 150 |
-----------end_max5_patcher----------- |
| 151 |
|
| 152 |
*/ |