#include <IRremote.h>
#include <LedControl.h>
#include <LiquidCrystal.h>
#include <Servo.h>
/* LCD Setup lcd(rs, e, d4, d5, d6, d7)*/
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
// BUTTON, LED
int Buzzer = 13;
int Button = 12; // pin connected to push button.
int LEDR = 11; // Red LED
int LEDGr = 10; // Green LED
int LEDB = 9; // Blue LED
int Beep; // for speaker
// Reaction time game parameters
int PSE; // Random pause time
int TME; // time
int RTME = 0; // reaction time
int COLOUR;
//gamemodes, Easy Med Hard
//measuring rxn time
//use for remote
int EASY = 700;
int MED = 500;
int HARD = 350;
/* Pin Connection: IR Receiver*/
IRrecv rec(25);
//servo
Servo doorServo;
int doorState = 0;
void setup()
{
//Servo
doorServo.attach(44); //servo
closeDoor();
// LCD Setup
lcd.begin(16, 2);
lcd.clear();
displayIntro(); // Display the intro
// Set up Button, LED pins, buzzer?
pinMode(LEDR, OUTPUT); // Set LED pins as output
pinMode(LEDB, OUTPUT);
pinMode(LEDGr, OUTPUT);
pinMode(Button, INPUT); // Set pushbutton as input
digitalWrite(LEDR, LOW); // Switch on all LED colors
digitalWrite(LEDB, LOW);
digitalWrite(LEDGr, LOW);
// Serial.begin(9600);
// // IR Receiver
// rec.enableIRIn();
}
void displayIntro()
{
lcd.clear();
lcd.print("You are a guard.");
lcd.setCursor(0, 1);
lcd.print("Close the gates");
delay(1000);
lcd.clear();
lcd.print("When the light");
lcd.setCursor(0, 1);
lcd.print("Turns RED!");
delay(1000);
lcd.clear();
lcd.print("Hold Button");
lcd.setCursor(0, 1);
lcd.print("to begin.");
}
void openDoor() {
doorServo.write(180);
doorState = 1;
}
void closeDoor() {
doorServo.write(0);
doorState = 0;
}
void loop()
{
playGame();
// closeDoor();
// if (digitalRead(Button) == LOW)
// { // Test does not start until
// // button is pushed (and held)
// tone(13, 1200, 30);
// delay(1400);
// noTone(13);
// }
// lcd.clear();
// digitalWrite(LEDR, HIGH); // Switch off start light
// digitalWrite(LEDB, HIGH);
// digitalWrite(LEDGr, HIGH);
// randomSeed(analogRead(0)); // Random noise from pin 0
// COLOUR = random(1, 4); // Generate random color
// PSE = random(500, 1200); // Set random pause duration between lights
// // Repeat this loop while color is green or blue AND pushbutton
// // is held
// while (COLOUR != 1 && digitalRead(Button) == HIGH)
// {
// digitalWrite(LEDGr, HIGH);
// digitalWrite(LEDB, HIGH);
// delay(PSE);
// randomSeed(analogRead(0));
// Beep = random(1, 4); // Select random beep from buzzer
// // (buzzer beeps 1 in 3 times)
// PSE = random(750, 1200); // Select random pause duration between
// // lights (to increase surprise effect)
// if (Beep == 1)
// {
// tone(13, 1600, 350);
// delay(750);
// noTone(13);
// }
// if (COLOUR == 2)
// {
// digitalWrite(LEDGr, LOW);
// }
// if (COLOUR == 3)
// {
// digitalWrite(LEDB, LOW);
// }
// delay(PSE);
// randomSeed(analogRead(0));
// COLOUR = random(1, 4); // Select random color
// }
// // Execute this loop if color is red
// if (COLOUR == 1 && digitalRead(Button) == HIGH)
// {
// digitalWrite(LEDGr, HIGH);
// digitalWrite(LEDB, HIGH);
// delay(PSE);
// TME = millis(); // Record time since program has started
// digitalWrite(LEDR, LOW);
// while (digitalRead(Button) == HIGH)
// { // Runs until button is
// // released, recording the
// // reaction time
// delay(1);
// }
// lcd.display();
// RTME = millis() - TME; // Reaction time in ms
// lcd.print("Time: "); // Display on LCD screen
// lcd.print(RTME); // Print the reaction time
// lcd.print(" ms"); // Print " ms" after the reaction time
// if (RTME < EASY) {
// lcd.setCursor(0, 1);
// lcd.print("You win!");
// openDoor();
// } else {
// lcd.setCursor(0, 1);
// lcd.print("You lost :(");
// closeDoor();
// }
// }
// // Execute if color is NOT red but the pushbutton is released
// if (COLOUR != 1)
// {
// lcd.print("Released too");
// lcd.setCursor(0, 1); // Move to second line
// lcd.print("soon!!!");
// tone(13, 3000, 1500);
// delay(500);
// noTone(13);
// }
// // Test does not restart until the button is pushed once
// while (digitalRead(Button) == LOW)
// {
// delay(10);
// }
// digitalWrite(LEDR, LOW); // Reset all lights to begin again
// digitalWrite(LEDB, LOW);
// digitalWrite(LEDGr, LOW);
// lcd.clear();
// lcd.print("Hold Button to");
// lcd.setCursor(0, 1);
// lcd.print("start.");
// int Time = 0;
// delay(1000);
}
void playGame() {
closeDoor();
if (digitalRead(Button) == LOW)
{ // Test does not start until
// button is pushed (and held)
tone(13, 1200, 30);
delay(1400);
noTone(13);
}
lcd.clear();
digitalWrite(LEDR, HIGH); // Switch off start light
digitalWrite(LEDB, HIGH);
digitalWrite(LEDGr, HIGH);
randomSeed(analogRead(0)); // Random noise from pin 0
COLOUR = random(1, 4); // Generate random color
PSE = random(500, 1200); // Set random pause duration between lights
// Repeat this loop while color is green or blue AND pushbutton
// is held
while (COLOUR != 1 && digitalRead(Button) == HIGH)
{
digitalWrite(LEDGr, HIGH);
digitalWrite(LEDB, HIGH);
delay(PSE);
randomSeed(analogRead(0));
Beep = random(1, 4); // Select random beep from buzzer
// (buzzer beeps 1 in 3 times)
PSE = random(750, 1200); // Select random pause duration between
// lights (to increase surprise effect)
if (Beep == 1)
{
tone(13, 1600, 350);
delay(750);
noTone(13);
}
if (COLOUR == 2)
{
digitalWrite(LEDGr, LOW);
}
if (COLOUR == 3)
{
digitalWrite(LEDB, LOW);
}
delay(PSE);
randomSeed(analogRead(0));
COLOUR = random(1, 4); // Select random color
}
// Execute this loop if color is red
if (COLOUR == 1 && digitalRead(Button) == HIGH)
{
digitalWrite(LEDGr, HIGH);
digitalWrite(LEDB, HIGH);
delay(PSE);
TME = millis(); // Record time since program has started
digitalWrite(LEDR, LOW);
while (digitalRead(Button) == HIGH)
{ // Runs until button is
// released, recording the
// reaction time
delay(1);
}
lcd.display();
RTME = millis() - TME; // Reaction time in ms
lcd.print("Time: "); // Display on LCD screen
lcd.print(RTME); // Print the reaction time
lcd.print(" ms"); // Print " ms" after the reaction time
if (RTME < EASY) {
lcd.setCursor(0, 1);
lcd.print("You win!");
openDoor();
} else {
lcd.setCursor(0, 1);
lcd.print("You lost :(");
closeDoor();
}
}
// Execute if color is NOT red but the pushbutton is released
if (COLOUR != 1)
{
lcd.print("Released too");
lcd.setCursor(0, 1); // Move to second line
lcd.print("soon!!!");
tone(13, 3000, 1500);
delay(500);
noTone(13);
}
// Test does not restart until the button is pushed once
while (digitalRead(Button) == LOW)
{
delay(10);
}
digitalWrite(LEDR, LOW); // Reset all lights to begin again
digitalWrite(LEDB, LOW);
digitalWrite(LEDGr, LOW);
lcd.clear();
lcd.print("Hold Button to");
lcd.setCursor(0, 1);
lcd.print("start.");
int Time = 0;
delay(1000);
}