while(Serial.available()) Serial.read(); //Clear the buffer
That way when you get to your wait there is definately nothing already there.
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);
}