Hello im new to arduino. Im using 2 xbees to transmit and receive trigger data. the end point is sending to the receiver a string of "F3 251 2 11 0101", "F3 251 2 11 1001" and "F3 251 2 11 0011". what the reciever has to do is listen to this stream of data and print error. how ever im am not able to do so.
this is the code im using for the receiver:
char inByte;
void setup(){
Serial.begin(9600);
}
void loop() {
// if there's any serial available, read it:
if (Serial.available() >= 16) {
A byte can only contain 1 character, not 16. Also, = is used for assignments, == is used for comparison. Here is an example of reading a string from the serial monitor. This example converts the string to a number, but you could just as easily modify it to compare it to a string using the strcmp() function:
// To send a number through the serial monitor, put it between brackets
const char startByte = '<';
const char stopByte = '>';
// Maximum characters in an int + null terminated character
const byte maxBuffer = 6;
void setup() {
Serial.begin(57600);
Serial.println("[Serial2Int]");
}
void loop() {
// Stores the characters between the start and stop bytes
static char buffer[maxBuffer];
// Keeps track of spot in buffer
static byte index=0;
if (Serial.available() > 0 ) {
char inChar = Serial.read();
if (inChar==startByte) { // If start byte is received
index=0; // then reset buffer and start fresh
} else if (inChar==stopByte) { // If stop byte is received
buffer[index] = '\0'; // then null terminate
processData(buffer); // and process the data
index=0; // this isn't necessary, but helps limit overflow
} else { // otherwise
buffer[index] = inChar; // put the character into our array
index++; // and move to the next key in the array
}
/* Overflow occurs when there are more than 5 characters in between
* the start and stop bytes. This has to do with having limited space
* in our array. We chose to limit our array to 5 (+1 for null terminator)
* because an int will never be above 5 characters */
if (index>=maxBuffer) {
index=0;
Serial.println("Overflow occured, next value is unreliable");
}
}
}
void processData(char buffer[]) {
unsigned int value = atoi(buffer); // convert string to int
Serial.print("Value: ");
Serial.println(value);
}
thanks for the replies. But im still a bit confused. I am suppose to read that string of data before i print error. is there any other way to serial read that string? will it be better if a it was being read separately? like i serial read the 1st letter then if that is 'F' i read the next. until i read till the last digit then i print error.