Found this website -- LCD Tutorial for interfacing with Microcontrollers: Checking LCD busy flag - Rickey's World of Microcontrollers & Microprocessors -- and tried to adjust my lcdBusy() function accordingly. I'm still hitting a message limit, though. So, either it's not correct and not doing what I think it's doing or it is correct and I have a buffer/flow control problem in another segment of the data path.
It seems if I'm waiting to grab the next message string from the Android (meetAndroid.getString(msgText);) until after I've made sure the LCD isn't busy, the only other place I can see these messages backing up and causing overflow is the buffer in the Arduino that holds the new string from the getString which is then copied into my char string/array variable msgText. If new messages are accumulating in that buffer rather than overwriting previous, no-longer-needed messages, then the overflow could be happening with the Arduino SRAM. I've looked for places where I could reset/flush this buffer and the only one that seems directly accessible from my code is a function in the MeetArduino library called meetArduino.flush(), which I've place right after the getString command puts the latest string into msgText -- but it doesn't seem to do anything.
Any idea how to check if that buffer is actually getting cleared?
Latest version of my lcdBusy function:
boolean lcdBusy () {
int busyFlag;
pinMode(9, INPUT); // set DB7/Pin 9 to input to be able to get busy flag status
digitalWrite(12, HIGH); // set EN like this because of...
// http://www.8051projects.net/lcd-interfacing/busyflag.php
// I don't really understand the enable pin
digitalWrite(10, LOW); // set RS to low (for command register)
digitalWrite(11, HIGH); // set RW to high (for reading)
delay(50);
busyFlag = digitalRead (9); // check status of DB7/Pin 9
if (busyFlag == HIGH) {
return true;
}
else {
pinMode(9,OUTPUT); // reset DB7/Pin 9 back to output
digitalWrite(11, LOW); // reset RW back to low (for writing)
digitalWrite(10, HIGH); // reset RS back to high (for data register)
digitalWrite(12, LOW); // toggle EN first to low then to high because of...
digitalWrite(12, HIGH); // http://www.8051projects.net/lcd-interfacing/busyflag.php
delay(200);
return false;
}
}
Latest version of my complete code:
#include <MeetAndroid.h>
#include <LiquidCrystal.h>
#define LCD_LINES 2
#define LCD_COLUMNS 40
LiquidCrystal lcd(10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9);
/*
rs: 10
rw: 13
en: 11
db0-db7: 2-9
*/
MeetAndroid meetAndroid;
char msgText[160];
byte msgLength;
byte charCounter;
byte positionMarker;
char space = ' ';
void setup() {
lcd.begin(LCD_COLUMNS,LCD_LINES);
Serial.begin(57600);
meetAndroid.registerFunction(getMsgText, 'z');
}
void loop() {
meetAndroid.receive();
}
void getMsgText(byte flag, byte numOfValues) {
boolean lcdIsBusy = true;
while (lcdIsBusy) {
digitalWrite(13, HIGH);
delay(300);
lcdIsBusy = lcdBusy();
}
msgLength = meetAndroid.stringLength();
meetAndroid.getString(msgText);
meetAndroid.flush();
lcd.clear();
positionMarker = 1;
// When the message length is less than the number of LCD columns, the message is written
// out to the LCD, there is a pause, then the message is scrolled off to the left
if (msgLength < 42) {
for (charCounter = 0; charCounter < msgLength - 1; charCounter++) {
lcd.setCursor(charCounter, 1);
lcd.write(msgText[charCounter]);
delay(100);
}
delay(2000);
for (charCounter = LCD_COLUMNS; charCounter < msgLength + LCD_COLUMNS; charCounter++) {
for (int i = 0; i < LCD_COLUMNS; i++) {
lcd.setCursor(i, 1);
if (i + positionMarker >= msgLength - 1) {
lcd.write(space);
}
else {
lcd.write(msgText[i + positionMarker]);
}
}
++positionMarker;
delay(175);
}
}
// If the message length is longer than the number of LCD columns, the first 40 chars are
// written out to the LCD, then at char 41, the message scrolls left until the last char
// of the message scrolls off the LCD
else {
for (charCounter = 0; charCounter < LCD_COLUMNS; charCounter++) {
lcd.setCursor(charCounter, 1);
lcd.write(msgText[charCounter]);
delay(100);
}
for (charCounter = LCD_COLUMNS; charCounter < msgLength + LCD_COLUMNS; charCounter++) {
for (int i = 0; i < LCD_COLUMNS; i++) {
lcd.setCursor(i, 1);
if (i + positionMarker >= msgLength - 1) {
lcd.write(space);
}
else {
lcd.write(msgText[i + positionMarker]);
}
}
++positionMarker;
delay(175);
}
}
}
// Checks the LCD Busy Flag
boolean lcdBusy () {
int busyFlag;
pinMode(9, INPUT); // set DB7/Pin 9 to input to be able to get busy flag status
digitalWrite(12, HIGH); // set EN like this because of...
// http://www.8051projects.net/lcd-interfacing/busyflag.php
// I don't really understand the enable pin
digitalWrite(10, LOW); // set RS to low (for command register)
digitalWrite(11, HIGH); // set RW to high (for reading)
delay(50);
busyFlag = digitalRead (9); // check status of DB7/Pin 9
if (busyFlag == HIGH) {
return true;
}
else {
pinMode(9,OUTPUT); // reset DB7/Pin 9 back to output
digitalWrite(11, LOW); // reset RW back to low (for writing)
digitalWrite(10, HIGH); // reset RS back to high (for data register)
digitalWrite(12, LOW); // toggle EN first to low then to high because of...
digitalWrite(12, HIGH); // http://www.8051projects.net/lcd-interfacing/busyflag.php
delay(200);
return false;
}
}
Thanks,
Tim