A question about Bluetooth buffers.

Hi everyone,

I’m having a slight issue with a controller I’ve made. I’ve got the arduino (pro mini) sending serial through the Bluesmirf Gold. In order to let the user know that the controller is connected, I have the master sending the arduino a “1” every 100 ms, or so. My issue comes when connection is lost. It takes almost 9 seconds for the arduino to recognize that connection is lost and to lower the LED, however it raises the LED immediately once a connection is made. I’m assuming that there is a buffer of some sort, and was wondering if anyone knows a way to clear it. Or, alternately, a different way to light the LED if a one is being read. I chose to go with an actual number and not just the if(bluetooth.available() as a fail safe. The reason for the counting is so that the LED doesn’t flicker. So if anyone has an idea on how to prevent flickering while also not building a buffer, please let me know!
Many thanks!
Here’s the bit of code I’m working with:

void loop(){ 
  char btc = bluetooth.read();
  unsigned long startTime = millis();
  if (btc == '1'){
   elapsedTime = millis() - startTime;
    Serial.println(elapsedTime);
    if(elapsedTime < 1000){
      digitalWrite(blue,HIGH);
      }else{ 
        digitalWrite(blue,LOW);
        Serial.println(elapsedTime); 
      }
  }else{
    elapsedTime = millis();
    if(elapsedTime > 1000){
      digitalWrite(blue,LOW);
      Serial.println(elapsedTime);   
    }
  }

I can't immediately make sense of your program and I think you can achieve what you want more simply.

Look at this snippet

btnVal = digitalRead(btnPin);
if (btnVal == HIGH) {    // assumes btn is LOW when pressed
   lastBtnPressMillis = millis();   // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
   // button has been pressed for longer than interval
}

and imagine replacing btnVal with the result of your Serial.read()/

Another approach (and maybe better) is to count the number of times the received data is "wrong" and if the count gets to some limit assume that the connection is lost. On the other hand if the "right" value is received set the counter back to 0.

...R

Thanks for the speedy response!

I was just considering the "count wrong" method. Ha!

I'll let you know how it goes. Thanks again!

It looks like the buffer is in the airbus that I'm using and not the bluetooth mod. No way around it then. Looks like the client will just have to wait 10 seconds to see if the problem is a connection one. Cest la vie.

  unsigned long startTime = millis();
  if (btc == '1'){
   elapsedTime = millis() - startTime;

Since you are setting 'startTime' to millis() just before subtracting it from millis() your answer is almost always going to be 0 and,rarely, 1. It will NEVER be >1000. You should add the 'static' keyword to your 'startTime' so it doesn't get reset every time through loop().

  static unsigned long startTime = millis();
  if (btc == '1'){
   elapsedTime = millis() - startTime;