We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Processing<->Arduino: strange numbers
Page Index Toggle Pages: 1
Processing<->Arduino: strange numbers (Read 273 times)
Processing<->Arduino: strange numbers
Mar 25th, 2009, 6:04pm
 
Hello,

i am working on an g4 mac, 10.5.3.
through arduino "analog in" pin i am recieving data, which arrives well in the serial-monitor of the arduino ide (numbers between 0-1024).
then i want to get it to processing.
1. the processing lib doesn^t work, maybe because of my old mac?
2. Now, when i use the serial lib, i first recieve the data from arduino like this

void draw(){

byte[] inBuffer = new byte[7];
 
 while (myPort.available() > 0) {
   
   inBuffer = myPort.readBytes();
   myPort.readBytes(inBuffer);

   if (inBuffer != null) {
     String myString = new String(inBuffer);

Then i have strings of different length, which I don§t manage to convert to integers. When e.g. using int(String) or transfering each singel part of the string to a char c1 = myString.charAt(i);   and then to int its simplz not working. It becomes a strange row of numbers:
10
48
13
10
48
13
10
48
13
10
48
13

SOmeone has a idea?
Re: Processing<->Arduino: strange numbers
Reply #1 - Mar 25th, 2009, 8:10pm
 
you are sending your values as string (which is what the serial monitor assumes) but processing reads them as bytes.

one way to read values between 0-1023 is to read them as string and convert that to int:

import processing.serial.*;

Serial myPort;
int lf = 10;
String myString;

void setup ()
{
   println( Serial.list() );
   myPort = new Serial( this, Serial.list()[1], 9600 );
}

void draw ()
{
   while (myPort.available() > 0)
   {
       myString = myPort.readStringUntil(lf);  // assumes you are sending them with println()
       if (myString != null) {
           println(int(myString.trim()));
       }
   }
}

F
Page Index Toggle Pages: 1