Hardware Reference
In-Depth Information
lcd.createChar(2, p60);
lcd.createChar(3, p80);
lcd.createChar(4, p100);
}
void loop()
{
//Move cursor to second line
lcd.setCursor(0,1);
//Clear the line each time it reaches the end
//with 16 " " (spaces)
lcd.print(" ");
//Iterate through each character on the second line
for (int i = 0; i<16; i++)
{
//Iterate through each progress value for each character
for (int j=0; j<5; j++)
{
lcd.setCursor(i, 1); //Move the cursor to this location
lcd.write(j); //Update progress bar
delay(100); //Wait
}
}
}
At the beginning of each pass through the loop, the 16-character-long string
of spaces is written to the display, clearing the progress bar before it starts again.
The outer for() loop iterates through all 16 positions. At each character posi-
tion, the inner for() loop keeps the cursor there and writes an incrementing
progress bar custom character to that location. The byte cast is not required here
because the ID 0 is defined by the j variable in the for() loop .
NOTE Towatchademovideooftheupdatingprogressbar,visit
www.exploringarduino.com/content/ch10 .Youcanalsofindthis
videoontheWileywebsiteshownatthebeginningofthischapter.
BuildingaPersonalThermostat
Now, let's make this display a bit more useful. To do so, you add the temperature
sensor from Chapter 8, “The I 2 C Bus,” a fan, and the speaker from Chapter 5,
“Making Sounds.” The display shows the temperature and the current fan state.
When it gets too hot, the speaker makes a noise to alert you, and the fan turns
on. When it gets sufficiently cool again, the fan turns off. Using two pushbuttons
and the debounce code in Listing 2-5 in Chapter 2, “Digital Inputs, Outputs,
and Pulse-Width Modulation,” you add the ability to increment or decrement
the desired temperature.
 
Search WWH ::




Custom Search