Step 6: Fortune Teller Code
#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);
}
Remove these ads by
Signing Up





















Not Nice















Visit Our Store »
Go Pro Today »



