I have a problem that I can't solve. I have worked for days at it and have unsuccessfully searched the internet for anything related. This is the offending code:
void display_text()
{
lcdSerial.print(clearScreen);
delay(10);
if(textSize < 5) { //one 80-char string
// display it
for(int i=0; i<20*textSize; i++)
{
lcdSerial.print(text[i]);
}
}
else if(textSize > 4) // greater than 80-char
{
topLeft = 0;
topLeftMax = 20*(textSize-4); // 3rd line from the bottom
// display the first 80 characters
for(int i=0; i<80; i++){
lcdSerial.print(text[i]);
}
// use upArrow and downArrow to scroll through the text
do
{
while(keyboard.available() == false) {
}
inByte = keyboard.read();
Serial.println(inByte,DEC); // for debugging
if(inByte == upArrow && topLeft > 0){
topLeft-=20;
}
else if(inByte == downArrow && topLeft < topLeftMax){
topLeft +=20;
}
lcdSerial.print(cursorHome);
for(int i=topLeft; i<topLeft+80; i++){
lcdSerial.print(text[i]);
}
}
while(inByte != PgDn); // to go for a response
}
}
text[] is a string of 320 characters to be displayed, with scrolling, on a 4 X 20 LCD. The first 80 characters are first placed on the LCD and then the upArrow and downArrow keys are supposed to allow scrolling through the text.
The problem is that the inByte read from the keyboard gives the correct code only about once every three keystrokes. On the other keystrokes it gives sometimes one, sometimes two incorrect codes with no pattern that I can perceive. The keyboard works correctly when I test it in another simple sketch. Strangely (to me) the keyboard will work correctly in this routine if I comment out the second last and third last lines of code. I am using a Arduino Uno and thinking that I was running out of SRAM I switched to an ATMega1280 but the problem persists. The keyboard is handled by PS2Keyboard.h and the LCD by NewSoftSerial.h.
What is going on here????
("textSize" in the above code is the number of preformatted 20-character lines of text.)