Serial problems between Arduino and Processing...

Hello,

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...

Hi Pixel, It looks like you are not detecting the NEWLINE in Processing. Why not post your Arduino and Processing code?

Here is the code

Arduino:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(analogRead(3));
  delay(20);
}

and Processing:

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.

Strange though it seems to work more or less reliably now though every few runs it will get jumbled, strange strange.

Oh I think I may have sorted it out.

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 think, maybe. Thanks for the help!

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.

Anyway, if you have it working than not to worry.

Have fun!

Yeah very strange. It also seems that having the serial monitor open in Arduino causes it to crash.

Thanks for the help though, it tweaked something thats making it run.