My experimenting with the
Arduino board is continuing even if I'm not gone so far from trying
provided example sketches. That's why I'm writing so little about it.
I recently got a couple of LCD display modules based on the Hitachi
HD44780 chip, that is the most widely used character LCD
standard.
Setting up the circuit
Connection the Arduino to
a LCD display module is quite easy, four wires carry the data while
two more (Enable and R/S) handle the control signals. Of course you
can use the Arduino digital pins you prefer to carry the task but
using pins 5,4,3 and 2 for data signals and pins 12 and 11 fro R/S
and Enable pin respectively will let you test the provided examples
without modification. Apart from connecting wires the only extra
components needed are a couple of trimmers (small variable resistors)
for LCD contrast and back-light intensity adjustment. Here how the
whole circuit looks like (not different from what you can find on
Arduino site
examples.)
“Hello-worlding”
... and a little more
A good Arduino motto
could be “There is a library for it”, among the many libraries
provided with Arduino could not miss, of course, one for devices as
widely used as LCD displays. Once the library is initialized, with
the pins used in the circuit, writing on the display is just matter
of simply calling “write()” commands. A hello-world
example sketch is already provided with Arduino software showing the
basics of LCD display writing.
I tried to go a little
further by playing with the HD44780 ability to define a handful of
custom character in order to play a little Pacman-like
animation on the display. Custom character are supported by Arduino's
LCD library and can be defined by a eight-by-five bit matrix:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make some custom characters: | |
... | |
byte pac2Def[8] = { | |
0b00000, | |
0b01110, | |
0b10100, | |
0b11000, | |
0b11100, | |
0b01110, | |
0b00000, | |
0b00000 | |
}; | |
… |
I then defined a “fill”
function to put the display in its initial status (filled with
“pills”)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… | |
// Initial display fill | |
void fill() | |
{ | |
lcd.setCursor(0,0); | |
lcd.write(pac1); | |
for(int j=0;j<7;j++) | |
{ | |
lcd.write(" "); | |
lcd.write(pill); | |
} | |
lcd.setCursor(0,1); | |
lcd.write(pill); | |
for(int j=0;j<7;j++) | |
{ | |
lcd.write(" "); | |
lcd.write(pill); | |
} | |
} | |
… |
and an “anim()”
function to do all the animation, and previous position clearing,
work:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// character animation | |
void anim() | |
{ | |
lcd.setCursor(px,py); | |
lcd.write(" "); | |
lcd.setCursor(x,y); | |
lcd.write(pac1); | |
delay(del); | |
lcd.setCursor(x,y); | |
lcd.write(pac2); | |
delay(del); | |
px = x; | |
py = y; | |
} | |
... |
The sketch's “setup()”
method so just initialize the custom characters and the display and
calls the fill function while the main “loop()” method
calls the animation function and update the Pacman position. Here is
the full sketch code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* LCD custom character demo */ | |
// 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); | |
// make some custom characters: | |
byte pac1Def[8] = { | |
0b00000, | |
0b01110, | |
0b11011, | |
0b11111, | |
0b11111, | |
0b01110, | |
0b00000, | |
0b00000 | |
}; | |
byte pac2Def[8] = { | |
0b00000, | |
0b01110, | |
0b10100, | |
0b11000, | |
0b11100, | |
0b01110, | |
0b00000, | |
0b00000 | |
}; | |
byte pillDef[8] = { | |
0b00000, | |
0b00000, | |
0b00000, | |
0b01100, | |
0b01100, | |
0b00000, | |
0b00000, | |
0b00000 | |
}; | |
const byte pac1 = 0x0; | |
const byte pac2 = 0x1; | |
const byte pill = 0x2; | |
const int del = 250; | |
int x = 0; | |
int y = 0; | |
int px = 0; | |
int py = 0; | |
void setup() { | |
// create a new character | |
lcd.createChar(0, pac1Def); | |
// create a new character | |
lcd.createChar(1, pac2Def); | |
// create a new character | |
lcd.createChar(2, pillDef); | |
// set up the lcd's number of columns and rows: | |
lcd.begin(16, 2); | |
// fill the display | |
fill(); | |
} | |
void loop() { | |
anim(); | |
x++; | |
if(x>15 && y == 0) | |
{ | |
x = 0; | |
y = 1; | |
} | |
else if(x>15 && y == 1) | |
{ | |
x = 0; | |
y = 0; | |
fill(); | |
} | |
} | |
// Initial display fill | |
void fill() | |
{ | |
lcd.setCursor(0,0); | |
lcd.write(pac1); | |
for(int j=0;j<7;j++) | |
{ | |
lcd.write(" "); | |
lcd.write(pill); | |
} | |
lcd.setCursor(0,1); | |
lcd.write(pill); | |
for(int j=0;j<7;j++) | |
{ | |
lcd.write(" "); | |
lcd.write(pill); | |
} | |
} | |
// character animation | |
void anim() | |
{ | |
lcd.setCursor(px,py); | |
lcd.write(" "); | |
lcd.setCursor(x,y); | |
lcd.write(pac1); | |
delay(del); | |
lcd.setCursor(x,y); | |
lcd.write(pac2); | |
delay(del); | |
px = x; | |
py = y; | |
} |
Here is, at last the
whole thing running: as usual the real circuit is a lot messy than
what I draw on a computer …
It looks a lot like the
games I used to write with my first
computer when I was a teenager … may be this is why I love
Arduino.
We also love arduino:
ReplyDeletehttp://www.youtube.com/watch?v=f30P3lXkI64&feature=youtu.be
Thanks for visiting Pat
Delete