Go Down

Topic: New LCD SerialDisplay Example (Read 1 time) previous topic - next topic

mjspock

Hi,
I noticed while experimenting with my 4x16 LCD that SerialDisplay example doest handle very well multiline LCD's.
Below is fixed version that can handle any type of LCD's and have good line breaking for new/next row.
Plase check it and maybe replace example in  Arduino software examples. It works great for me for any strings.
Of course works great for me :)

Here is updated code:
Quote

/*
  LiquidCrystal Library - Serial Input
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the 
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch displays text sent over the serial port 
 (e.g. from the Serial Monitor) on an attached LCD.
 
 The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe 
 modified 22 Nov 2010
 by Tom Igoe
 modified 29 Apr 2011
 by Marcin Jurczuk
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// your LCD rows number
int LCD_ROWS = 4;
// your LCD columns number
int LCD_COLS = 16;
// temporary data buffer
int dataFromSerial;
// offset is used to calculate proper line breaking
int lcdOffset = 0 ;
// current row
int row = 0;
// this is used for bug fixing some lcd's
int cursorRow = 0;
void setup(){
    // set up the LCD's number of columns and rows:
  lcd.begin(LCD_COLS, LCD_ROWS);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    lcd.setCursor(0,0);
    // we're starting on screen begining
    lcdOffset=0;
    // if there is new line and space char (0x20) is next to display
    // we want to delete it so text will look much better
    bool newLine = false;
    // start processing data from serial
    while (Serial.available() > 0) {
      //read byte from buffer
      dataFromSerial = Serial.read();
      lcdOffset++;
      // Here we are calculating if we will write to new LCD row
      // If yes - move cursor,skip space if it is first char
     // and fix bugs on some LCD's
      for(int colNo = 1;colNo<4;colNo++) {
      if(lcdOffset == (LCD_COLS*colNo+1)) {
        // my LCD is broken and for rows > 2 needs negative (sic!)
        // cursor col positions by -4. If you have "broken" LCD
        // uncomment this line
        //if ( colNo > 1 ) { cursorRow = -4; } else { cursorRow = 0;};
        lcd.setCursor(cursorRow,colNo);
        newLine = true;
      }
      }

      if (!(newLine && dataFromSerial == 0x20)) {
      lcd.write(dataFromSerial);
      }
      // there was new line and space char - reduce offset
      // increased at the begining since we didn't print anything
      else { lcdOffset--; }
      // clear all data
      newLine=false;
    } // end of data processing
  }
}


Chrisprols

Hi !

I'm getting a screen shield, myself.
could you give some feedback from your experience with screens. How did the code end up ? how did it work ? Did you do some other projects using the display ?

Thanks !


   christophe.

Go Up