Hello, I tried searching in here and couldnt find what I was looking for. Story short, my pc send to the arduino a command (keyword) through xbee and the arduino, based on the keyword will do something and send a response back. I need to send strings through xbee because I attach the module ID to the response so the pc knows which module replied back.
The problem I have is that the first command works fine. When I send a second command, the serial string sent still includes the first response.
Here is the serial monitor results:
CM <---- command from PC
AA16| <--- response sent o pc from arduino (Good!)
TEST <---- command from PC
AA16|CMCOK| <--- response sent o pc from arduino (BAD!).. should only be "CMCOK|"
I am using xbee.print so I am able to send the string. Before I added the Module ID to it, I was using xbee.write because it was not using a string variable and everything was working fine.
here is my code: (MODID is a string variable)
#define buzzerPin 8
#define XBee Serial3
String MODID = "AA";
String MODQ = "16";
const byte numChars = 32; // XBEE variable
char receivedChars[numChars]; // XBEE variable
boolean newData = false; // XBEE variable
void setup() {
XBee.begin(9600);
Serial.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
showNewData();
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (XBee.available() > 0 && newData == false) {
rc = XBee.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println(receivedChars); //Uncomment to see string in serial monitor
String RecData; RecData=String(receivedChars); RecData.trim();
if(RecData == "CM"){
String MODCHK = MODID += MODQ += "|";
Serial.println(MODCHK);
XBee.print(MODCHK);
delay(1);
}
if(RecData == "TEST"){
String TestStatus = "";
TestStatus += MODID;
digitalWrite(buzzerPin,HIGH); delay(100);
digitalWrite(buzzerPin,LOW); delay(100);
digitalWrite(buzzerPin,HIGH); delay(100);
digitalWrite(buzzerPin,LOW);
TestStatus += "CMCOK|";
Serial.println(TestStatus);
XBee.print(TestStatus);
delay(1);
}
newData = false;
}
}
Any help is appreciated... starting to bang my head lol