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 › Many values from Processing to Arduino
Page Index Toggle Pages: 1
Many values from Processing to Arduino (Read 3673 times)
Many values from Processing to Arduino
Jan 26th, 2007, 12:55pm
 
How can i transfer many values (variables) from Processing to Arduino? I need two values to control a motor (ventilator) and LEDs?
Re: Many values from Processing to Arduino
Reply #1 - Aug 26th, 2007, 3:20am
 
I know this is old, but it's gone unanswered, you might have since found a solution, but others might be reading it looking for one.

From processing, you send data to Arduino using

port.write('A'); // use A as a handshake
port.write('0'); // first byte
delay(10); // add a small delay so you don't overwhelm Arduino
port.write('1'); // the second byte, etc etc

On the Arduino side:-

use

Serial.read()

Count the number of bytes received, when the number of bytes you expect to receive, have been received, then the individual bytes sent from processing can be extracted from an array.
Re: Many values from Processing to Arduino
Reply #2 - Sep 13th, 2007, 1:14pm
 
I recently wrote some message queueing functionality to ensure good communication between Processing and Arduino.

It works like this:
- Processing stores all the messages it wants to send in an array
- messages in the queue are sent one by one, and Arduino sends back a confirmation after each one it receives
- make sure that Processing only sends the next message after receiving confirmation about the previous one

I've also added a timeout so that a lack of confirmation for a given time triggers the same message to be sent again. Also, if after a couple of tries the message is still not confirmed, it is put at the end of the queue to retry later when the other messages have been sent.

It's mostly a matter now of deciding on a 'language' for Processing and Arduino to communicate with (e.g. I use the message '999' for confirmation and I precede each message from Processing with a number to specify the type of message that will follow). The exact nature of this language is up to you.

If anyone wants an example, let me know. I can post it here, but it'll take a little while to clean up the code and unless anybody wants it I won't go through the trouble Smiley
Re: Many values from Processing to Arduino
Reply #3 - Sep 14th, 2007, 9:37pm
 
I do it like this...

first two or three bytes can be used for a "handshake"
int[] arrayOne = {165,24,1,24,60,126,219,255,36,90,165};

void draw()
{
 // blah blah blah code to trigger send...
 for(byte x = 0; x < 11; x++){
   port.write(arrayOne[x]);
   }
}

on the Arduino end....

{
 if(Serial.available()){  // if there is serial data to read...
   bufferArray[index] = Serial.read(); // load the current byte into the current array index location
   index++;       // advance index by one
 }
 else{           // if there is no serial data...
   index = 0;     // reset the index and wait for the next packet
 }
 if((index >= 11)){  // if index is at matrix
   index = 0;     // reset the index to index location "0"
 }
}

edit: this way you only need to modify the variables in the array
Re: Many values from Processing to Arduino
Reply #4 - Sep 17th, 2007, 2:15pm
 
Thank you for your answers!

But I solve this problem for a long time.

I make it so:
If the value from Processing under 15 then is the value for variable A. If the value over 15 then ist the value for variable B.

Here is my arduino-code:

...

void serial_input()
{
 if (Serial.available() > 0) {
   incomingByte = Serial.read();
   //Serial.print("I received: ");
   Serial.println(incomingByte, DEC);
   digitalWrite(2, LOW);
 }
 //else {
 //  incomingByte = 0;
 //  digitalWrite(2, HIGH);
 //  digitalWrite(5, LOW);
 //  digitalWrite(6, LOW);
 //  digitalWrite(7, LOW);
 //}
 
 if (incomingByte > 15){
   motor = incomingByte;
 }
 else{
   lichter = incomingByte;
 }
}

...
Re: Many values from Processing to Arduino
Reply #5 - Sep 18th, 2007, 12:41pm
 
I've gotten a request to post the message queueing code, so I've cleaned it up and put it on http://www.rockabit.com/downloads/message%20queueing.zip

It contains both the Processing and the Arduino code. Some notes:
- I use Serial.buffer() which triggers serialEvent(). Other options are available, but I found this to function best when trying to send integers from Arduino to Processing. Please read the notes on this in the sendMsg() method in the Arduino code.
- I found negative numbers to pose problems when sent to Arduino. In my original code I prevented negative messages by adding an amount that ensured positive numbers, which was subtracted again when received by Arduino.

If you have any questions, feel free to ask!

Cheers, Ralph
Page Index Toggle Pages: 1