Hi all,
I hope that someone can spot the problem with what should be a simple application.
I have a ProMini (3.3v 8Mhz) connected to an accelerometer and HC-05 bluetooth module, with the intention of sending motion data to an Android phone. The phone app was written using MIT App Inventor.
Every few seconds, the Android app sends an 'S' (actually sends 83) to the promini, which then replies with a single item of data, which is a floating point number. Weirdly, I am getting the 'previous' result...
To try and figure it out, I've written simple programs without all of the accelerometer code (which works fine), just sending a random integer, and see this:
Value to be sent by ProMini Value received by Android
56 nothing
43 56
92 43
12 92
etc....
I find that, if the Android app waits for around 1000mS after sending the 'S', before checking for incoming data, it works correctly. I know that Arduino serial is a bit slow, but.....
As the accelerometer is using the normal serial rx/tx pins, I use the SoftwareSerial library for the bluetooth.
Oddly, if I change the arduino code and upload, the first poll returns the last value seen in the previous session, suggesting that the number is lurking in a buffer somewhere...In the above example, I would receive '12' in the first poll of the new session. Sometimes I receive 2 values together.
Also - when I leave it running for some time, I see that there are several 'no replies', apparently at random.
I've tried alternative methods such as BTserial.readString, to no avail.
Here is the arduino code - I can post the blocks for the MIT AI app if it's of use?
Thanks in advance for any clues offered...
// BT test
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX, go to TX, RX respectively of bt device hc-05
String msgToSend="22";
char incomingMsg ;
void setup(void)
{
Serial.begin(9600);
Serial.println("hello");
BTserial.begin(9600);
} // end of setup
void loop(void)
{
msgToSend="x";
while (BTserial.available() > 0){
incomingMsg = BTserial.read(); //read a byte sent by android
Serial.println (incomingMsg);
}
if (incomingMsg == 'S') { // send data to android via BT
msgToSend= String(random(100));
Serial.println (msgToSend);
BTserial.print (msgToSend);
incomingMsg="X";
}
}// end of main loop