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