0
votes

I am working on a project in which I use a phone app that I built in order to use Google's Speech Recognizer, connect my phone with my Arduino via Bluetooth and then when I say a word it sends the word in order to display it in a LCD.

The phone App works great with no problems. The problem is in the Arduino code. When I say the word hello for example the Arduino receives ello. I know that it receives it because I also use the Serial monitor to display the data in my computer screen except the LCD. Then after Arduino receives the first chunk of data if I send a second word like world Arduino receives elloorld. So it not only misses again the first letter of the word but also the Serial Port is not empty it in the end of the loop.

I tried with data += c; instead of data.concat(c); and the difference is that the second word isn't elloorld and it is just orld

Here is my code:

#include <LiquidCrystal.h> 

LiquidCrystal lcd(12, 11, 9, 8, 7, 6, 5, 4, 3, 2);

char c;
String data = "";

void setup() {
  lcd.begin(16, 2);

  Serial.begin(9600);
}

void loop() {
  lcd.clear();  //clean the lcd
  lcd.home();   // set the cursor in the up left corner

  while(Serial.available() > 0){
    c = Serial.read();
    data.concat(c);
  }

  if(data.length() > 0){
    Serial.println(data);
  }

  lcd.print(data);

  delay(3000);

  data = "";
}

If in the end of the loop I try to clean the Serial Port with this code:

while(Serial.available() > 0){
  Serial.read();
}

Then the arduino doesn't receive data at all.

1
What if you remove that android SpeechToText to Bluetooth gimmick and test it with SerialMonitor?datafiddler
It's dead for 3 seconds until you clear data and start over again. Is that ok for you? Is there a reason for this?datafiddler
If i disconnect the the bluetooth module and use the serial monitor it displays the whole word but in the end it adds 2 ΞΞ . i.e. helloΞΞ.vamoirid
the delay function is just to see for 3 seconds the word in the lcd. Should i do it with a flag instead so as not to use the delay?vamoirid
helloΞΞ is probably showing CR / LF added by SerialMonitor- You can control this in SerialMonitordatafiddler

1 Answers

1
votes

Your code wakes up every 3000 ms, then processes everything that is pending in the Serial input buffer and falls asleep again.

If you remove that ugly String data and the ugly delay(3000) and the unnecessary while, you can try this simple loop:

unsigned long lastreceived;
void loop() {
  if (Serial.available()) {
     lcd.write(Serial.read());
     lastreceived=millis();
  }
  if (millis() - lastreceived > 1000) {
    // after one second of silence, prepare for a new message
    lcd.clear();
    lcd.home();
    lastreceived=millis(); // don't clear too often
  }
}