Go Down

Topic: Serial port with Processing (Read 20986 times) previous topic - next topic

dragonuv

I have uploaded this sketch into the arduino:

Code: [Select]

void setup() {
   pinMode(2, INPUT);
 Serial.begin(9600);  
}
void loop()
{  
   Serial.println(analogRead(2));
}


and the analog pin is connected to the arduino's 5v, which means in the serial port it has to show only numbers around 1023.

though i have tried to read serial data from processing, it gives me numbers lower than 60. here is the processing code:

Code: [Select]
import processing.serial.*;
Serial myPort;  
int val;    
void setup()
{
 String portName = Serial.list()[1]; //com3, same as arduino
 myPort = new Serial(this, portName, 9600);
}

void draw()
{
 if ( myPort.available() > 0)  // If data is available,
 {  
   val = myPort.read();         // read it and store it in val
   println(val);
 }
}


what can cause this problem?

Grumpy_Mike

Quote
what can cause this problem?


When you write from the arduino you are sending several ASCII bytes. When you read from processing you are only reading one byte.

See:- http://www.arduino.cc/playground/Interfacing/Processing

dragonuv

I'm having a problem with this line:
import cc.arduino.*;
the code says "The package "cc" does not exist. You might be missing a library"

although I have downloaded the library for firmata v1 into the processing libraries folder

dragonuv

anyone knows what is the problem now ?

zdroshnya

You must have the hierarchy:
<Processing sketchbook>/libraries/<library name>/library/<library name>.jar

best

dragonuv

thanks very much, that helped.

Aniss

Don't know if you solved your original problem, but as Grumpy said it's because you're sending 2 bytes at a time when you send an int value...

Here is a method of sending and receiving int values that works for me:

Arduino:
Code: [Select]
Serial.print(0xff, BYTE); // Sync byte
Serial.print((val >> 8) & 0xff, BYTE);
Serial.print(val & 0xff, BYTE);


Processing:
Code: [Select]
while (port.available() >= 3) {
   if (port.read() == 0xff)
     val = (port.read() << 8) | (port.read());
}

HM

zdroshnya, thank you very much, this is the hint I needed.
The problem of the normal arduino installation is the name of the file.
It is written "Arduino.jar" and so, I've got every time this error message.
I have renamed the file in "arduino.jar" and the library works well.
The guys of the arduino project should corred this in the "processing-arduino-0017.zip" file.

thanks a lot

Helmut

Go Up