Arduino Fortune Teller
Intro: Arduino Fortune Teller
This project gives the user a fortune and lucky number like from a fortune cookie. This was made as a Christmas present.
STEP 1: Step 1
Solder header pins onto LCD. Solder wires to female header pins and hookup to Arduino. Here we used the LCD 4 bit Library and hooked it up accordingly. This is slightly different the using the 8 bit library.
STEP 2: Step 2
Check that the code and screen work.
STEP 3: Step 3
Using an old cigar box I created a shell for this project. I had to cut 4 holes for the LEDS, 1 hole for the LCD, 1 hole for the on/off switch and 1 for the button.
STEP 4: Step 4
Now for the tricky part, stuff all the wires into the box!
Here we hookup the battery by itself to the on/off switch, stuff everything inside and hookup all the LEDs.
Here we hookup the battery by itself to the on/off switch, stuff everything inside and hookup all the LEDs.
STEP 5: Step 5
Work is complete - get a fortune!
STEP 6: Fortune Teller Code
#include <LCD4Bit.h>
#define countof(array) ( sizeof(array) / sizeof(*(array)) )
LCD4Bit lcd = LCD4Bit(2);
// Todd Miller 2008
int ledPin = 13; // choose the pin for the LED
int inPin = 4; // 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 tv = 0;
int tv2 = 0;
long previousMillis = 0; // will store last time LED was updated
long interval = 1100; // interval at which to blink (milliseconds)
char *msgs1= {"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"};
char *msgs2 = {"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."};
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 ;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
pinMode(0,INPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
digitalWrite(13, HIGH);
lcd.init();
lcd.printIn("Fortune Teller");
lcd.cursorTo(2, 0);
lcd.printIn("Robot 3000");
delay(2000);
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn("Thinking");
for (int i=0; i<3; i++)
{
lcd.print('.');
delay(400);
animation();
}
lcd.cursorTo(1, 0);
lcd.clear();
for (int i=0; i<9; i++)
{
pos2=pos2+1;
pos1=pos1+i;
int pick = random(NUM_MSGS);
int randy = random(1,10);
char* msg3 = msgs3;
lcd.printIn(msg3);
delay(300);
if (pos2>= 2) pos2=0;
animation();
}
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn("Your");
lcd.cursorTo(2, 0);
lcd.printIn("fortune is");
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(3, HIGH);
delay(1200);
for (int i=0; i<3; i++)
{
lcd.print('.');
delay(300);
}
lcd.leftScroll(20, 50);
lcd.clear();
//pick a random message from the array
int rind= random(1,21)-10;
if (rind == x)
{
rind=rind+1;
}
else
{
rind= random(rind);
}
x=rind;
int randNumber= random(1, countof(msgs1));
char* msg1 = msgs1;
char* msg2 = msgs2;
lcd.printIn(msg1);
lcd.cursorTo(2, 0);
lcd.printIn(msg2);
delay(2500);
lcd.clear();
int pick2 = random(NUM_MSGS2);
char* msg4 = msgs4;
lcd.cursorTo(1, 0);
strcpy(my_str, "number is ");
strcat(my_str, msg4);
strcat(my_str, ".");
lcd.printIn("Your lucky");
lcd.cursorTo(2, 0);
lcd.printIn(my_str);
delay(1200);
for (int i=0; i<3; i++){
lcd.print('.');
delay(300);
}
delay(300);
lcd.leftScroll(20, 50);
lcd.clear();
lcd.cursorTo(1, 0);
val = digitalRead(inPin); // read input value
animation2();
}
void animation()
{
if (i = 1) digitalWrite(6, HIGH); //light the firstLED
if (i = 1 ) digitalWrite(5, LOW); //light the firstLED
if (i = 1 ) digitalWrite(3, LOW); //light the firstLED
delay(70);
if (i = 2 ) digitalWrite(6, LOW); //light the firstLED
if (i = 2 ) digitalWrite(5, HIGH); //light the firstLED
if (i = 2 ) digitalWrite(3, LOW); //light the firstLED
delay(70);
if (i = 3 ) digitalWrite(6, LOW); //light the firstLED
if (i = 3 ) digitalWrite(5, LOW); //light the firstLED
if (i = 3 ) digitalWrite(3, HIGH); //light the firstLED
delay(70);
if (i = 9 ) digitalWrite(6, LOW); //light the firstLED
if (i = 9 ) digitalWrite(5, LOW); //light the firstLED
if (i = 9 ) digitalWrite(3, LOW); //light the firstLED
}
void animation2()
{
digitalWrite(13, HIGH);
do {
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
b = 0;
} else {
b = 1;
y=0;
tv=0;
tv2=0;
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(3, LOW);// turn LED ON
}
if (millis() - previousMillis > interval) {
previousMillis = millis(); // remember the last time we printed
tv = tv +1;
tv2=tv2+1;
if (tv == 1)
{
lcd.clear();
lcd.printIn("Fortune Teller");
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
}
if (tv == 2)
{
lcd.cursorTo(2,0);
lcd.printIn("Robot 3000");
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(3, LOW);
}
if (tv == 3 && tv2 !=9 && tv2 !=10)
{
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("Push the button");
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(3, LOW);
}
if (tv == 4 && tv2 !=9 && tv2 !=10)
{
lcd.cursorTo(2,0);
lcd.printIn("for a fortune!");
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(3, HIGH);
}
if (tv2==7)
{
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("Your fortune");
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
}
if (tv2==8)
{
lcd.cursorTo(2,0);
lcd.printIn("awaits you.");
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
tv=0;
tv2=0;
}
}
} while (b==0);
}
#define countof(array) ( sizeof(array) / sizeof(*(array)) )
LCD4Bit lcd = LCD4Bit(2);
// Todd Miller 2008
int ledPin = 13; // choose the pin for the LED
int inPin = 4; // 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 tv = 0;
int tv2 = 0;
long previousMillis = 0; // will store last time LED was updated
long interval = 1100; // interval at which to blink (milliseconds)
char *msgs1= {"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"};
char *msgs2 = {"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."};
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 ;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
pinMode(0,INPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
digitalWrite(13, HIGH);
lcd.init();
lcd.printIn("Fortune Teller");
lcd.cursorTo(2, 0);
lcd.printIn("Robot 3000");
delay(2000);
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn("Thinking");
for (int i=0; i<3; i++)
{
lcd.print('.');
delay(400);
animation();
}
lcd.cursorTo(1, 0);
lcd.clear();
for (int i=0; i<9; i++)
{
pos2=pos2+1;
pos1=pos1+i;
int pick = random(NUM_MSGS);
int randy = random(1,10);
char* msg3 = msgs3;
lcd.printIn(msg3);
delay(300);
if (pos2>= 2) pos2=0;
animation();
}
lcd.clear();
lcd.cursorTo(1, 0);
lcd.printIn("Your");
lcd.cursorTo(2, 0);
lcd.printIn("fortune is");
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(3, HIGH);
delay(1200);
for (int i=0; i<3; i++)
{
lcd.print('.');
delay(300);
}
lcd.leftScroll(20, 50);
lcd.clear();
//pick a random message from the array
int rind= random(1,21)-10;
if (rind == x)
{
rind=rind+1;
}
else
{
rind= random(rind);
}
x=rind;
int randNumber= random(1, countof(msgs1));
char* msg1 = msgs1;
char* msg2 = msgs2;
lcd.printIn(msg1);
lcd.cursorTo(2, 0);
lcd.printIn(msg2);
delay(2500);
lcd.clear();
int pick2 = random(NUM_MSGS2);
char* msg4 = msgs4;
lcd.cursorTo(1, 0);
strcpy(my_str, "number is ");
strcat(my_str, msg4);
strcat(my_str, ".");
lcd.printIn("Your lucky");
lcd.cursorTo(2, 0);
lcd.printIn(my_str);
delay(1200);
for (int i=0; i<3; i++){
lcd.print('.');
delay(300);
}
delay(300);
lcd.leftScroll(20, 50);
lcd.clear();
lcd.cursorTo(1, 0);
val = digitalRead(inPin); // read input value
animation2();
}
void animation()
{
if (i = 1) digitalWrite(6, HIGH); //light the firstLED
if (i = 1 ) digitalWrite(5, LOW); //light the firstLED
if (i = 1 ) digitalWrite(3, LOW); //light the firstLED
delay(70);
if (i = 2 ) digitalWrite(6, LOW); //light the firstLED
if (i = 2 ) digitalWrite(5, HIGH); //light the firstLED
if (i = 2 ) digitalWrite(3, LOW); //light the firstLED
delay(70);
if (i = 3 ) digitalWrite(6, LOW); //light the firstLED
if (i = 3 ) digitalWrite(5, LOW); //light the firstLED
if (i = 3 ) digitalWrite(3, HIGH); //light the firstLED
delay(70);
if (i = 9 ) digitalWrite(6, LOW); //light the firstLED
if (i = 9 ) digitalWrite(5, LOW); //light the firstLED
if (i = 9 ) digitalWrite(3, LOW); //light the firstLED
}
void animation2()
{
digitalWrite(13, HIGH);
do {
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
b = 0;
} else {
b = 1;
y=0;
tv=0;
tv2=0;
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(3, LOW);// turn LED ON
}
if (millis() - previousMillis > interval) {
previousMillis = millis(); // remember the last time we printed
tv = tv +1;
tv2=tv2+1;
if (tv == 1)
{
lcd.clear();
lcd.printIn("Fortune Teller");
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
}
if (tv == 2)
{
lcd.cursorTo(2,0);
lcd.printIn("Robot 3000");
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(3, LOW);
}
if (tv == 3 && tv2 !=9 && tv2 !=10)
{
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("Push the button");
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(3, LOW);
}
if (tv == 4 && tv2 !=9 && tv2 !=10)
{
lcd.cursorTo(2,0);
lcd.printIn("for a fortune!");
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(3, HIGH);
}
if (tv2==7)
{
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("Your fortune");
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
}
if (tv2==8)
{
lcd.cursorTo(2,0);
lcd.printIn("awaits you.");
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
tv=0;
tv2=0;
}
}
} while (b==0);
}
29 Comments
TaranA4 4 years ago
DesignStudent 10 years ago
DesignStudent 10 years ago
I added your code that you gave me like this:
********************************************************************************************
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("Madame Zahara");
********************************************************************************************
lcd.print(" ");
********************************************************************************************
int val = digitalRead(inPin);
********************************************************************************************
if (val == HIGH) // button has been pressed
{
********************************************************************************************
// 1. go to led animation for "thinking"
********************************************************************************************
// 2. go get fortune and print to screen
}
********************************************************************************************
Serial.print("Madame Zahara");
********************************************************************************************
delay(2000);
********************************************************************************************
lcd.clear();
********************************************************************************************
lcd.setCursor(0, 0);
********************************************************************************************
lcd.print("Thinking");
********************************************************************************************
for (int i=0; i<4; i++)
{
********************************************************************************************
Paulys 10 years ago
+5V--------------button--------- 1k resistor-------- gnd
>>>>>>>>>>>>>>>>>>>|
>>>>>>>>>>>>>>>>>>>|
Wire to pin 6 goes in here|
http://arduino.cc/en/Tutorial/Button
Paulys 10 years ago
http://arduino.cc/en/Tutorial/Button
+5V--------------button--------- 1k resistor-------- gnd
|
|
pin 6
DesignStudent 10 years ago
Paulys 10 years ago
void loop() {
int val = digitalRead(inPin);
if (val == HIGH) // button has been pressed
{
// 1. go to led animation for "thinking"
// 2. go get fortune and print to screen
}
Also, here are two good resources to get you going. Good luck making it.
http://arduino.cc/en/Tutorial/Button
http://arduino.cc/en/Tutorial/BlinkWithoutDelay
rconnell 11 years ago
Paulys 11 years ago
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);
}
fmoser 12 years ago
I'm probably going to use a 40x4 display, and an SD card.
Paulys 12 years ago
kimbo84 13 years ago
**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
Paulys 13 years ago
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);
}
jman 31 14 years ago
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
Paulys 14 years ago
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.
Mario1 14 years ago
sweenes 14 years ago
Paulys 14 years ago
sweenes 14 years ago
chilll2009 14 years ago