Go Down

Topic: sending integers from arduino to processing (Read 2705 times) previous topic - next topic


So I'm trying to send some ints from arduino to procesing. I've used the serial.write() and printInteger() functions and the serial monitor shows that the data is good. The problem comes in processing.

I've tried getting the data as a string, or getting it byte by byte. Reading the values as strings gives me some errors in the data (it tends to drop a bit from time to time). I can get it by reading each byte and then reconstructing the int from it's component bytes. But that only works if I know how big the int coming through is going to be. The problem is that I'm never sure just how big the value coming in is going to be.

Can I get some help on a simple querying system? I'd like to send the arduino a character, and have it send back a packet of 4 or 5 integers of varying sizes. I have the arduino setup to receive a byte and write the values to the serial port, but I can't get good data in processing. Thanks!


Have you tried the Graph or VirtualColorMixer examples in Arduino 0008?  They do something similar to what you're trying.  Do you have the same problems with those?


Well I'm using those as my examples, but when I try to add the ability to add a query the arduino for specific readings (ie, send an A to get the analog1 value, B for analog2, etc) everything falls apart.
buff = buff.substring(0, buff.length()-1);
usually throws an index error so I checked the length on it before calling it. Then I get an error from:
int val = Integer.parseInt(buff)/4;

The only thing I've changed from the graph tutorial is that I add a port.write(67) to the draw just after the lines are drawn.


Hmm... this often happens right when the Processing sketch starts, presumably because it starts reading data in the middle of a line, when there's no characters for it.  You might try catching the exception thrown by the parseInt() call and just setting val to some default.  If you're also having problems after the sketch has been running a while, let me know, because that would indicate that data is going missing somewhere.


Well I added some extra checks before writing the incoming byte into the character string. I think the problem was that a lf or cr or null string was getting through and screwing up the transformation of the string to a number. Here's the modified code:

void serialEvent(int serial)
 if (serial != NEWLINE && serial != 13) {
   // Store all the characters on the line.
   buff += char(serial);
//    println(serial);
 } else {if(buff.length()>0){
   // The end of each line is marked by two characters, a carriage
   // return and a newline.  We're here because we've gotten a newline,
   // but we still need to strip off the carriage return.
   println("buffer length is " +buff.length());
   buff = buff.substring(0, buff.length()-1);

   // Parse the String into an integer.  We divide by 4 because
   // analog inputs go from 0 to 1023 while colors in Processing
   // only go from 0 to 255.
   float val = parseFloat(buff)/1;
 //  time = (val);

   // Clear the value of "buff"
   buff = "";

   // Shift over the existing values to make room for the new one.
   for (int i = 0; i < 63; i++)
     values = values[i + 1];

   // Add the received value to the array.
   values[63] = val;

Go Up