Go Down

Topic: Waiting for a byte to received in the Serial buffer before moving on (Read 1 time) previous topic - next topic

jerseyguy1996

#5
Aug 05, 2012, 02:08 am Last Edit: Aug 05, 2012, 02:10 am by jerseyguy1996 Reason: 1


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:

Code: [Select]
//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:

Code: [Select]
//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);
}


Arduino Uno;
Mega328

P18F4550

#6
Aug 05, 2012, 03:03 am Last Edit: Aug 05, 2012, 03:07 am by P18F4550 Reason: 1
Code: [Select]
while(Serial.available()>0)
{
// do an action here

}

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

jerseyguy1996


Code: [Select]
while(Serial.available()>0)
{
// do an action here

}

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.
Arduino Uno;
Mega328

P18F4550

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

Go Up