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 › Arduino to Processing, via Serial
Page Index Toggle Pages: 1
Arduino to Processing, via Serial (Read 11020 times)
Arduino to Processing, via Serial
Jan 20th, 2009, 9:21pm
 
I'm having problems using an IR sensor to send values from Arduino to Processing.  The Serial library is being used.  The IR sensor is connected to an Arduino board, hooked to my MacBook via USB, with Processing also running on the same computer.

In the Arduino sketch window, the sensor values (output by Serial.println) look correct, but when they are sent them to the Processing sketch, the println values come out in the Processing window as:
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
... etc

I also noticed that the Serial.println values in the Arduino sketch vary depending on whether I have 'Serial.print(range, DEC);' active or not.  When that line is on, the numbers I get range from 99 to about 2600.  When the line is off, the range is from 7 to about 80 (similar to the actual sensor distances in centimeters).

Here's my code (adjusted from existing sample files):
--------------------------------------
ARDUINO

void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
}

void loop() {
 sensorValue = analogRead(sensorPin); // read the pot value
 
 // the sensor actually gives results that aren't linear.
 // this formula converts the results to a linear range.
 int range = (6787 / (sensorValue - 3)) - 4;
 
 //Serial.print(range, DEC);    // print the sensor value to Processing
 Serial.println(range, DEC);  // print the sensor value
 delay(10);                   // wait 10 milliseconds
                              // before the next loop
}
--------------------------------------


--------------------------------------
PROCESSING
import processing.serial.*;

Serial port;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
 //size(200, 200);
 frameRate(10);
 // Open the port that the board is connected to and use the same speed (9600 bps)
 port = new Serial(this, 9600);
}

void draw()
{
 //if (0 < port.available()) {  // If data is available,
   val = port.read();         // read it and store it in val
   println(val);
 //}
 /*
 background(255);             // Set background to white
 if (val == 0) {              // If the serial value is 0,
   fill(0);                   // set fill to black
 }
 else {                       // If the serial value is not 0,
   fill(204);                 // set fill to light gray
 }
 rect(50, 50, 100, 100);
 */
}
--------------------------------------

I suspect there's a problem with how the Serial parts of the code are written, somehow Arduino is not talking to Processing.  (I'm not a great programmer).  Please let me know if you have any advice, thanks.
Re: Arduino to Processing, via Serial
Reply #1 - Jan 21st, 2009, 6:38pm
 
I see that your "if" statement and ending bracket in processing are commented out as well as some other stuff, they shouldn't be.
try adding back that commented out code and try resetting val to 0 after each loop,  try this:



import processing.serial.*;

Serial port;  // Create object from Serial class
int val; // Data received from the serial port

void setup()  
{
 size(200, 200);
 frameRate(10);
 // Open the port that the board is connected to and use the same speed (9600 bps)
 port = new Serial(this, 9600);
}

void draw()
{  
val=0;
 if (0 < port.available()) {  // If data is available,
   val = port.read();    // read it and store it in val
   println(val);
 }
 
 background(255);   // Set background to white
 if (val == 0) {    // If the serial value is 0,
   fill(0);    // set fill to black
 }  
 else {        // If the serial value is not 0,
   fill(204);       // set fill to light gray
 }
 rect(50, 50, 100, 100);
 
}


also just as a test, leave out DEC in your serial.print of the arduino sketch, if it works you should see ascii code numbers for your values in processing, I had an issue sending DEC before so I just converted the ascii values in processing


let me know if this works
Re: Arduino to Processing, via Serial
Reply #2 - Jan 21st, 2009, 10:58pm
 
Thanks for replying, Action_Owl.  

I've all your suggestions, but unfortunately it's still not working in Processing.  With the 'if port is available' statement activated, Processing actually doesn't show any values printing out.  If I comment out the 'if ' statement, then it outputs "-1" repeatedly, as it did when I first posted my problem.


Re: Arduino to Processing, via Serial (& FIRMA
Reply #3 - Jan 22nd, 2009, 3:48am
 
I finally got this to work, but by a rather different method.  Someone suggested I use Firmata, from the Arduino programming environment.  For those who aren't familiar with it, Firmata is a library you install into the Arduino environment.

i) install Firmata library into Arduino.
ii) upload the StandardFirmata sketch onto your Arduino board.  StandardFirmata can be found in the Arduino environment at File/Sketchbook/Examples/Library-Firmata/StandardFirmata.
iii) close Arduino program.
iv) open Processing sketch and run it.

In my case, I'm using a Sharp IR sensor to generate values for Processing.  The code I used in Processing is here:

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

Arduino arduino;
int sensorPin = 0;    // Analog input pin
int sensorValue = 0;  // value read from the pot

void setup()
{
 //println(Arduino.list());
 arduino = new Arduino(this, Arduino.list()[0]); // v2
 //arduino = new Arduino(this, Arduino.list()[0], 57600); // v1
 arduino.pinMode(sensorPin, Arduino.INPUT);
}

void draw()
{
 sensorValue = arduino.analogRead(sensorPin); // read the pot value
 
 // the sensor actually gives results that aren't linear.
 // this formula converts the results to a linear range.
 int range = (6787 / (sensorValue - 3)) - 4;

 println(range);  // print the sensor value
 delay(10);                   // wait 10 milliseconds
                              // before the next loop
}
//--------------------------------------
Re: Arduino to Processing, via Serial
Reply #4 - Jan 22nd, 2009, 6:03pm
 
sorry jlui1200,
I assumed you had already installed Firmata
glad u got it working!
Re: Arduino to Processing, via Serial
Reply #5 - May 9th, 2009, 11:18am
 
hi, i'm having a very similar problem but with a light sensor instead, I've uploaded standard fimata onto the arduino and my processing script is very similar to yours:
Code:
import processing.serial.*;

import cc.arduino.*;

Arduino arduino;
int sensorPin = 0;
int sensorValue;


void setup() {
size(470, 280);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 115200);


arduino.pinMode(sensorValue, Arduino.INPUT);
}

void draw() {

sensorValue = arduino.analogRead(sensorPin);

println(sensorValue);


}


but it just keeps printing 0. The light sensor seems to work fine in arduino. Do i need to assign pins in the firmata code? I am really confused by this.

Re: Arduino to Processing, via Serial
Reply #6 - May 30th, 2009, 5:22pm
 
Hi,

My project is very similar to the two main posters here.  I am using an IR thermopile into a Boarduino, output via serial/USB to Processing.  

What I have been searching for a long time is code I can adapt or use to make a realtime, color bar graph display of my Boarduino's output. I want to see the bar graph rapidly respond to changes in the thermopile output coming from my Boarduino.  

Eventualy, I want to export the Processing App as an .exe so it can install on a laptop.  

For my purposes, would I be better off biting the bullet and trying to learn some other language like Python, or ?  

Thanks!  

Les
Re: Arduino to Processing, via Serial
Reply #7 - May 30th, 2009, 5:22pm
 
Hi,

My project is very similar to the two main posters here.  I am using an IR thermopile into a Boarduino, output via serial/USB to Processing.  

What I have been searching for a long time is code I can adapt or use to make a realtime, color bar graph display of my Boarduino's output. I want to see the bar graph rapidly respond to changes in the thermopile output coming from my Boarduino.  

Eventualy, I want to export the Processing App as an .exe so it can install on a laptop.  

For my purposes, would I be better off biting the bullet and trying to learn some other language like Python, or ?  

Thanks!  

Les
Re: Arduino to Processing, via Serial
Reply #8 - Oct 29th, 2009, 9:22pm
 
I have the above mentioned problem. The proximity sensor works fine with Max/MSP+Arduino while in Processing the value printed is zero. The sketch:

Quote:
import cc.arduino.*;

import processing.serial.*;

import promidi.*;

Arduino arduino;

MidiIO midiIO;
MidiOut midiOut;

int sensorv;

void setup(){
  size(200,200);
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  midiIO = MidiIO.getInstance(this);
  midiOut = midiIO.getMidiOut(1,"IAC Driver - Bus 1");

  midiIO.printDevices();
  println(Arduino.list());

}
void draw(){
  background(0);
  rect(mouseX,mouseY,10,10);
  sensorv = arduino.analogRead(0);
  midiOut.sendController(new Controller(1,sensorv));
  println(sensorv);
}












Re: Arduino to Processing, via Serial
Reply #9 - Nov 7th, 2009, 5:17am
 
Hi,
I was stumped by this for a while too. I was attempting to use Processing via Firmata to read the analogPin on my Arduino.
I was using a potentiometer to generate a signal for the analogPin.
When i tried using the StandardFirmata example on the Arduino, Processing kept giving me zero's, but then I tried loading the SimpleAnalogFirmata example onto the Arduino instead, and it worked a treat. I haven't had time to look into the details as to why.
Hope this helps some.
H

Code:

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

Arduino arduino;
int analogPin = 0;
int value = 0;

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

void draw(){
value = arduino.analogRead(analogPin);
println(value);
delay(10);
}
Re: Arduino to Processing, via Serial
Reply #10 - Nov 7th, 2009, 3:55pm
 
Hi, boys,
I solved problem like this some weeks ago. Finally I found the problem. When you read serial data from Arduino, you - most probably - read only the last byte of your data. Look at my solution in topic named "How to read float variable from serial port".  It is here: http://processing.org/discourse/yabb2/num_1254511350.html
Re: Arduino to Processing, via Serial
Reply #11 - Nov 24th, 2009, 6:28pm
 
I think a lot of people are having problems with Firmata because the version included in Arduino 0017 is simply broken, as acknowledged by the Firmata developers.  search for "Firmata 0017" on the Arduino forum site.
Page Index Toggle Pages: 1