Hi,
I needed help about sending values to serial port,
I'm a newbee and need to send 3 diferent values from arduino to processing. ( 3 different sensors)
what's the easier way to do that? I can only send a byte, that's all? how could I split it in different values?
I don't know if this is well explained (sorry , my english is not really good)
thanks to those who will help me.
What is the range of values you need to send from Arduino to processing ?
One possible way is to split the valur into a high byte and a low byte abd send them one at a time and then in processing combine them again.
Thanks a lot for your answer.
I have 3 analog values coming from 3 potentiometers.
how could I send them alternatively?
you can either split yor values in a high and a low byte and send them individually, or send your value as string
thanks,
what's the max string length we can send?
i send a string with "spliters" characters and split the string in processing?
this is what you mean?
is there an example on the web to explain this precisely? :-[
someone to help me?
thanks a lot
i send a string with "spliters" characters and split the string in processing ... is there an example on the web to explain this precisely? :-[
The first google hit with the search terms: arduino processing split
gives this link: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Electronics;action=display;num=1193069410
Reply #3 has an example that seems relevant to what you want to do.
i send a string with "spliters" characters and split the string in processing ... is there an example on the web to explain this precisely? :-[
The first google hit with the search terms: arduino processing split
gives this link: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Electronics;action=display;num=1193069410Reply #3 has an example that seems relevant to what you want to do.
thanks mem, i've already tried with the example you linked, but It doesn't seem to work, I only get one value out of
2. (2 potentiometers). it might not be the right way to process? I don't know where the problem is...
i've already tried with the example you linked, but It doesn't seem to work, I only get one value out of 2. (2 potentiometers). it might not be the right way to process? I don't know where the problem is...
If you have only two values to send then you need to modify the code, try the following and see if that works.
//construct a message with multiple values in arduino
Serial.print(value1, DEC);
Serial.print(",");
Serial.print(value2, 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 == 2){
var1 = inputs[0];
var2 = inputs[1];
}
}
}
if that still doesn't work, comment out any other code in your sketch that may be writing to the serial port, it could be confusing the processing code.
hi,
my code seems to be the same than the one you propose (maybe i'm wrong)
here is my processing sketch:
import processing.serial.*;
int linefeed = 10;
Serial myPort;
int val1, val2;
void setup()
{
size(100, 100);
println(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.bufferUntil(linefeed);
}
void draw(){
background(0);
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil(linefeed);
if(myString != null ){
myString = trim(myString);
int values[] = int(split(myString, ","));
if (values.length == 2){
val1 = values[0];
val2 = values[1];
println(val1);
println(val2);
}
}
}
for information, here is my arduino's one.
int analogInput = 3;
int analogInput2 = 5;
int led = 13;
int analogValue = 0;
int analogValue2 = 0;
void setup()
{
pinMode(led, OUTPUT);
pinMode(analogInput, INPUT);
pinMode(analogInput2, INPUT);
beginSerial(9600);
}
void loop()
{
analogValue = analogRead(analogInput);
analogValue2 = analogRead(analogInput2);
Serial.print(analogValue, DEC);
Serial.print(",");
Serial.println(analogValue2, DEC);
Serial.print("|");
if (analogValue < 512) digitalWrite(led, HIGH);else digitalWrite(led, LOW);
delay(100);
}
don't really know if the suitable ascii code for the "linefeed" is 10 or 124? the both seem to correspond.
thanks again for your help.
Linefeed is 10, but you are not sending that, you are sending the '|' character which has an ascii value of 124.
Try it with this line in your processing code
String myString = myPort.readStringUntil(124); //the ascii value of the "|" character
Instead of:
String myString = myPort.readStringUntil(linefeed);
If that still doesn't work, please post whatever output you do see from processing.
i've tried with the ascii code 124... and it's the same result
the printed values are for example..
1023 (my first pot)
0 (my second pot)
1023
0
1220
0
..... etc
when i change the position of my pot 1, the value changes.... so it's allright for this one
but when i turn the second, the printed value is still "0".
it's as if the second one was unvisible.
Lets see what the arduino is sending.
Stop the processing app and start the serial monitor in the Arduino IDE. You should see the strings printed on the monitor. When you have a half dozen lines or so, stop the monitor and copy the serial output into a post here.
That should help us determine if the problem is with the arduino or processing code.
I realise that i posted in the wrong place, i would have been better in software/interfacing!!!.