Arduino and Processing
in
Integration and Hardware
•
1 year ago
I'm a new Arduino user, as well as new to Processing. I'm working on an example sketch named Graph, which uses both languages. The sketch involves loading and running a segment using Arduino as follows:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(10);
}
This segment is compiled and uploaded to an Arduino Uno. Next, a segment is written in Processing as follows:
import processing.serial.*;
Serial myPort;
int xPos = 1;
void setup () {
size(400, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0);
}
void draw () {
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
xPos++;
}
}
}
This is then compiled and uploaded to the Arduino, which is already running the Arduino segment.
The problem is that this segment will not compile correctly. The error message I get is
"Error inside Serial.<init>()"
When the error appears, the line beginning
myPort=new Serial(this,.......
is highlighted.
Can someone offer suggestions on what I'm doing wrong?
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(10);
}
This segment is compiled and uploaded to an Arduino Uno. Next, a segment is written in Processing as follows:
import processing.serial.*;
Serial myPort;
int xPos = 1;
void setup () {
size(400, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0);
}
void draw () {
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
xPos++;
}
}
}
This is then compiled and uploaded to the Arduino, which is already running the Arduino segment.
The problem is that this segment will not compile correctly. The error message I get is
"Error inside Serial.<init>()"
When the error appears, the line beginning
myPort=new Serial(this,.......
is highlighted.
Can someone offer suggestions on what I'm doing wrong?
1