Skip to content

Commit 8e6a30e

Browse files
committed
Merge pull request arduino#4220 from agdl/communicationExamples
Fixed processing code for Processing 3
2 parents 89a36ca + 3229e42 commit 8e6a30e

File tree

2 files changed

+114
-113
lines changed

2 files changed

+114
-113
lines changed

build/shared/examples/04.Communication/Graph/Graph.ino

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -44,71 +44,71 @@ void loop() {
4444
// Graphing sketch
4545
4646
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-
// This example code is in the public domain.
55-
56-
import processing.serial.*;
57-
58-
Serial myPort; // The serial port
59-
int xPos = 1; // horizontal position of the graph
60-
61-
void setup () {
62-
// set the window size:
63-
size(400, 300);
64-
65-
// List all the available serial ports
66-
// if using Processing 2.1 or later, use Serial.printArray()
67-
println(Serial.list());
68-
69-
// I know that the first port in the serial list on my mac
70-
// is always my Arduino, so I open Serial.list()[0].
71-
// Open whatever port is the one you're using.
72-
myPort = new Serial(this, Serial.list()[0], 9600);
73-
74-
// don't generate a serialEvent() unless you get a newline character:
75-
myPort.bufferUntil('\n');
76-
77-
// set inital background:
78-
background(0);
79-
}
80-
void draw () {
81-
// everything happens in the serialEvent()
82-
}
83-
84-
void serialEvent (Serial myPort) {
85-
// get the ASCII string:
86-
String inString = myPort.readStringUntil('\n');
87-
88-
if (inString != null) {
89-
// trim off any whitespace:
90-
inString = trim(inString);
91-
// convert to an int and map to the screen height:
92-
float inByte = float(inString);
93-
inByte = map(inByte, 0, 1023, 0, height);
94-
95-
// draw the line:
96-
stroke(127,34,255);
97-
line(xPos, height, xPos, height - inByte);
98-
99-
// at the edge of the screen, go back to the beginning:
100-
if (xPos >= width) {
101-
xPos = 0;
102-
background(0);
103-
}
104-
else {
105-
// increment the horizontal position:
106-
xPos++;
107-
}
108-
}
109-
}
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
11050
111-
*/
51+
// Created 20 Apr 2005
52+
// Updated 24 Nov 2015
53+
// by Tom Igoe
54+
// This example code is in the public domain.
55+
56+
import processing.serial.*;
57+
58+
Serial myPort; // The serial port
59+
int xPos = 1; // horizontal position of the graph
60+
float inByte = 0;
61+
62+
void setup () {
63+
// set the window size:
64+
size(400, 300);
65+
66+
// List all the available serial ports
67+
// if using Processing 2.1 or later, use Serial.printArray()
68+
println(Serial.list());
69+
70+
// I know that the first port in the serial list on my mac
71+
// is always my Arduino, so I open Serial.list()[0].
72+
// Open whatever port is the one you're using.
73+
myPort = new Serial(this, Serial.list()[0], 9600);
74+
75+
// don't generate a serialEvent() unless you get a newline character:
76+
myPort.bufferUntil('\n');
77+
78+
// set inital background:
79+
background(0);
80+
}
81+
void draw () {
82+
// draw the line:
83+
stroke(127, 34, 255);
84+
line(xPos, height, xPos, height - inByte);
85+
86+
// at the edge of the screen, go back to the beginning:
87+
if (xPos >= width) {
88+
xPos = 0;
89+
background(0);
90+
} else {
91+
// increment the horizontal position:
92+
xPos++;
93+
}
94+
}
95+
96+
97+
void serialEvent (Serial myPort) {
98+
// get the ASCII string:
99+
String inString = myPort.readStringUntil('\n');
100+
101+
if (inString != null) {
102+
// trim off any whitespace:
103+
inString = trim(inString);
104+
// convert to an int and map to the screen height:
105+
inByte = float(inString);
106+
println(inByte);
107+
inByte = map(inByte, 0, 1023, 0, height);
108+
}
109+
}
110+
111+
*/
112112

113113
/* Max/MSP v5 patch for this example
114114
----------begin_max5_patcher----------

build/shared/examples/04.Communication/VirtualColorMixer/VirtualColorMixer.ino

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,55 +36,56 @@ void loop() {
3636
3737
// This example code is in the public domain.
3838
39-
import processing.serial.*;
40-
41-
float redValue = 0; // red value
42-
float greenValue = 0; // green value
43-
float blueValue = 0; // blue value
44-
45-
Serial myPort;
46-
47-
void setup() {
48-
size(200, 200);
49-
50-
// List all the available serial ports
51-
// if using Processing 2.1 or later, use Serial.printArray()
52-
println(Serial.list());
53-
54-
// I know that the first port in the serial list on my mac
55-
// is always my Arduino, so I open Serial.list()[0].
56-
// Open whatever port is the one you're using.
57-
myPort = new Serial(this, Serial.list()[0], 9600);
58-
// don't generate a serialEvent() unless you get a newline character:
59-
myPort.bufferUntil('\n');
60-
}
61-
62-
void draw() {
63-
// set the background color with the color values:
64-
background(redValue, greenValue, blueValue);
65-
}
66-
67-
void serialEvent(Serial myPort) {
68-
// get the ASCII string:
69-
String inString = myPort.readStringUntil('\n');
70-
71-
if (inString != null) {
72-
// trim off any whitespace:
73-
inString = trim(inString);
74-
// split the string on the commas and convert the
75-
// resulting substrings into an integer array:
76-
float[] colors = float(split(inString, ","));
77-
// if the array has at least three elements, you know
78-
// you got the whole thing. Put the numbers in the
79-
// color variables:
80-
if (colors.length >=3) {
81-
// map them to the range 0-255:
82-
redValue = map(colors[0], 0, 1023, 0, 255);
83-
greenValue = map(colors[1], 0, 1023, 0, 255);
84-
blueValue = map(colors[2], 0, 1023, 0, 255);
85-
}
86-
}
87-
}
39+
import processing.serial.*;
40+
41+
float redValue = 0; // red value
42+
float greenValue = 0; // green value
43+
float blueValue = 0; // blue value
44+
45+
Serial myPort;
46+
47+
void setup() {
48+
size(200, 200);
49+
50+
// List all the available serial ports
51+
// if using Processing 2.1 or later, use Serial.printArray()
52+
println(Serial.list());
53+
54+
// I know that the first port in the serial list on my mac
55+
// is always my Arduino, so I open Serial.list()[0].
56+
// Open whatever port is the one you're using.
57+
myPort = new Serial(this, Serial.list()[0], 9600);
58+
// don't generate a serialEvent() unless you get a newline character:
59+
myPort.bufferUntil('\n');
60+
}
61+
62+
void draw() {
63+
// set the background color with the color values:
64+
background(redValue, greenValue, blueValue);
65+
}
66+
67+
void serialEvent(Serial myPort) {
68+
// get the ASCII string:
69+
String inString = myPort.readStringUntil('\n');
70+
71+
if (inString != null) {
72+
// trim off any whitespace:
73+
inString = trim(inString);
74+
// split the string on the commas and convert the
75+
// resulting substrings into an integer array:
76+
float[] colors = float(split(inString, ","));
77+
// if the array has at least three elements, you know
78+
// you got the whole thing. Put the numbers in the
79+
// color variables:
80+
if (colors.length >=3) {
81+
// map them to the range 0-255:
82+
redValue = map(colors[0], 0, 1023, 0, 255);
83+
greenValue = map(colors[1], 0, 1023, 0, 255);
84+
blueValue = map(colors[2], 0, 1023, 0, 255);
85+
}
86+
}
87+
}
88+
8889
*/
8990

9091
/* Max/MSP patch for this example

0 commit comments

Comments
 (0)