Code:
/*
NKC Electronics serial display - Custom Characters
Demonstrates how to add custom characters on an LCD display.
This sketch prints "I <heart> Arduino!" and a little dancing man
to the LCD.
The circuit:
* LCD SCL (pin 7 or pin 3 on J2) pin to Arduino SCL pin or Analog 5
* LCD SDA (pin 8 or pin 4 on J2) pin to Arduino SDA pin or Analog 4
* LCD VSS (pin 9 or pin 5 on J2) pin to GND
* LCD VDD (pin 10 or pin 6 on J2) pin to +5V
Based on the original CustomCharacter example from Arduino
*/
#include <Wire.h>
char ESC = 0xFE; // escape character to send commands to LCD
char addr = 0x28; // default i2c address
// make some custom characters:
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
byte smiley[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b10001,
0b01110,
0b00000
};
byte frownie[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b00000,
0b01110,
0b10001
};
byte armsDown[8] = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp[8] = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
void setup() {
Wire.begin();
// create a new character
createChar(0, heart);
// create a new character
createChar(1, smiley);
// create a new character
createChar(2, frownie);
// create a new character
createChar(3, armsDown);
// create a new character
createChar(4, armsUp);
// Initialize LCD module
// Display On
Wire.beginTransmission(addr);
Wire.write(ESC);
Wire.write(0x41);
// Clear Screen
Wire.write(ESC);
Wire.write(0x51);
// Set Contrast
Wire.write(ESC);
Wire.write(0x52);
Wire.write(40);
// Set Backlight
Wire.write(ESC);
Wire.write(0x53);
Wire.write(8);
// Write some text to the display
Wire.write("I ");
Wire.write((byte)0);
Wire.write(" Arduino! ");
Wire.write(1);
Wire.endTransmission();
}
void loop() {
// read the potentiometer on A0:
int sensorReading = analogRead(A0);
// map the result to 200 - 1000:
int delayTime = map(sensorReading, 0, 1023, 200, 1000);
// set the cursor to the bottom row, 5th position:
Wire.beginTransmission(addr);
Wire.write(ESC);
Wire.write(0x45);
Wire.write(0x44);
// draw the little man, arms down:
Wire.write(3);
Wire.endTransmission();
delay(delayTime);
Wire.beginTransmission(addr);
Wire.write(ESC);
Wire.write(0x45);
Wire.write(0x44);
// draw him arms up:
Wire.write(4);
Wire.endTransmission();
delay(delayTime);
}
void createChar(byte c, byte *d)
{
Wire.beginTransmission(addr);
Wire.write(ESC);
Wire.write(0x54);
Wire.write(c);
Wire.write(d, 8);
Wire.endTransmission();
}