Anda di halaman 1dari 20

Figure 14-5.

The LCD News Reader Fritzing diagram


Example 14-1. The LCD News Reader sketch
/*
The LCD News Reader
20 August 2013
*/
// include the LCD library code:
#include <LiquidCrystal.h>
// set up pins on Arduino for LCD and test lead
LiquidCrystal lcd(12,11,5,4,3,2);

// set up the LCD's number of columns and rows


#define Xdelay 1900
String
String
String
String

a;
b;
c;
d;

Chapter 14: The LCD News Reader

123

void setup() {
lcd.begin(16,2);
lcd.setCursor(0,0);

clearLCD();
backlightOn();
lcd.print("HELLO, WORLD!");
delay(Xdelay);
}

void loop()
{
char databuff[16];
char dispbuff[16];
// display on/off test
for(int x = 5; x>0; x--)
{
delay(1000);
displayOff();
delay(1000);
displayOn();
}

clearLCD();
backlightOn();
lcd.print("SLOW FADE
fadeOut(100);
fadeIn(10);

");

// light up all segments as a test

lcd.print("0123456789abcdef");
delay(Xdelay);
lcd.print("ghijklmnopqrstuv");
delay(Xdelay);
lcd.print("wxyz +?*&%$#()!=");
delay(Xdelay);
lcd.print("
");
delay(Xdelay);
lcd.print("
");
delay(Xdelay);
a
b
c
d

124

=
=
=
=

"0123456789abcdef";
"ghijklmnopqrstuv";
"wxyz +?*&%$#()!=";
"
";

Make: Basic Arduino Projects

selectLineTwo();
lcd.print(a);
delay(Xdelay);
selectLineOne();
lcd.print(a);
selectLineTwo();
lcd.print(b);
delay(Xdelay);
selectLineOne();
lcd.print(b);
selectLineTwo();
lcd.print(c);
delay(Xdelay);
selectLineOne();
lcd.print(c);
selectLineTwo();
lcd.print(d);
delay(Xdelay);
selectLineOne();
lcd.print(d);
selectLineTwo();
lcd.print(d);
delay(Xdelay);

for (int x = 0; x<=5; x++)


{
for(int i = 15; i>=0; i--)
{
goTo(i);
if (i%4 == 1)
lcd.print("- ");
if (i%4 == 2)
lcd.print("I ");
if (i%4 == 3)
lcd.print("- ");
if (i%4 == 0)
lcd.print("I ");
delay(100);
}
for(int i =0; i<=14; i++)
{
goTo(i);
lcd.print(" @");
delay(100);
}
}
clearLCD();
}

Chapter 14: The LCD News Reader

125

void selectLineOne()
{
lcd.write(0xFE);
//command flag
lcd.write(128);
//position
delay(10);
}
void selectLineTwo()
{
lcd.write(0xFE);
//command flag
lcd.write(192);
//position
delay(10);
}
void goTo(int position)
{
if (position<16)
{
lcd.write(0xFE);
//command flag
lcd.write((position+128));
//position
}else if (position<32)
{
lcd.write(0xFE);
//command flag
lcd.write((position+48+128));
//position
} else { goTo(0); }
delay(10);
}
void clearLCD()
{
lcd.write(0xFE);
lcd.write(0x01);
delay(10);
}
void backlightOn()
{
lcd.write(0x7C);
lcd.write(157);
delay(10);
}
void backlightOff()
{
lcd.write(0x7C);
lcd.write(128);
delay(10);
}

//command flag
//clear command

//command flag for backlight stuff


//light level

//command flag for backlight stuff


//light level for off

void backlightValue(int bv)


{
int val = bv;
if (bv < 128) val= map(bv, 0, 1023, 128, 157);
if (bv > 157) val = map(bv, 0, 1023, 128, 157);
lcd.write(0x7C);
lcd.write(val);
delay(10);

126

//command flag for backlight stuff


//light level

Make: Basic Arduino Projects

void displayOn()
{
lcd.write(0xFE);
lcd.write(0x0C);
delay(10);
}

//command flag
//clear command

void displayOff()
{
lcd.write(0xFE);
lcd.write(0x08);
delay(10);
}

//command flag
//clear command

void fadeOut(int fade)


{
for (int x = 157; x>128; x--)
{
backlightValue(x);
delay(fade);
}
}
void fadeIn(int fade)
{
for (int x = 128; x<=157; x++)
{
backlightValue(x);
delay(fade);
}
}

Chapter 14: The LCD News Reader 127

Figure 14-7. The LCD News Reader block diagram

Figure 14-8. The LCD News Reader circuit schematic diagram

Something to Think About


How can a pushbutton switch be used to control the display?

Chapter 14: The LCD News Reader

129

Figure 15-2. Fritzing diagram for a logic tester with an RGB LED

Upload the Logic Tester Sketch


With the Logic Tester built, its time to upload the sketch. As shown in
Example 15-1, the sketch operates an RGB LED using a pushbutton switch and two
fixed resistors. Here are the steps youll need to follow:
1. Attach the Arduino to your computer using a USB cable.
2. Open the Arduino software and type Example 15-1 into the softwares text
editor.
3. Upload the sketch to the Arduino.
Once the Logic Tester sketch has been uploaded to the Arduino microcontroller, the
RGBs red LED will be on, as shown in Figure 15-1. Attaching the long test wire to
the +5VDC source on the MakerShield and pressing the pushbutton switch will allow
the RGB green LED to turn on, as shown in Figure 15-3.
Example 15-1. The Logic Tester sketch
/*
Logic Tester with RGB LED
Turns on the green LED when a logic "1" (+5V) signal is detected. The
red LED will turn on at logic "0" (0V) signal. Also, when powering
up the Arduino the red LED is on.
4 May 2013
Don Wilcher
*/

Chapter 15: A Logic Tester (with an RGB LED)

133

Figure 15-5. The Logic Tester Fritzing circuit schematic diagram

Something to Think About


How can the Logic Tester be operated without a pushbutton switch?

136

Make: Basic Arduino Projects

Figure 16-1. A Logic Tester with an LCD

Lets Build a Logic Tester


Building this tester requires the use of an LCD. If this is your first time using an LCD,
I suggest reading Chapter 14. For help adding the 16-pin male header to the LCD,
see Figure 14-2 and Figure 14-3. The 10K potentiometers center pin is wired to
pin number 3 of the LCD. The potentiometers remaining pins should be wired to
+5VDC and ground. Place the LCD onto the solderless breadboard, as shown in
Figure 16-2. LCD pin numbers 1 and 2 are wired to ground and +5VDC, respectively.
Adjust the 10K potentiometer contrast control for the LCD for proper pixel-square
visibility. For reference on how to do this, see Figure 14-4.
Complete the rest of the tester wiring using the Fritzing diagram shown in
Figure 16-2.

138

Make: Basic Arduino Projects

+5V source, as shown in Figure 16-3. Impress the local Makerspace by testing Arduino and digital electronic circuits with your Logic Tester!
Example 16-1. The Logic Tester sketch
/*
Logic Tester
LCD displays "HIGH (1)" when digital circuit signal is +5V. A "LOW (0)"
is displayed when digital circuit signal is OV.
27 April 2013
Don Wilcher
*/
// include the LCD library code:
#include <LiquidCrystal.h>
// set up pins on Arduino for LCD and transistor lead:
LiquidCrystal lcd(12,11,5,4,3,2);
int xistorPin = 6;
int digitalStatus = 0;
// variable for reading the digital circuit state

// initialize the transistor pin as an input and set up the LCD's number
// of columns and rows:
void setup() {
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("LOGIC TESTER");
pinMode(xistorPin, INPUT);
}

void loop() {
// check if digital signal is HIGH or LOW:
digitalStatus = digitalRead(xistorPin);
if (digitalStatus == HIGH) {
// if digital circuit signal is +5V, display HIGH (1):
lcd.setCursor(0,1);
lcd.print("HIGH (1) "); // display HIGH (1)
}
else {
// if digital circuit signal is 0V, display LOW (0):
lcd.setCursor(0,1);
lcd.print(" LOW (0) ");
}
}

140 Make: Basic Arduino Projects

Anda mungkin juga menyukai