Remove these ads by
Signing Up
Remove these ads by
Signing Up
Remove these ads by
Signing Up
PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format.
You also have the ability to customize your PDF download.
I have not used this in a while.
Try using the code below
You should include the Liquid Crystal library and maybe the Wire library.
If you are missing either of these , look at http://arduino.cc/en/Reference/Libraries .
Also make sure you wire up the pins correctly to (12, 11, 5, 4, 3, 2);
Hope this helps.
#include
#define countof(array) ( sizeof(array) / sizeof(*(array)) )
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//
int ledPin = 1; // choose the pin for the LED
int inPin = 6; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int pos1 = 1;
int pos2 = 0;
int x=0;
int b = 0;
int y = 0;
int z=0;
int custom_0[] = {0x0e, 0x0e, 0x04, 0x0e, 0x1b, 0x0a, 0x0e, 0x11};
long previousMillis = 0; // will store last time LED was updated
long interval = 1100; // interval at which to blink (milliseconds)
char *msgs1[] = {"Live long and","A promotion is","You will soon","Laughter is the","Live long and", "Laugh hard and", "Eat well and", "A creative ","Someone", "A merry heart","Seize the day.","You are","Wealth is","Happy times","I see robots","Smile and you","Luck will soon","You will", "Flattery will","The world is", "Little and often", "Trust your", "Wall-e is", "Trust", "Time to start", "May the force"};
char *msgs2[] = {"prosper","coming soon.","be happy.","best medicine.","prosper.", "laugh often.", "make merry.", "mind is yours.","loves you.","is good.", " ","very lucky!", "soon yours.", "are ahead.", "in your future.","will be rich.","follow you.","prosper.", "get you far.", "your oyster.", "makes much.", "intuition.", "your friend.", "the collective.","something new.", "be with you."};
//("You will", "Flattery will", "The world is", "Little and often", "Trust your", " Wall-e is", "Trust", "Time to start", "May the force");
//("prosper.", "get you far.", "your oyster." "makes much.", "intuition.", "your friend.", "the collective.","something new.", "be with you.");
char *msgs3[] = {"!","@","#","$", "%", "&", "* ","@", "?","="};
char *msgs4[] = {"1","2","3","4", "5", "6", "7","8", "9","10","11","12","14","15","16","17","18","19","20"};
int NUM_MSGS = 11;
int NUM_MSGS2 = 19;
int i=0;
char my_str[100];
// Define a custom char in lcd
int defineCharacter(int ascii, int *data) {
int baseAddress = (ascii * 8) + 64;
// baseAddress = 64 | (ascii << 3);
lcd.command(baseAddress);
for (int i = 0; i < 8; i++)
lcd.write(data[i]);
lcd.command(128);
return ascii;
}
void setup() {
defineCharacter(0, custom_0);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
pinMode(0,INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
lcd.begin(16,2);
lcd.setCursor(0, 0);
lcd.print("Fortune Teller");
Serial.print("Fortune Teller ");
lcd.setCursor(0,1);
lcd.print("Robot 3000");
lcd.print(" ");
lcd.write(0);
Serial.print("Robot 3000");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thinking");
for (int i=0; i<4; i++)
{
lcd.print('.');
delay(200);
}
lcd.setCursor(0, 1);
lcd.print((char)229);
lcd.print(" ");
delay(200);
lcd.print((char)224);
lcd.print(" ");
delay(200);
lcd.print((char)225);
lcd.print(" ");
delay(200);
lcd.print((char)226);
lcd.print(" ");
delay(200);
lcd.print((char)227);
lcd.print(" ");
delay(200);
lcd.print((char)218);
lcd.print(" ");
delay(200);
lcd.print((char)216);
animation();
lcd.setCursor(0, 0);
delay(200);
for (int i=0; i<9; i++)
{
// pos2=pos2+1;
// pos1=pos1+i;
// int pick = random(NUM_MSGS);
// int randy = random(1,20);
// char* msg3 = msgs3[randy];
// lcd.print(msg3);
delay(100);
// if (pos2>= 2) pos2=0;
animation();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Your");
lcd.setCursor(0, 1);
lcd.print("fortune is");
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(200);
for (int i=0; i<3; i++)
{
lcd.print('.');
delay(100);
}
delay(2000);
lcd.clear();
//pick a random message from the array
int rind= random(1,30)-10;
if (rind == x)
{
rind=rind+1;
}
else
{
rind= random(rind);
}
x=rind;
int randNumber= random(1, countof(msgs1));
char* msg1 = msgs1[randNumber ];
char* msg2 = msgs2[randNumber ];
lcd.print(msg1);
Serial.print(msg1);
lcd.setCursor(0, 1);
lcd.print(msg2);
Serial.println(msg2);
delay(2500);
lcd.clear();
int pick2 = random(NUM_MSGS2);
char* msg4 = msgs4[pick2];
lcd.setCursor(0, 0);
strcpy(my_str, "number is ");
strcat(my_str, msg4);
strcat(my_str, ".");
lcd.print("Your lucky");
lcd.setCursor(0, 1);
lcd.print(my_str);
delay(1200);
for (int i=0; i<3; i++){
lcd.print('.');
delay(100);
}
delay(100);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
val = digitalRead(inPin); // read input value
animation4 ();
b=0;
// animation2();
}
void animation()
{
if (i = 1) digitalWrite(7, HIGH); //light the firstLED
if (i = 1 ) digitalWrite(8, LOW); //light the firstLED
if (i = 1 ) digitalWrite(9, LOW); //light the firstLED
delay(100);
if (i = 2 ) digitalWrite(7, LOW); //light the firstLED
if (i = 2 ) digitalWrite(8, HIGH); //light the firstLED
if (i = 2 ) digitalWrite(9, LOW); //light the firstLED
delay(100);
if (i = 3 ) digitalWrite(7, LOW); //light the firstLED
if (i = 3 ) digitalWrite(8, LOW); //light the firstLED
if (i = 3 ) digitalWrite(9, HIGH); //light the firstLED
delay(100);
if (i = 9 ) digitalWrite(7, LOW); //light the firstLED
if (i = 9 ) digitalWrite(8, LOW); //light the firstLED
if (i = 9 ) digitalWrite(9, LOW); //light the firstLED
}
void animation4 ()
{
Serial.print(" b = ");
Serial.println(b);
delay(25);
do{
val = digitalRead(inPin); // read input value
if (val == HIGH)
{
b=1;
}
if (x!=300){
lcd.setCursor(0,0);
lcd.print("Push the button");
lcd.setCursor(0,1);
val = digitalRead(inPin);
lcd.print("for a fortune!");
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
x++;
}
if(x>=300)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Button needs");
lcd.setCursor(0,1);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
val = digitalRead(inPin);
delay(1250);
lcd.print("pushing!");
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
val = digitalRead(inPin);
delay(2000);
x=0;
lcd.clear();
Serial.print(x);
Serial.print(" z ");
Serial.println(z);
val = digitalRead(inPin);
z++;
if (z>=2)
{
lcd.setCursor(0,0);
lcd.print("Push the");
lcd.setCursor(0,1);
val = digitalRead(inPin);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(1250);
lcd.print("button please!");
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
val = digitalRead(inPin);
delay(2000);
z=0;
}
}
}
while (b==0);
}
I'm probably going to use a 40x4 display, and an SD card.
**line of code highlighted**
LCD4Bit lcd = LCD4Bit(2);
**Error message**
LCD4Bit DOES NOT NAME A TYPE
Can you please explain why and how to fix this please :)
Thanks
I have not used this in a while.
Try using the code below
You should include the Liquid Crystal library and maybe the Wire library.
If you are missing either of these , look at http://arduino.cc/en/Reference/Libraries .
Also make sure you wire up the pins correctly to (12, 11, 5, 4, 3, 2);
Hope this helps.
#include
#define countof(array) ( sizeof(array) / sizeof(*(array)) )
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Todd Miller 2008
int ledPin = 1; // choose the pin for the LED
int inPin = 6; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int pos1 = 1;
int pos2 = 0;
int x=0;
int b = 0;
int y = 0;
int z=0;
int custom_0[] = {0x0e, 0x0e, 0x04, 0x0e, 0x1b, 0x0a, 0x0e, 0x11};
long previousMillis = 0; // will store last time LED was updated
long interval = 1100; // interval at which to blink (milliseconds)
char *msgs1[] = {"Live long and","A promotion is","You will soon","Laughter is the","Live long and", "Laugh hard and", "Eat well and", "A creative ","Someone", "A merry heart","Seize the day.","You are","Wealth is","Happy times","I see robots","Smile and you","Luck will soon","You will", "Flattery will","The world is", "Little and often", "Trust your", "Wall-e is", "Trust", "Time to start", "May the force"};
char *msgs2[] = {"prosper","coming soon.","be happy.","best medicine.","prosper.", "laugh often.", "make merry.", "mind is yours.","loves you.","is good.", " ","very lucky!", "soon yours.", "are ahead.", "in your future.","will be rich.","follow you.","prosper.", "get you far.", "your oyster.", "makes much.", "intuition.", "your friend.", "the collective.","something new.", "be with you."};
//("You will", "Flattery will", "The world is", "Little and often", "Trust your", " Wall-e is", "Trust", "Time to start", "May the force");
//("prosper.", "get you far.", "your oyster." "makes much.", "intuition.", "your friend.", "the collective.","something new.", "be with you.");
char *msgs3[] = {"!","@","#","$", "%", "&", "* ","@", "?","="};
char *msgs4[] = {"1","2","3","4", "5", "6", "7","8", "9","10","11","12","14","15","16","17","18","19","20"};
int NUM_MSGS = 11;
int NUM_MSGS2 = 19;
int i=0;
char my_str[100];
// Define a custom char in lcd
int defineCharacter(int ascii, int *data) {
int baseAddress = (ascii * 8) + 64;
// baseAddress = 64 | (ascii << 3);
lcd.command(baseAddress);
for (int i = 0; i < 8; i++)
lcd.write(data[i]);
lcd.command(128);
return ascii;
}
void setup() {
defineCharacter(0, custom_0);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
pinMode(0,INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
lcd.begin(16,2);
lcd.setCursor(0, 0);
lcd.print("Fortune Teller");
Serial.print("Fortune Teller ");
lcd.setCursor(0,1);
lcd.print("Robot 3000");
lcd.print(" ");
lcd.write(0);
Serial.print("Robot 3000");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thinking");
for (int i=0; i<4; i++)
{
lcd.print('.');
delay(200);
}
lcd.setCursor(0, 1);
lcd.print((char)229);
lcd.print(" ");
delay(200);
lcd.print((char)224);
lcd.print(" ");
delay(200);
lcd.print((char)225);
lcd.print(" ");
delay(200);
lcd.print((char)226);
lcd.print(" ");
delay(200);
lcd.print((char)227);
lcd.print(" ");
delay(200);
lcd.print((char)218);
lcd.print(" ");
delay(200);
lcd.print((char)216);
animation();
lcd.setCursor(0, 0);
delay(200);
for (int i=0; i<9; i++)
{
// pos2=pos2+1;
// pos1=pos1+i;
// int pick = random(NUM_MSGS);
// int randy = random(1,20);
// char* msg3 = msgs3[randy];
// lcd.print(msg3);
delay(100);
// if (pos2>= 2) pos2=0;
animation();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Your");
lcd.setCursor(0, 1);
lcd.print("fortune is");
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(200);
for (int i=0; i<3; i++)
{
lcd.print('.');
delay(100);
}
delay(2000);
lcd.clear();
//pick a random message from the array
int rind= random(1,30)-10;
if (rind == x)
{
rind=rind+1;
}
else
{
rind= random(rind);
}
x=rind;
int randNumber= random(1, countof(msgs1));
char* msg1 = msgs1[randNumber ];
char* msg2 = msgs2[randNumber ];
lcd.print(msg1);
Serial.print(msg1);
lcd.setCursor(0, 1);
lcd.print(msg2);
Serial.println(msg2);
delay(2500);
lcd.clear();
int pick2 = random(NUM_MSGS2);
char* msg4 = msgs4[pick2];
lcd.setCursor(0, 0);
strcpy(my_str, "number is ");
strcat(my_str, msg4);
strcat(my_str, ".");
lcd.print("Your lucky");
lcd.setCursor(0, 1);
lcd.print(my_str);
delay(1200);
for (int i=0; i<3; i++){
lcd.print('.');
delay(100);
}
delay(100);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
val = digitalRead(inPin); // read input value
animation4 ();
b=0;
// animation2();
}
void animation()
{
if (i = 1) digitalWrite(7, HIGH); //light the firstLED
if (i = 1 ) digitalWrite(8, LOW); //light the firstLED
if (i = 1 ) digitalWrite(9, LOW); //light the firstLED
delay(100);
if (i = 2 ) digitalWrite(7, LOW); //light the firstLED
if (i = 2 ) digitalWrite(8, HIGH); //light the firstLED
if (i = 2 ) digitalWrite(9, LOW); //light the firstLED
delay(100);
if (i = 3 ) digitalWrite(7, LOW); //light the firstLED
if (i = 3 ) digitalWrite(8, LOW); //light the firstLED
if (i = 3 ) digitalWrite(9, HIGH); //light the firstLED
delay(100);
if (i = 9 ) digitalWrite(7, LOW); //light the firstLED
if (i = 9 ) digitalWrite(8, LOW); //light the firstLED
if (i = 9 ) digitalWrite(9, LOW); //light the firstLED
}
void animation4 ()
{
Serial.print(" b = ");
Serial.println(b);
delay(25);
do{
val = digitalRead(inPin); // read input value
if (val == HIGH)
{
b=1;
}
if (x!=300){
lcd.setCursor(0,0);
lcd.print("Push the button");
lcd.setCursor(0,1);
val = digitalRead(inPin);
lcd.print("for a fortune!");
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
x++;
}
if(x>=300)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Button needs");
lcd.setCursor(0,1);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
val = digitalRead(inPin);
delay(1250);
lcd.print("pushing!");
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
val = digitalRead(inPin);
delay(2000);
x=0;
lcd.clear();
Serial.print(x);
Serial.print(" z ");
Serial.println(z);
val = digitalRead(inPin);
z++;
if (z>=2)
{
lcd.setCursor(0,0);
lcd.print("Push the");
lcd.setCursor(0,1);
val = digitalRead(inPin);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay(1250);
lcd.print("button please!");
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
val = digitalRead(inPin);
delay(2000);
z=0;
}
}
}
while (b==0);
}
This is a really neat project. Do you have a schematic for it, or a wiring diagram? I am trying to understand how to make an LCD interactive project and yours seems like a good place to learn. I don't have a grasp on how the momentary button interacts with the LCD menu.
Thanks for any advice,
Jeremy
For the button, basically I' m suing a do while loop. I animate the LEDS while b=0. If b<>0, then I go grab a new fortune.
Good luck.
I used a 16 x 2 LCD from Sparkfun, 4 LEDs, an Arduino from Sparkfun, 2 2.2 resisitors to connect the backlight on the LCD, a pushbutton switch and a small slider type switch to turn it on and off. I also used male headers to solder to the LCD and corresponding female headers to connect the wires to the breadboard. You can also buy some of thes parts at Adafruit as well, although they don't sell the LCDs.