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 › several data from arduino to processing
Page Index Toggle Pages: 1
several data from arduino to processing (Read 7343 times)
several data from arduino to processing
Oct 22nd, 2007, 6:10pm
 
I use currently this piece of code:

void serialEvent(int serial){
 if(serial!=10) {      
     if (serial=='A') buf = true;
     if (serial=='B') buf = false;
     if (buf){ if (serial!='A') bufA += char(serial);
               }else{
               if (serial!='B') bufB+= char(serial);
               }
     } else {
               if (buf){  value1 = int(bufA); bufA="";
               } else {   value2 = int(bufB); bufB="";
               }
     }      
}

to send two sets of data (labeled "A" and "B") from arduino to processing. I am puzzled trying to add two more data "C" and "D". I do not know how to do it! Do I need 2 booleans buf1 and buf2?
Re: several data from arduino to processing
Reply #1 - Oct 27th, 2007, 4:00pm
 
Hello,

I am working on the same problem, principaly to pass 8 sensors analog information from Arduino to processing and 3 stepper information from processing to Arduino.

in Arduino I have this program (here for two sensors, but easy to use with 6):

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 int Sensor = analogRead(0);
 Serial.print("a");
 decomp(Sensor);
 Serial.println();
 Sensor = analogRead(1);
 Serial.print("b")
 decomp(Sensor);
 Serial.println();
}

void decomp(int Sensor){
 int val0 = Sensor / 1000;
 Serial.print(val0);
 Sensor = Sensor % 1000;
 int val1 = Sensor /100;
 Serial.print(val1);
 Sensor = Sensor % 100;
 int val2 = Sensor / 10;
 Serial.print(val2);
 int val3 = Sensor % 10;
 Serial.print (val3);
}

in Processing, I have this one:

import processing.serial.*;

Serial myport;
int val0 = 0; //variable fo units, tens etc.
int val1 = 0;
int val2 = 0;
int val3 = 0;
char val4; //letter tag
int val5 = 0; //variable for 10 and 13 ASCII
int val6 = 0;
int vala; //variable for each sensor
int valb;
int valc;
int vald;
int vale;
int valf;

void setup() {
 size(400,400);
 frameRate(25);
 myport = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
 background(0);
 if (0 < myport.available()) {  // If data is available,
   val5 = myport.read();         // read it and store it in val5
   if (val5 ==13){ //new line
     val6 = myport.read(); //carriage return
     val4 = myport.readChar(); //letter tag
     println(val4); //for test purpose
     val0 = myport.read() - 48; //48 is ASCII value of 0
     println(val0); //thousands
     val1 = myport.read() - 48;
     println(val1); //hundreds
     val2 = myport.read() - 48;
     println(val2); //tens
     val3 = myport.read() - 48;
     println(val3); //units
     int valT = (val0 * 1000) + (val1 * 100) + (val2 * 10) + val3
       switch (val4){
     case "a":
       vala = valT;
     case "b":
       valb = valT;
     case "c":
       valc = valT;
     case "d":
       vald = valT;
     case "e":
       vale = valT;
     case "f":
       valf = valT;
     }
   }
 }
}

The strange is that work, when I start the program, I read the good value, but not changing in real tim when I move the pots. But if I stop and start the program, the value are updated. When I receive the serial flow in Arduino or in goSerial app, the values are updated in real time.

Perhaps my project can help you? perhaps someone have an idea of what is wrong?

My goal is to use it to obtain accurate information in Isadora, for the moment I use a Max/Msp patch, working well in both way, but I want to use processing for educational/licence reasons.

Thank you in advance


Re: several data from arduino to processing
Reply #2 - Oct 29th, 2007, 8:55pm
 
hi,

i think this is a good starting point:
http://processing.org/learning/libraries/serialcallresponse.html

if you want to send back values to arduino: instead of sending 'A' to request the new data, send your values. On arduino, instead of answering imediately after receiving a byte, wait for all three (or whatever number) of values to arrive.

In processing, go for the array-solution igoe suggests. on arduino, you need some counter variable, to know the state of the communication and to react accordingly in the loop().
Re: several data from arduino to processing
Reply #3 - Nov 26th, 2007, 3:58pm
 
First, let me recommend "Making Things Talk" by Tom Igoe.

Now, a good way to send data between processing and arduino is by constructing a simple protocol with strings. You'll need each value you're passing, a delimiter to separate the values, and either a header or a footer to know when you're packet is finished. In the example, I'm using a comma as a delimiter and a pipe as the footer. Processing reads data until it reaches the footer and then uses the delimiter to tell the input values apart. You'll still want to set up communication so that each side waits for the other to finish talking.

//construct a message with multiple values in arduino
//we can't just send a string from here since it only officially understands 'bytes'
Serial.print(value1, DEC);
Serial.print(",");
Serial.print(value2, DEC);
Serial.print(",");
Serial.print(value3, DEC);
Serial.print("|");

//read the message in Processing
void serialEvent(Serial myPort){
 String myString = myPort.readStringUntil(124); //the ascii value of the "|" character
 if(myString != null ){
   myString = trim(myString); //remove whitespace around our values
   int inputs[] = int(split(myString, ','));
   //now assign your values in processing
   if(inputs.length == 3){
     var1 = inputs[0];
     var2 = inputs[1];
     var3 = inputs[2];
   }
 }

}
Re: several data from arduino to processing
Reply #4 - Jan 25th, 2008, 11:57pm
 
I made this script for eight buttons. The values are added  binary

Code:

ARDUINO:

int pins[] = { 3, 4, 5, 6, 7, 8, 9, 10};
int num_pins = 8;
int old_val, new_val;

void setup()
{
beginSerial(9600);
for (int i = 0; i < num_pins; i++) pinMode(pins[i], INPUT);
old_val = 0; new_val = 0;
}

void loop()
{
new_val = 0;
int pow = 1;
for (int i = 0; i < num_pins; i++) {
new_val += digitalRead(pins[i]) * pow;
pow = pow * 2;
}
if(old_val!=new_val) {
Serial.println(new_val);
delay(100);
}
old_val = new_val;
}


Code:

PROCESSING:

import processing.serial.*;

// ---------------------------------------------------------
Serial myPort;
PFont myFont;
float val;
int[] pin_val;
int lf = 10; // ASCII linefeed
boolean received;

// ---------------------------------------------------------
void setup() {
size(400,200);
myFont = createFont("Arial",18);
textFont(myFont, 18);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
pin_val = new int[8];
val = 0;
received = false;
}

// ---------------------------------------------------------
void draw() {
background(0);
fill(255);
textFont(myFont, 18);
text("received: " + val, 10,50);
if (received) rect(10,10,16,16);
received = false;
show_boxes();
}

// ---------------------------------------------------------
void serialEvent(Serial p) {
String myString = myPort.readStringUntil(lf);
if(myString != null){
val = float(myString);
float val2 = val;
received = true;
for(int i=7; i>=0;i--) {
pin_val[i] = 0;
if(val2>=pow(2,i)) {
val2 -= pow(2,i);
pin_val[i] = 1;
}
}
}
}

// ---------------------------------------------------------
void show_boxes() {
for(int i=0; i<pin_val.length; i++) {
fill(255);
textFont(myFont, 9);
text(i+3, 14+i*20, 95);
if(pin_val[i]!=0) fill (200, 100, 0);
rect (10+i*20, 100, 16, 16);
}
}


greetings ascorbin

Re: several data from arduino to processing
Reply #5 - Mar 6th, 2008, 11:42pm
 
David Wicks wrote on Nov 26th, 2007, 3:58pm:
First, let me recommend "Making Things Talk" by Tom Igoe.

Now, a good way to send data between processing and arduino is by constructing a simple protocol with strings. You'll need each value you're passing, a delimiter to separate the values, and either a header or a footer to know when you're packet is finished. In the example, I'm using a comma as a delimiter and a pipe as the footer. Processing reads data until it reaches the footer and then uses the delimiter to tell the input values apart. You'll still want to set up communication so that each side waits for the other to finish talking.

//construct a message with multiple values in arduino
//we can't just send a string from here since it only officially understands 'bytes'
Serial.print(value1, DEC);
Serial.print(",");
Serial.print(value2, DEC);
Serial.print(",");
Serial.print(value3, DEC);
Serial.print("|");

//read the message in Processing
void serialEvent(Serial myPort){
 String myString = myPort.readStringUntil(124); //the ascii value of the "|" character
 if(myString != null ){
   myString = trim(myString); //remove whitespace around our values
   int inputs[] = int(split(myString, ','));
   //now assign your values in processing
   if(inputs.length == 3){
     var1 = inputs[0];
     var2 = inputs[1];
     var3 = inputs[2];
   }
 }

}


I'm trying to use Wicks' example above to take data from two potentiometers and use the numbers as x and y coords to move a square around on the screen.  It seems like the split is working, but I wind up with an array with a correct value for the first pot and a 0 for the second one regardless of what the value actually should be.  I know that the data string inRead was definitely read in and was not changed.  Just something seems to be not working with the array.  

Any ideas

I'm running:

Arduino v 0009 Alpha
Processing v 0135 Beta
Mac OSX 10.4.11
Re: several data from arduino to processing
Reply #6 - May 2nd, 2008, 12:22am
 
hi, I got the same problem,
it works for one potentiometer, but for the other, processing allways prints "0"
do someone have an idea to solve this problem.
Thanks you.
Re: several data from arduino to processing
Reply #7 - May 9th, 2008, 8:46pm
 
The problem is that processing has some problems  with the char "|", so what is necessary is just adding a  comma "," after the second value that you want to send  to processing like this:

valor1 = analogRead(2)/4;
valor2 = analogRead(3)/4;
Serial.print(valor1,DEC);
Serial.print(",");
Serial.print(valor2,DEC);
Serial.print(",");
Serial.print("|");

Then in processing, discard the tird value, the comma ",".
It worked for me
Regards
CAL
Re: several data from arduino to processing
Reply #8 - Apr 28th, 2009, 7:24am
 
Is it possible to do this the other way, i.e. send a string to the arduino from processing, read it as a string and then separate it into 3 values?

First, let me recommend "Making Things Talk" by Tom Igoe.

Now, a good way to send data between processing and arduino is by constructing a simple protocol with strings. You'll need each value you're passing, a delimiter to separate the values, and either a header or a footer to know when you're packet is finished. In the example, I'm using a comma as a delimiter and a pipe as the footer. Processing reads data until it reaches the footer and then uses the delimiter to tell the input values apart. You'll still want to set up communication so that each side waits for the other to finish talking.

//construct a message with multiple values in arduino
//we can't just send a string from here since it only officially understands 'bytes'
Serial.print(value1, DEC);
Serial.print(",");
Serial.print(value2, DEC);
Serial.print(",");
Serial.print(value3, DEC);
Serial.print("|");

//read the message in Processing
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(124); //the ascii value of the "|" character
if(myString != null ){
  myString = trim(myString); //remove whitespace around our values
  int inputs[] = int(split(myString, ','));
  //now assign your values in processing
  if(inputs.length == 3){
    var1 = inputs[0];
    var2 = inputs[1];
    var3 = inputs[2];
  }
}

}
Re: several data from arduino to processing
Reply #9 - Apr 28th, 2009, 7:25am
 
Is it possible to do this the other way, i.e. send a string to the arduino from processing, read it as a string and then separate it into 3 values?

Not sure of the code on the arduino side for this...anyone?

First, let me recommend "Making Things Talk" by Tom Igoe.

Now, a good way to send data between processing and arduino is by constructing a simple protocol with strings. You'll need each value you're passing, a delimiter to separate the values, and either a header or a footer to know when you're packet is finished. In the example, I'm using a comma as a delimiter and a pipe as the footer. Processing reads data until it reaches the footer and then uses the delimiter to tell the input values apart. You'll still want to set up communication so that each side waits for the other to finish talking.

//construct a message with multiple values in arduino
//we can't just send a string from here since it only officially understands 'bytes'
Serial.print(value1, DEC);
Serial.print(",");
Serial.print(value2, DEC);
Serial.print(",");
Serial.print(value3, DEC);
Serial.print("|");

//read the message in Processing
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(124); //the ascii value of the "|" character
if(myString != null ){
  myString = trim(myString); //remove whitespace around our values
  int inputs[] = int(split(myString, ','));
  //now assign your values in processing
  if(inputs.length == 3){
    var1 = inputs[0];
    var2 = inputs[1];
    var3 = inputs[2];
  }
}

}
Page Index Toggle Pages: 1