/*
Game -Catch The Color
IDEA: A reaction timer based game which requires the immediate ttention of player to catch the color
LOGIC: This sketch first prints introductorty statements if the game .
Then the colour to be found is printed and game begins!
If the color is found you win else you loose ...
THE CIRCUIT:
==========================================
LCD pin Connect to
------------------------------------------
01 - GND GND, pot
02 - VCC +5V, pot
03 - Contrast Pot wiper
04 - RS Pin8 (P2.0)
05 - R/W GND
06 - EN Pin9 (P2.1)
07 - DB0 GND
08 - DB1 GND
09 - DB2 GND
10 - DB3 GND
11 - DB4 Pin10 (P2.2)
12 - DB5 Pin11 (P2.3)
13 - DB6 Pin12 (P2.4)
14 - DB7 Pin13 (P2.5)
15 - BL+ +5V
16 - BL- GND
===========================================
=============================================
LED Connect to
---------------------------------------------
LED1(RED) +ive R1(10K)
LED1(RED) -ive GND
LED1(WHITE) +ive R2(10K)
LED1(WHITE) -ive GND
LED1(GREEN) +ive R3(10K)
LED1(GREEN) -ive GND
==============================================
RESISTOR Connect to
---------------------------------------------
R1 Pin2(P1.0)
R1 LED1
R2 Pin3(P1.1)
R2 LED2
R3 Pin4(P1.2)
R3 LED3
================================================
================================================
PUSH BUTTON Connect to
------------------------------------------------
PushButton(pullup)-p1 VCC
PushButton(pullup)-p2 VCC
PushButton(pullup)-p2 Pin4(P1.3)
================================================
*/
// include the library code:
#include <LiquidCrystal.h>
String string[3]= { "Red","White","Green" };// an array of string for choosing color
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5);
//initializing the pins for leds
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int c1=0;
// declaration of few intergral values for calculations
int c2=0;
int c3=0;
int i=0;
int j=0;
//intializing the pin for button
int pushbutton=5;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//intializing mode of operation of pins
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(pushbutton,INPUT_PULLUP);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.print("Welcome To Game");
delay(3000);
lcd.clear();
lcd.print("#CATCH THE COLOR");
delay(3000);
lcd.clear();
lcd.print(" **Start**");
delay(5000);
lcd.clear();
c3=random(3);
lcd.print(">>Stop at color:");
delay(3000);
lcd.clear();
lcd.print(string[c3]);
lcd.setCursor(1,0);
delay(3000);
//lcd.clear();
c2=loop1();
lcd.clear();
main:
if(c2>0)
{
if(c2==c3+1)
{
for(i=0;i<5;i++)
{lcd.print(" **YOU WON**");
delay(2500);
lcd.clear();
delay(250); }
}
else
{
for(i=0;i<3;i++)
{lcd.print("YOU LOST..");
delay(2500);
lcd.clear();
delay(500); }
}
}
else
{ c2=0;
while(c2==0)
c2 =loop1();
goto main ;
}
lcd.print("****************");
delay(3000);
lcd.clear();
}
int loop1() {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
int buttonstate= digitalRead(pushbutton);
c2=0;
digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED3, LOW);
c2++;
delay(100);
buttonstate= digitalRead(pushbutton);
// wait for a second
if(buttonstate==HIGH)
return(c2);
else{
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH); // turn the LED on (HIGH is the voltage level)
c2++;
delay(100);
buttonstate= digitalRead(pushbutton);
if(buttonstate==HIGH)
return(c2);
else
{ // turn the LED off by making the voltage LOW // wait for a second
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH); // turn the LED on (HIGH is the voltage level)
c2++;
delay(100);
buttonstate= digitalRead(pushbutton);
if(buttonstate==HIGH)
return(c2);}
}
}
Comments