I am new to the arduino and processing and I am having a problem. I am trying to send an analog value from a POT on the arduino to processing.
When I monitor the arduino the value works fine and spits out a range from 0 - 690 with a line break after each number. When I run the processing sketch, however, it gets all jumbled and the sketch crashes because it can not parse the mix of letters.
When I say jumbled I mean that the out put will normally be:
0
10
20
20
24
26
But when the processing sketch runs it looks more like:
0102
024 2025
2
020
I am taking in the serial info as characters into a string and parsing out the Integer when it hits a line break like in the examples for the color mixer from the arduino site.
Any ideas as to why this might be happening? I thought maybe the FTDI Driver or something like that, I have v2.10 installed. Perhaps a java library? I am really clueless.
I am running all the most up to date versions of the software on an Arduino Duemilanove on an Intel MacBook running OS X 10.4.11
Any help would be appreciated, I have hit a wall with what I know here...
import processing.serial.*;
Serial port;
String pot = ""; // Data received from the serial port
int val;
int NEWLINE = 10;
void setup()
{
size(200, 200);
String portName = Serial.list()[0];
port = new Serial(this, portName, 9600);
}
void draw()
{
if ( port.available() > 0) { // If data is available,
serialEvent(port.read());
}
print(val);
print(" / ");
}
void serialEvent(int serial)
{
if(serial != NEWLINE){
pot += char(serial);
} else {
pot = pot.substring(0, pot.length()-1);
val = Integer.parseInt(pot);
pot = "";
}
}
I just pulled from some examples and tried to simplify them. Thoughts?
here is your code with print statements added to display the pot value when a newline is received. I have commented out the print of the int value for testing so you should only see the incoming string
You may also want to increase the delay in the Arduino sketch to 100ms so it doesn't flood with print statements
import processing.serial.*;
Serial port;
String pot = ""; // Data received from the serial port
int val;
int NEWLINE = 10;
void setup()
{
size(200, 200);
String portName = Serial.list()[0];
port = new Serial(this, portName, 9600);
}
void draw()
{
if ( port.available() > 0) { // If data is available,
serialEvent(port.read());
}
// print(val);
// print(" / ");
}
void serialEvent(int serial)
{
if(serial != NEWLINE){
pot += char(serial);
} else {
print("got ");
print(pot);
pot = pot.substring(0, pot.length()-1);
val = Integer.parseInt(pot);
print("val = ");
println(val);
pot = "";
}
}
This is basically the same thing though. The problem was not in printing the values.
The problem is that when the processing sketch runs all of the data being sent from the serial port gets mis formatted. If you watch the Serial Monitor from arduino you can see it gets all shifted.
Moving the print statement still gives the same problems.
It seems that the error was because I initialized the string as "" so there was no length to it. When the serial event ran the first time it sometimes has no data for some reason and its length is 0, the function then tries to take off the carriage return by subtracting one and is left with -1 which causes an error.
I wasn't proposing that adding the print statements would fix the problem, it was suggested to help you see what was coming in on the serial port.
I don't understand why initializing the string as null would cause the problem, it looks like the string is only used when at least one character has been appended.