Hello,
I have two arduinos talking through serial. One is acting like an RS232 device(just simulating query response) and the other is sending the queries. I am trying to get the sending device programmed in a way that it will send the first command and wait for a response...then send the second command and wait for a response...etc. But I am receiving the following:
int i = 0;
int lastSent = 0;
String readString;
char cmds[5] = {'1', '2', '3', '4', '5'};
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < sizeof(cmds); i++){
Serial.write(cmds[i]);
}
if (Serial.available() > 0) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == '\r') {
Serial.println(readString); //prints string to serial port out
readString = ""; //clears variable for new input
Serial.flush();
i++;
}
else {
readString += c; //makes the string readString
}
}
delay(5000);
}
Receiver Code:
void setup()
{
// Set up both ports at 9600 baud. This value is most important
// for the XBee. Make sure the baud rate matches the config
// setting of your XBee.
Serial.begin(9600);
}
void loop()
{
String readString;
if (Serial.available())
{ // If data comes in from XBee, send it out to serial monitor
byte ch;
ch = Serial.read();
switch(ch){
case '1':
Serial.write("A\r");
break;
case '2':
Serial.write("B\r");
break;
case '3':
Serial.write("C\r");
break;
case '4':
Serial.write("D\r");
break;
case '5':
Serial.write("E\r");
break;
case '6':
Serial.write("F\r");
break;
default:
break;
}
}
}
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
The "commands" are just one character, aren't they? Then why do you try to send all 5 commands before looking for a response?
Think about this as a state machine. Start in the initial "zero commands sent" state. Send a command, now you are in the "waiting for command 1 response" state. When you get that response, you can go forwards to the next state. If you detect that you've stayed in that state more than 5 seconds (or whatever) you can jump to another state - maybe start again from the top if one response is missng.
Thank you for the responses. Robin2 after looking at Serial Input Basics, Example 2 was exactly what I was looking for! Everything works perfect now except the timing(not as accurate as I would like).
If you post your code and your accuracy requirements, then we can help. There may be another method that will work better for you.
However, if it works, then it's probably time to stop playing with it and move on to the next project. Make sure you make an archive copy of this "adequately working" code because you may find you need to refer back to it one year from now.
There aren't exactly any accuracy requirements. When I had the following code set to an interval of 900000 the arduino was losing somewhere between .5 and 1 second each loop. So I was thinking about using an RTC to pulse every 15 minutes. I would actually like to combine the two sets of code in the future.