Bluetooth to bluetooth mate loss of data

Hello all,

I was able to get two bluetooth mates and two arduino pro minis communicating perfectly through the serial windows using the suggested code from sparkfun on each arduino

#include <SoftwareSerial.h>  
int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
  }
 }

This worked perfectly for sending text back and forth, the next step was to be able to send commands and light a LED on the other arduino. I found this tutorial here which i used to get the LED to light up: http://www.instructables.com/id/Serial-Communications-with-Arduino/step4/Serial-LED-code/

I used this code to get the LED to light up, and then turn back off depending on an a or b entered into the serial window of the sending arduino

void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  Serial.print ((char)bluetooth.read());  
             inByte = (char)bluetooth.read();
             if(inByte == 'a') { // byte is 'a'
             digitalWrite(13, HIGH);
             Serial.println("LED - On");
             }
             else if( inByte == 'b'){// byte isn't 'a'
             digitalWrite(13, LOW);
             Serial.println("LED - off");
             }
                 if(Serial.available())  // If stuff was typed in the serial monitor
                  {bluetooth.print((char)Serial.read());
                  }
                }

This is working, but then instead of typing just an "a" or a "b" i decided to type in longer data to see what would happen. I then noticed that when I type in the word hello for instance, the receiving arduino serial would only display ello, or llo, or ell. At this point i realized that when typing just "a" or "b", sometimes the LED would not turn on or off at the first instance, sometimes i would have to enter "a" several times to get the LED to light.
It was at this point that I realized I am loosing data somehow from the sending arduino to the receiving. When I used the first set of code provided by sparkfun there was no data loss. using my modified code i am getting some data loss.
I could have my transmitting arduino always broadcast the status of the two push buttons by sending constant commands, but I would rather only transmit when a button is pushed instead of constant transmission.
Does anyone see when my mistake is at?

Thanks

Does anyone see when my mistake is at?

It starts
with
your
poor
indenting.

Use Tolls + Auto Format to make your code presentable.

It's followed up by testing that there is at least one byte to read, and then reading two or more bytes.

Thanks Paul,

I performed the Tolls Auto Format like you said and it works perfectly now!

Hello Paul,

I found this forum you commented on the bluetooth mate: Bluetooth RN-42 command mode problem [SOLVED] - Networking, Protocols, and Devices - Arduino Forum

I noticed I don't have any delay in my loop. When I get off work tonight ill add some delay and hopefully it might solve the problem

Hello again Paul,

I found this old post you wrote on capturing Serial data using started && ended: http://forums.adafruit.com/viewtopic.php?f=8&t=9918
I was able to get the code to work using a single letter or number for control without losing data like I had been before. I won't bother posting the code because that is not the purpose of this question. After trying several different methods from strings to arrays I realized that as soon as I tried to capture the data and store it in an array I would lose some of the data. I removed that part of the code and set in some delays and that is when I saw how it receives the information from the bluetooth module one piece at a time. So at this point I realized i needed some sort of way to state this is the beginning of a string, and then have a character to declare the end of the string. That is what led me to the post that i linked above.
So I have taken your example and integrated it with the bare code that the bluetooth module uses, and I am getting an error compiling, I added your code one line at a time and recompiled as I went.
Anyways I auto formatted for you :slight_smile:

#include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3
int inByte = 0;
char c =0;
int led = 13;
boolean started = false;
boolean ended = false;
char inData[24]; // Size as appropriate
byte index = 0;
#define SOP "{"
#define EOP "}"
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{ 
  pinMode(led, OUTPUT); 
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  
  bluetooth.print("$");  
  bluetooth.print("$");
  bluetooth.print("$");  
  delay(100);  
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop(){
  while(bluetooth.available() > 0) 
  {
    char inChar = bluetooth.read();
    if(inChar == SOP)
    {
      started = true;
      index = 0;
      inData[index] = '\0';
    }
    else if(inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if(index < 24-1) // Array size
      {
        inData[index++] = inChar;
        inData[index] = '\0';
      }
    }
  }

  if(started && ended)
  {
    // Parse the data in inData here...
  }
}

When I try to compile this I get this error:
v1_1_ino.ino: In function 'void loop()':
v1_1_ino:35: error: ISO C++ forbids comparison between pointer and integer
v1_1_ino:41: error: ISO C++ forbids comparison between pointer and integer

It highlights this line:
if(inChar == SOP)

When I wanted to pair the neurosky mindwave mobile 2 to the arduino via a bluetooth mate gold, it sent gibberish on my serial monitor. I checked, it had nothing to do with the baud rate. When i tried again after disconnecting my bluetooth mate gold from the arduino and shutting down the laptop, I could not send strings to the bluetooth mate gold. I can’t put it into command mode and so on.

.......... and so on, and so on.
Secret code then? Paranoia rules, I see.