Hello guys!
I'm writing to you because I discovered a glitch in the communication between Arduino and Processing via XBEE (wireless module).
I've creating with some friends a rover that you can control via pc.
If you type v100 the rover will move forward with a tension of 100v.
The problem is this:
In the Arduino there's a listener function that get all the char of the rover and with some switch he define what the rover has to do:
e.g. v100 is the command > the arduino will listen by reading from serial v, 1, 0, 0.
The code works well if I type v100 via Serial Monitor but when I put this command on the processing this doesn't work.
I don't know how serial monitor send the information.. on processing all the characters of the command are sent singly.
I don't know what to do.. could you help me??
Thanks
Those are part of the codes reserved for the communication:
Arduino code
void serialevent()
{
char comando = 0; // var for the command
char inchar = 0; //char incoming from the serial
String linestring = ""; //string of the incoming char
char linearray[9] = ""; //array same as the string /\
boolean serialComplete = false; //is the string completed?
int temp = 0;
boolean negative = false;
while( Serial.available() ) //se c'è qualcosa nel buffer
{
inchar = (char) Serial.read(); //get char
if(inchar >= 'a' && inchar <= 'z') /if it's a letter
comando = inchar;
else if(inchar >= '0' && inchar <= '9') //if it's a number
linestring += inchar;
else if(inchar == '-') //if it's negative
negative = true;
else if(inchar == 10) //if it's the terminus
{
linestring.toCharArray(linearray, 9); //copio la stringa nell'array di char
switch(comando)
{
//direzione
case 's':
temp = atoi( linearray ); //da stringa a numero
if(negative)
temp = -temp;
dir = constrain( temp, -255, 255 );
confirm('s', temp);
break;
//velocità
case 'v':
temp = atoi( linearray );
if(negative)
temp = -temp;
vel = constrain( temp, -255, 255 );
confirm('v', temp);
break;
//luci
case 'l':
temp = atoi( linearray );
luci = constrain( temp, 0, 255 );
confirm('l', temp);
comandoLuci();
break;
//dimostrazione
case 't':
test();
confirm('t', 0);
break;
//freno a mano
case 'f':
stopall(true);
confirm('f', 0);
break;
//dati attuali
case 'p':
speak();
confirm('p', 0);
break;
//aiuto via seriale
case 'h':
help();
confirm('h', 0);
break;
case 'b':
buzzer();
confirm('b', 0);
break;
//in caso di errore
default:
Serial.println(F("EEE"));
break;
}
//erase
memset( linearray,'\0',sizeof( linearray ));
linestring = "";
inchar = 0;
comando = 0;
temp = 0;
negative = false;
}
delay(serialDelay); //little delay for receiving another char
}
Processing code:
void SerialSend(String dataString){
char dataChar;
for(short e = 0; e < dataString.length(); e++){
dataChar = dataString.charAt(e);
println(dataChar);
myPort.write(dataChar);
}//for
println(char(10));
myPort.write(10);
}//SerialSend
if(PressedMovKey[0]){
if(timeW == Reactivity-1){
//**Graphic Rover**//
RRcol = color(#0060ff);
RLcol = color(#0060ff);
/**Pass Information to Arduino**/
//pass only one info
if(!OneSend[0]){
println("W_"+LoopTW);
OneSend[0] = true;
SerialSend("v100");//Front
}//if
}//One Action