ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Hello World for Freetronics LCD

The LCD class provides an enhanced version of the standard Arduino LiquidCrystal library that supports the additional features of the Freetronics LCD shield; namely the back light and the Up, Down, Left, Right, and Select buttons. This tutorial explains how to use the LCD class to perform basic text output and to use the enhanced shield features.

HelloWorld.png

We start by including the library and initializing it:

#include <LCD.h>
LCD lcd;

Unlike the LiquidCrystal library we don't normally need to specify the pin assignments for this shield. The one exception is when the shield is used with the USBDroid and the D9 pin is reassigned as described on this page. In that case, the initialization sequence would look something like this instead:

LCD lcd(A1);

The next step is to enable the screen saver and print some text in the setup function:

void setup() {
lcd.print("hello, world!");
}

The screen saver is a built-in feature of the LCD class that turns off the display and the back light after a specific timeout (the default is 10 seconds). Pressing any of the keys on the shield or calling LCD::display() will wake up the screen again.

In the program's loop function we print the number of seconds since startup to the second line of the LCD display:

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);

We then print the name of the button that is currently pressed:

lcd.setCursor(8, 1);
int button = lcd.getButton();
if (button == LCD_BUTTON_LEFT)
lcd.print("LEFT");
else if (button == LCD_BUTTON_RIGHT)
lcd.print("RIGHT");
else if (button == LCD_BUTTON_UP)
lcd.print("UP");
else if (button == LCD_BUTTON_DOWN)
lcd.print("DOWN");
else if (button == LCD_BUTTON_SELECT)
lcd.print("SELECT");
else if (button < 0) // button release
lcd.print(" ");
}

The LCD::getButton() function returns the key that has been pressed or released, or LCD_BUTTON_NONE if no key has been pressed or released this time through the loop.

The full source code for the example follows:

/*
This example demonstrates how to use the LCD library, which extends the
standard LiquidCrystal library to provide support for the Freetronics back light
and Up/Down/Left/Right/Select buttons. More information on the shield here:
http://www.freetronics.com/pages/16x2-lcd-shield-quickstart-guide
This example is placed into the public domain.
*/
#include <LCD.h>
LCD lcd;
// Note: if you are using the USBDroid and have reassigned pin D9 on the LCD shield to some
// other pin (e.g. A1), then you will need to initialize the shield with something like:
// LCD lcd(A1);
// See also: http://www.freetronics.com/pages/combining-the-lcd-keypad-shield-and-the-usbdroid
void setup() {
lcd.print("hello, world!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
lcd.setCursor(8, 1);
int button = lcd.getButton();
if (button == LCD_BUTTON_LEFT)
lcd.print("LEFT");
else if (button == LCD_BUTTON_RIGHT)
lcd.print("RIGHT");
else if (button == LCD_BUTTON_UP)
lcd.print("UP");
else if (button == LCD_BUTTON_DOWN)
lcd.print("DOWN");
else if (button == LCD_BUTTON_SELECT)
lcd.print("SELECT");
else if (button < 0) // button release
lcd.print(" ");
}