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 & HelpElectronics,  Serial Library › Read arduino serial variable
Page Index Toggle Pages: 1
Read arduino serial variable (Read 5390 times)
Read arduino serial variable
Jan 27th, 2008, 2:25pm
 
Hi,im a newbie of processing, i have make a arduino project for read ultrasound value and run correctly. But processing do not read the date...
i use mac with leopard and usb/serial.
thanks

the  arduino code :
Code:

//Output
int statusLed = 13;
int ledPin = 12;
//intput
float ez1Analog = 5;

void setup() {
pinMode(statusLed,OUTPUT);
pinMode(ez1Analog,INPUT);
pinMode(ledPin, OUTPUT);
beginSerial(9600);
}

void loop() {
int val = analogRead(ez1Analog);
if (val > 0) {
val = (val/2);
val = (val/0.39);
Serial.println(val);
Serial.print(val);

}
if (val < 100 & val > 30) {
digitalWrite(ledPin, HIGH);

}
else {
digitalWrite(ledPin, LOW);

}

blinkLed(statusLed,val);
}

void blinkLed(int pin, int ms) {
digitalWrite(pin,LOW);
digitalWrite(pin,HIGH);
delay(ms);
digitalWrite(pin,LOW);
delay(ms);
}


and Processing code

Code:



import processing.serial.*;
String portname="/dev/tty.usbserial-A1001Njy";
Serial port; // Create object from Serial class
int val; // Data received from the serial port

void setup()
{

// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, portname, 9600);
}

void draw()
{

val = port.read(); // read it and store it in val

println(val);
}



Re: Read arduino serial variable
Reply #1 - May 29th, 2008, 2:40am
 
Have u tried reseting the arduino just before starting the processing program?
Re: Read arduino serial variable
Reply #2 - Jun 6th, 2008, 6:52am
 
One thing I see, you are reading the val into an integer and then dividing by .39, this won't get you what you want.
Re: Read arduino serial variable
Reply #3 - Jun 15th, 2008, 5:25am
 
i just posted this somewhere else, but i guess it could help to post again?

I use
http://www.arduino.cc/playground/Interfacing/Firmata
to get values via serial port - don't forget to adjust the serial port values in the Firmata processing scripts to get the right one

I just posted:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Exhibition;acti on=display;num=1213500039

maybe it'll help? =] - lemme know if you have questions

best of luck,
-Jeff
Re: Read arduino serial variable
Reply #4 - Jun 15th, 2008, 4:24pm
 
thanks, for use the firmata i need to place folder "firmata" in my project?
and where i can found the rules for this library ?
mau
Re: Read arduino serial variable
Reply #5 - Jun 15th, 2008, 9:32pm
 
Quoted from http://www.arduino.cc/playground/Interfacing/Processing

//Start Quote:

Arduino Library for Processing (and Firmata)

There's a Processing library that works with Firmata (a standard Arduino firmware that works with lots of software).

Download

processing-arduino.zip (http://www.arduino.cc/playground/uploads/Interfacing/processing-arduino.zip)

Instructions

  1. Unzip the library and copy the "arduino" folder into the "libraries" sub-folder of the Processing application directory.
  2. Open the firmware (in arduino/firmware) in Arduino and upload it to the Arduino board.
  3. Configure Processing for serial: http://processing.org/reference/libraries/serial/
  4. In Processing, open one of the examples that comes with with the Arduino library.
  5. Edit the example code to select the correct serial port.
  6. Run the example.

//End Quote


I'd say the most tricky part is step #5 where you pick the correct COM port - basically, you pick the same COM port you use to program your arduino

under the setup() in every example is a line that reads:
 arduino = new Arduino(this, Arduino.list()[X], 57600);

Where X is a number - unfortunately, this is kind of confusing because the X isn't just COM port number X, but is based on the list Arduino.list() - to figure out the mapping, run this in processing:

//START CODE

import processing.serial.*;
import cc.arduino.*;
println(Arduino.list());

//END CODE

in the CLI on the bottom, you should see something like:



Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "COM1"
[1] "COM4"
[2] "COM5"
[3] "COM6"
[4] "COM7"
[5] "COM8"
[6] "COM9"
[7] "COM10"
[8] "COM11"
[9] "COM12"

the COM port you want should correspond to the array index and that is your X value that you fill in the
 arduino = new Arduino(this, Arduino.list()[X], 57600);
line

i hope that helps =]

as for the "rules" for the library - i'd read:
http://www.arduino.cc/playground/Interfacing/Processing
which is basically the article i used - there's a really good reference section there too
Re: Read arduino serial variable
Reply #6 - Jun 15th, 2008, 10:04pm
 
thanks, ok, run the port is the same in arduino and processing, but my result is this :

http://www.ma-mu.org/file/serial.png

in processing the data is wrong: the same number 49 50 51 13 ...

i don't understanding why, maybe is the usb/serial no good ?

thanks
Re: Read arduino serial variable
Reply #7 - Jun 15th, 2008, 11:52pm
 
perhaps I didn't explain it well enough =/

The way I'm introducing is really different from the previous way you were doing it (I find this easier because I'm more of a software person than a hardware person)

The Arduino should have the firmata sketch loaded. To do this, download:
http://www.arduino.cc/playground/uploads/Interfacing/processing-arduino. zip
and unextract it.

In the arduino IDE, open the file in the path arduino/firmware/Standard_Firmata/Standard_Firmata.pde

Now upload this sketch to your Arduino board. Basically, the Firmata sketch allows processing to access the Arduino board over the serial bus and query the digital/analog sensors (among another things).

-That's it for the Arduino programming - nothing left to do with the Arduino.

Now on the processing side, try this sketch (I just tested it and it seems to work):

Code:

import processing.serial.*;
import cc.arduino.*;

int analogPin = 5;
int analogValue = 0;

Arduino arduino;

void setup(){
println(arduino.list());
arduino = new Arduino(this, Arduino.list()[1], 57600);
}

void draw(){
analogValue = arduino.analogRead(analogPin);
int inByte = int(map(analogValue,0,1023,0,255)); //I'm mapping the analogSensor values (0-1023) to values between 0-255 like you originally wanted =]
println(inByte);
}


I tried to preserve the variable names you used in your previous example - I tested this with a potentiometer hooked up to analog input 5 on the arduino and it worked just fine.

Lemme know how that works for you =] - hope it helps

cheers,
-Jeff

PS: in the arduino script in your .png, you have a line that reads:
analogValue = analogValue / 0.39
why is that? analogRead() returns 10-bit resolution values between 0-1023, so wouldn't dividing by 0.39 map your values between 0-2623 instead of 0-256 like you want?

EDIT: figured out how to use the CODE tags =]
Re: Read arduino serial variable
Reply #8 - Jun 16th, 2008, 2:50am
 
thanks, i use a simple script for show my problem, is different from the first code
I make your change, but the result is wron the report of println is 0.
for the code of  operation / 0.39 , it is a older script for  sensor ultrasound for convert from inc to cm.

mau




Re: Read arduino serial variable
Reply #9 - Jun 16th, 2008, 3:22am
 
perhaps try serialEvent() and see if that does anything?

http://processing.org/reference/libraries/serial/serialEvent_.html

I normally just stick to Firmata, so this is out of my league, sorry =/
Page Index Toggle Pages: 1