but the code seems to be rolling right over that. There shouldn't be anything in the serial buffer at that point in the code. On a sort of related note:
if there is nothing in the buffer, Serial.read() returns 0xFF (255).
while(!Serial.available());
is indeed correct (what you wrote is equivalent). What it suggests is that there is already something in the buffer. Before that section of code, you could try this:
while(Serial.available()) Serial.read(); //Clear the buffer
That way when you get to your wait there is definately nothing already there.
but the code seems to be rolling right over that. There shouldn't be anything in the serial buffer at that point in the code.
Perhaps there's a discrepancy between what you think 'should' be in the buffer and what's actually in the buffer. The code you posted will loop until something is available. If it exits the loop, you know a byte is available to be read.
It's not a particularly good way top handle serial port input,l but if you want your sketch to wait and do nothing until input arrives on the serial port, that while loop will do it.
PeterH:
It's not a particularly good way top handle serial port input,l but if you want your sketch to wait and do nothing until input arrives on the serial port, that while loop will do it.
What would be a better way to wait for serial data to show up? The objective is to have two arduino's talk to each other over bluetooth. To coordinate their activities I want to make sure the master device waits until the slave device has finished executing a command before sending another command. So it will send a command over bluetooth (by writing to the serial port) and then wait for the slave device to respond with a '1' (not ascii...just a byte) and then it will send the next command.
This worked! I am trying to figure out how there is anything in the Serial buffer though. This is the code for the Arduino Master:
//uses the iteadstudio bluetooth master/slave shield v 2.2
//http://imall.iteadstudio.com/development-platform/arduino/shields/im120417010.html
void setup() {
// initialize the digital pin as an output.
Serial.begin(38400);
}
void loop()
{
for(int i = 0; i < 11; i++)
{
//clear the serial buffer
while(Serial.available()) {Serial.read();}
//send the first command to the bluetooth shield
Serial.write(i);
//wait for a response from the slave device
while(!(Serial.available())){}
delay (1000);
}
}
and this is the code for the arduino slave:
//used the iteadstudio bluetooth shield slave v 2.1
//http://imall.iteadstudio.com/development-platform/arduino/shields/im120417006.html
#define led 13
void setup() {
// initialize serial:
Serial.begin(38400);
pinMode(led, OUTPUT);
// reserve 200 bytes for the inputString:
//inputString.reserve(50);
}
void loop()
{
//wait for a command
while(!(Serial.available()));
//read the command
int i = Serial.read();
//blink the led
for(int j = 0; j < i; j++)
{
digitalWrite(led,HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
//let the master know that you are ready for another command
Serial.write(1);
}
waits for one byte, >1 will wait for 2 and so on
Here is what i did to make two arduino's talk, the RX waits for 2 bytes then actions it
http://arduino.cc/forum/index.php/topic,114239.msg859538.html#msg859538
Thanks for the guidance! I will give that a try. I need the system to be fairly robust because it will be controlling all of the systems on my saltwater reef tank. I need to make sure that the command makes it through and if not it will try again a few times and finally notify me of any failure.
my program sends 2 bytes, 1st is pin number, the 2nd byte is state, a 3rd could be sent with a checksum which the RX will then send back to TX to confirm