Hi everyone...
I have a problem with the communication between my xBees, I think is a timming problem or when to send and receive data. I would really appreciate your help.
My project is about send a number from one arduino connected to a Joystick ( the data is received by Processing and this is sent to the other arduino), then in the other arduino, I have to send a String to Processing.
Here's my code.
Arduino:
#include "mthread.h"
String val1 = "";
int val = 6;
char car;
int flag = 0;
String sensores = "";
//////////////////////////////////////////////////////////////////////////////////
class SerialMonitor : public Thread {
public:
SerialMonitor();
void readVal();
void writeVal();
protected:
bool loop();
};
SerialMonitor::SerialMonitor() {
Serial.begin(9600);
}
void SerialMonitor::readVal(){
while(Serial.available()>0){
car = Serial.read();
val1.concat(car);
}
val = val1.toInt();
Serial.println(val);
//delay
long t1 = millis();
do{
}while(millis() - t1 < 100);
}
void SerialMonitor::writeVal(){
int sensor = 500;
sensores = "arduino";
sensores.concat(";");
sensores.concat(sensor);
//Serial.println(sensores);
//delay
long t1 = millis();
do{
}while(millis() - t1 < 100);
}
bool SerialMonitor::loop() {
if (Serial.available()>0){
readVal();
}
writeVal();
//delay(100);
return true;
}
void setup() {
main_thread_list->add_thread(new SerialMonitor());
}
Processing:
import processing.net.*;
Client myClient1;
Client myClient2;
String dataIn = "6";
String sensor1 = "";
void setup() {
size(600,600);
background(255);
myClient1 = new Client(this, "192.168.0.52", 2000); //car
myClient2 = new Client(this, "192.168.0.50", 2000); //joystick
}
void draw() {
sensorsData();
if(dataIn == null){
dataIn = "6";
}
myClient1.write(dataIn);
if (myClient2.available() > 0) {
dataIn = myClient2.readStringUntil('\n');
delay(100);
}else{
dataIn = trim(dataIn);
print(dataIn);
delay(100);
}
if(sensor1 !=null){
if ( myClient1.available() > 0){ // If data is available,
sensor1 = myClient1.readStringUntil('\n'); // read it and store it in val
//sensor1 = trim(sensor1);
delay(100);
}
}
}
PD: I have no problems with the code in the Joystick.ino
I'm using:
2 arduino UNOs
2 xBee shields
2 WiFiBee V1r1 by Tinysine
1 Joystick