I retrieve the RSSI value of the BLE under the loop. I would like to make the buzzer beep upon receiving the required responses as a result of executing the AT commands. However, if I were to assign Serial.read() to a char variable, I cannot evaluate the condition that contains the tone() codes in its block.
char inData[] = "";
int index = 0;
String inString = "";
String inParse[] = "";
byte charReceived = 0;
String responseString = "";
int responseLength = 0;
int inDataIndex = 0;
int rssi;
void setup() {
Serial.begin(115200);
}
void serialEvent() {
while(Serial.available()) {
char inChar = Serial.read(); //if comment this 1
inData[index] = inChar; // and this 2
index++;
inString += inChar; //and this 3, buzzer will beep
if (Serial.read() == 13) { // \r cannot be detected if I were to use buzzer. Use 13
playAlarm();
//delay(1000);
index = 0;
responseLength = strlen(inData) - 1; //Get rid of LF -1
if(inData[0] != '-') //get rid of the first garbage) //inData[0]
inDataIndex = 1;
else
inDataIndex = 0;
for(int i=inDataIndex; i<responseLength; i++)
responseString += inData[i];
rssi = responseString.toInt(); //parse string to int
if(responseString)
responseString="";
}
}
}
void loop(){
Serial.println("AT+RSSI=?");
/*Some people suggests that it is best to retreive RSSI value
* every 2 seconds. Anything faster than that, will make the RSSI
* value too incnsistent
*/
delay(150); //Must put delay. or else, it will return CMD error
}
void playAlarm() {
tone(8, 2100, 150); //21000 //Above is 9900 useless
noTone(8);
tone(8, 1000, 150);
}
void stopAlarm() {
noTone(8);
}
Its always an error to read more characters than are available:
while(Serial.available()) {
char inChar = Serial.read(); //if comment this 1
inData[index] = inChar; // and this 2
index++;
inString += inChar; //and this 3, buzzer will beep
if (Serial.read() == 13) { // \r cannot be detected if I were to use buzzer. Use 13
should be changed to something like this:
while(Serial.available()) { // pass this test, at least 1 char is available.
char inChar = Serial.read();
if (index < MAXCHARS) // check for buffer over-run.
{
inData[index++] = inChar;
}
inString += inChar; // why use string and String?
if (inChar == 13) { // compare the character actually read
and change
char inData[] = "";
to
#define MAXCHARS 50 // or whatever is sensible
char inData[MAXCHARS+1] ;
@PaulS - I am sorry if it sounds so newbie, and have not reach your level yet. I just started and still learning. I got the sample codes somewhere and I simply copied it and did some changes to it. The codes work pretty well if I were to use LCD display instead of the buzzer though.
@MarkT - Thank you so much!! I got the buzzer working finally! I think I understand the rationale behind your modifications. Yeap. You hit on the nail. Before I posted these codes, I know there is 1 or 2 lines that is not relevant but I can't seem to find it as I was pretty in mess. You are right. I don't need the "inString"
Can you explain this line? inData[index++] = inChar; If I understand correctly, it is a form of post-increment. So, the counter will automatically increase by 1 count after it store the char in one of its element? I guess its the same as