Processing and arduino help

 

Hello, my question is what should i do if i want to send multiple variables from processing/arduino to arduino and vice-versa

I think you put it in a long string but how do you decode it. I think its like this not sure 123,456,789,100 with commas seperating them. I want to send values from different sensors from one arduino to another with xbee like shown here.

 

http://www.jeremyblum.com/2010/05/09/sudoglove/

No easy way here

There is no magic bullet here, you are going to simply have to learn how to deal with strings. Its a really good thing to know -for RC stuff as well as a lot of other stuff.

I would forget for a moment, how this relates to this particular problem and instead just start doing some searches for info on manipulating strings. There are even a lot of libraries that will help you with this. Gotta learn how the zero-thing works, how to split them, remove whitespace, all-caps, all-lower, adding them, etc…

It should be said though, that at the end of the day, you are just sending bytes here. No real reason they have to be in the form of a string. Any reason you don’t just break up your int’s into bytes and just simply send them as “numbers”?

But what if I want to send

But what if I want to send data from 4 sensors from one arduino to another using xbee.

wouldnt the recieving arduino get confused with so many bytes.

How would it know which belongs to what so i thought for 4 sensors it could be like

sensor 1 sensor 2   sensor 3 sensor 4

123        ,     456    ,     789    ,     100

If im asking a stupid question i am sorry.

also any sites for me to

also any sites for me to understand manipulating strings.

I kind of know what strings are since we are learning c++ in my school.

There are many ways to accomplish what you want

The difficulty lies in picking one that is easy to use and reasonably efficient.

Here are some questions that you need to answer first:

 - Will you allways be sending the same values?

 - Will you send them all at the same time and in the same order?

 

"Confused by so many bytes"

Well, “confused” does not really exist in a robot brain, it is either coded correctly or not. You can however, loose data or receive crap data --and there are ways to fix this.

When sending a given number of bytes, it is sometimes a good idea to use qualifiers and terminators. These are simply two numbers sent at the beginning and/or end of your sends to help keep things “in sync”. The most common example of this is to send a CR or LF (13 or 10) at the end of your sends to say, “this send is done”. Data sent without this last byte can be ignored --something did not come in right…

Another option which is fairly fail-safe is to use a checksum. Put simply, you take all the bytes you are going to send and add them up. This total gets sent at the end. Your receiver gets the data, adds all the bytes and compares it to the checksum number. If they don’t match, it ignores the data (or does whatever you tell it to do when this happens) and waits for another clean packet to come by.

 

All-in-all, if you are just sending a few bytes worth of data, and not trying to do it at incredible baud rates, you should rarely if ever have bad data using X-bees.

 

One more because I think this might get you…  You can not send an int, long, etc. Everything must be sent as bytes. If you have a number smaller than 0 or bigger than 255, it will have to be “broken up” into its component bytes, sent, then “reassembled” on the other side.

When you ask the first
When you ask the first question do you mean will I kepp sending a 250 on and on its a no.
If you mean will I send the same type of data yes.
And for the second question yes I will send them at the same time .

Is this what I’m looking
Is this what I’m looking for
http://arduino.cc/forum/index.php/topic,40350.0.html

I kind of made some code on

I kind of made some code on this subject 

the processing part takes x position and y position of mouse and writes it to the port of the arduino.

heres processing code.

import processing.serial.*;

 Serial port;

 

 void setup() {

 size(180, 180);

 

 println(“Available serial ports:”);

 println(Serial.list());

 

 // Uses the first port in this list (number 0).  Change this to

 // select the port corresponding to your Arduino board.  The last

 // parameter (e.g. 9600) is the speed of the communication.  It

 // has to correspond to the value passed to Serial.begin() in your

 // Arduino sketch.

 port = new Serial(this, Serial.list()[0], 9600);  

 

 // If you know the name of the port used by the Arduino board, you

 // can specify it directly like this.

 //port = new Serial(this, “COM1”, 9600);

 }

 

 void draw() {

 // draw a gradient from black to white

 for (int i = 0; i < 180; i++) {

 stroke(i);

 line(i, 0, i, 180);

 }

 

 // write the current X-position of the mouse to the serial port as

 // a single byte

 port.write(mouseX);

 port.write(",");

 port.write(mouseY);

 }

the x and y goes in this form 120,120

 

the arduino side kind of decodes it i think it uses serial.parseint got it here http://arduino.cc/en/Tutorial/ReadASCIIString

here is the adino code 

 

#include <Servo.h>

Servo servo;

Servo servo1;

void setup()

{

  // initialize the serial communication:

  Serial.begin(9600);

 

  servo.attach(9);

  servo1.attach(10);

}

 

void loop() {

  int brightness;

  int brightness1;

 

  // check if data has been sent from the computer:

  if (Serial.available()) {

    // read the most recent byte (which will be from 0 to 255):

    int brightness = Serial.parseInt();

    int brightness1 = Serial.parseInt();

    // set the brightness of the LED:

    servo.write(brightness);

    servo1.write(brightness1);

 

  }

}

im kind of confused what

im kind of confused what order is it in and since i am starting for my first time im using processing to send values then i will use those values and turn them into pwm or something.

What you di im guessing is for the xbee and 2 arduinos

thank you for heling and putting the effort

So the standard firmata is

So the standard firmata is the only way i guess but what if I need to send arduino to arduino.

I’ll see if I can try this again…

Alright, here is what I would do:

On the processing side, write a sketch that goes like this:

byte i=0;

loop:
 
  mySerial.print(i);      **I can’t really remember how the serial.print command looks like in processing
  mySerial.print(i+1);
  mySerial.print(i+2);

  delay(1000); 
  i++; 

 

Now you have a little count spitting out of processing. It is 3 bytes, the count, count+1 and count+2. Your first data will be 0,1,2, the second batch will be 1,2,3, the next one will be 2,3,4, and so on. Just let this run, it will do a “send” of 3 bytes, first-second-third, once a second. When we get up to our 255 limit for a byte, each count should simply overflow and start again.

Using this system, we can test to see that the proper data is coming through, and we can also easily see if it is in the correct order.

 

Now on the arduino side, we are simply waiting to catch 3 bytes as they come in:

loop:

if(serial.available()>2)
{
  variableOne=serial.read;
  variableTwo=serial.read;
  variableThree=serial.read;
  serial.flush();

 }

From there, you can do anything you want to see if your data is being read right. You can blink leds, send the data out to a LCD, add another FTDI and SoftSerial and send it back to the computer to serial monitor, whatever. But the above is about as simple as it can be, just send three bytes in order, and receive them in the same order. That’s it.

 

Its not really working I

Its not really working I tried writing the values to leds since i cant open the terminal to debug.

So i made the arduino recieve the values from processing andthen send them back to processing’s terminal to debug but i get some weird values.

Weird values, no problem

Weird values are actually kinda common in a situation like this. It would take a really long time to explain the specific ins and outs of serial data transfer, so I will hit the bullet points. You will have to play around a bit and do some googling.

Put simply, serial.print and serial.write are different. Also, take a look at the ASCII chart and you will find that all 256 combinations of a byte can be written 4 or 5 different ways --I.e. 65 0x43 101 @#65 and regular ol’ “A”. Your funny values are probably due to the fact that an “A” is being sent and a “65” is being recieved (or any other combination). I am not going to lie to you, it gets really confusing and the more you learn, the more you seem to be confused --At least it did with me.

After I while, I just accepted the fact that I was going to have to play with the "serial.print"s and the "serial.write"s until I found a combination that worked. You should also play with adding BYTE() to your sends, serial.write(BYTE(yourVariableToSend). --Yeah, I seem to remember that “BYTE” thing working in the past, try that one for sure.

I am sorry I don’t have better answers for you on this one, it has been quite a while since I have done this. --and when I did, I really did just play with it until it worked. I am sure I wrote down the right way to do it, but damned if I would ever be able to find it.

Or its your baud rate

…or your baud rates don’t match…

thnks for the help ill try

thnks for the help ill try it for sure :wink: