Having recently bought the 2004 20x4 I2C Serial LCD Module from HobbyComponents i decided to write a sketch to test its capabilities, (which i'm delighted with by the way) so this is what i came up with:
Code: Select all
/*
FILE: LCD_Test
DATE: 07/09/14
Author: JamiePhonic
VERSION: 1
Description: Demos the basic capabilities of the HobbyComponents 2004 20x4 I2C Serial LCD Module (HCMODU0010)
DEVICE PINOUT (SPI Interface):
PIN 1: GND
PIN 2: +5V
PIN 3: SDA - Connect to Arduino analogue PIN 4 (Or Digital 20 on a Mega 2560)
PIN 4: SCL - Connect to Arduino analogue PIN 5 (Or Digital 21 on a Mega 2560)
*/
//Include the SPI/IIC Library
#include <Wire.h> // Import the arduino Wire (I2C) library used by the next library to controll the LCD
#include <LiquidCrystal_I2C.h> // Import the I2C LCD control library
/* Initialise the LiquidCrystal library. The default address is 0x27 and this is a 20 character, 4 line display. Change the values to '16, 2' if you have the smaller version */
LiquidCrystal_I2C lcd(0x27, 20, 4); // Create the LCD object
// Setup function (Run once at the start)
void setup()
{
Serial.begin(9600); // Open a serial connection
delay(100); // Wait 100 milli seconds
Serial.println("Serial started"); // Print "serial started" to the console then move to a new line
/* Initialise the LCD */
Serial.println("Attempting to Initalise LCD..."); // Print whats happening to the serial monitor
delay(100); // Wait 100 milli seconds
lcd.init(); // Call the special I2C LCD initalisation function
Serial.println("LCD Initalised!"); // Update the serial monitor
lcd.backlight(); // Turn on the LCD backlight
LCD_progress_bar_Setup(); // Call the progress bar setup function (we only want to do this once)
}
/* Main program loop */
void loop()
{
LCD_Demo(); // Now run the Demo function
delay(1000); // Wait 1 second before we go again
}
void LCD_Demo()
{
// Demo data to display.
char Numbers[] = "1 2 3 4 5 6 7 8 9 0";
char* LowerAlpha[] = { "abcdefghijklmnopqrst", "uvwxyz" };
char* UpperAlpha[] = { "ABCDEFGHIJKLMNOPQRST", "UVWXYZ" };
char* Punctuation[] = { "!\"$%^&*()-=,./<>?:;'", "@#~[]{}\\" };
int delayTime = 2000; // The delay before moving to the next screen (2 seconds here)
lcd.clear();
Serial.println("Running LCD Demo"); // Print to the serial monitor whats going on
lcd.print("Running Demo..");
delay(2000);
// Lets demo numbers
lcd.clear();
Serial.println("Demoing Numbers"); // Print to the serial monitor whats going on
lcd.print("Numbers:");
lcd.setCursor(0, 2);
lcd.print(Numbers);
delay(delayTime);
// Now lets demo the lowe case alphabat
lcd.clear();
Serial.println("Demoing Lower case alphabet"); // Print to the serial monitor whats going on
lcd.print("Lower case alphabet:");
lcd.setCursor(0, 2);
lcd.print(LowerAlpha[0]);
lcd.setCursor(0, 3);
lcd.print(LowerAlpha[1]);
delay(delayTime);
// Then the upper case alphabet
lcd.clear();
Serial.println("Demoing Upper case alphabet"); // Print to the serial monitor whats going on
lcd.print("Upper case alphabet:");
lcd.setCursor(0, 2);
lcd.print(UpperAlpha[0]);
lcd.setCursor(0, 3);
lcd.print(UpperAlpha[1]);
delay(delayTime);
// Now some punctuation
lcd.clear();
Serial.println("Demoing Punctuation"); // Print to the serial monitor whats going on
lcd.print("Basic Punctuation:");
lcd.setCursor(0, 2);
lcd.print(Punctuation[0]);
lcd.setCursor(0, 3);
lcd.print(Punctuation[1]);
delay(delayTime);
// And finally a progress bar
lcd.clear();
Serial.println("Demoing a progress bar"); // Print to the serial monitor whats going on
lcd.print("Progress bar:");
for (int J = 0; J < 100; J++)
{
LCD_progress_bar(J, 0, 100, 1, 20); // Call the progress bar function to update it
delay(20); // Wait 20 milli seconds before we continue to smooth out the motion
}
lcd.setCursor(0, 3);
lcd.print("OK!");
Serial.println("Demo complete!");
Serial.println("");
}
// Sets up the custom characters used by the progress bar function.
// Copy this function and the one below to your own sektch to add a progress bar to it!
// Call the 'LCD_progress_bar_Setup' in your setup, then call the 'LCD_progress_bar' function to draw/update it
void LCD_progress_bar_Setup()
{
/* LCD Progress Bar Characters, create your custom bars */
uint8_t bar0[8] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
uint8_t bar1[8] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 };
uint8_t bar2[8] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 };
uint8_t bar3[8] = { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C };
uint8_t bar4[8] = { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E };
uint8_t bar5[8] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
/* Now create those custom characters and store them in the LCD's memory */
lcd.createChar(0, bar0);
lcd.createChar(1, bar1);
lcd.createChar(2, bar2);
lcd.createChar(3, bar3);
lcd.createChar(4, bar4);
lcd.createChar(5, bar5);
}
// Actually draws the progress bar on screen
void LCD_progress_bar(int data, int dataMinVal, int dataMaxVal, int row, int lcd_size)
{
int B = map(data, dataMinVal, dataMaxVal, 0, 6 * lcd_size);
int A = B / 6;
lcd.setCursor(A, row);
lcd.write(B % 6);
}