Oh well, after hooking up my 4x20 LCD (that was held over from the Beagle Bone AI project, that I could never get to work) it still failed on my Arduino Mega. I ordered one, off eBay. The first thing I did was to run the test program. Vola it worked.
The next piece of business was to get formatted text to the display. Now as we all know Arduino does not support the C library function printf, which will sends formatted text to the console. Using printf is straightforward with these conventions: int printf( const *format, ......) here is it used to print the ASCII table
printf("ASCII value = %d, Character =%c\n", ch, ch ); you will note that the return int is optional.
But the Arduino you can't send the message to the LCD display without doing this. My function displayWrite( int row, char[] ) handles writing to either the LCD (during testing) or the LED displays. Furthermore, I can also wrap my library #include statements.
sprintf( line, "TANK #%d - %d", tank, lbs );
displayWrite ( 2, 0, line);
~~
void dispalyWrite( int row, int col, char[] ) {
lcd.setCursor( col, row );
lcd.print( line ); }
Since I will only be using the LCD for testing and a pair of HDSP-2131 (HDSP-2111) LED Display 5x7 8 characters that are part of the fuel display, and I don't want to rip my code when I change displays. Let me introduce a neat feature of the C preprocessor which are #ifdef, #ifndef, #elseif, #elif, #else, #endif. I use them so I don't have to rewrite my display function ie. over the years I have gotten into the habit of writing modularized code. The star of the show is #ifdef. This directive works by looking at the term after the directive. If that term has not been #defined it will pass over this code block until an #else, #endif is encountered. Here is a nice reference on the #directives.
#define displayLCD
#ifdef displayLCD
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#endif
void dispalyWrite( int row, int col, char[] ) {
#ifdef displayLCD
lcd.setCursor( col, row );
lcd.print( line );
#else
/// Code to drive the HDSP-2111 w/o shift reg
#endif
} // this closes the function.
Top Comments