Go Down

Topic: Problem scrolling LCD text (Read 542 times) previous topic - next topic

glenz

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:
Code: [Select]

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.)

floresta

lcdSerial.print(cursorHome);

Just a shot in the dark but the cursorHome (and the clearScreen) command takes about 40 X more time to complete than any other command.  Maybe you've got an timing issue.

Don

glenz

You are likely correct, Don, but the timing problem is in the "behind the scenes"  code of the various libraries involved.  I have actually removed the (cursorHome) command because the cursor was ending up in the 0,0 position anyway at the end of the previous print operation. I'm still working at it. I have sprinkled "Serial.print(millis())"  throughout the code and have noticed that the action "goes away" somewhere for up to 500ms. before coming back with an incorrect keyboard code. (??)

floresta

Quote
... but the timing problem is in the "behind the scenes"  code of the various libraries involved.


That's the price you pay when you use libraries written by someone else.


Don

glenz

I want to report that the problem is solved.  Don's comments about "timing" and "libraries" steered me in the right direction. It seems that the two libraries I was using both use interrupts and that a conflict existed. Since I already had shifted from Uno to Mega1280 I dispensed with NewSoftSerial and used one of the hardware serial ports available on the Mega instead.  Everything works as it should now, but I can't go back to the Uno as I had hoped to do.

Go Up