#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 MODE;
int EASY = 700;
int MED = 500;
int HARD = 350;
int threshold;
/* Pin Connection: IR Receiver*/
IRrecv rec(25);
int Remote; //initialize to 0 for first run.
int Rem1 = 48;
int Rem2 = 24;
int Rem3 = 122;
//servo
Servo doorServo;
int doorState = 0;
void displayIntro()
{
lcd.clear();
lcd.print("You are a guard.");
lcd.setCursor(0, 1);
lcd.print("Close the gates");
delay(1500);
lcd.clear();
lcd.print("When the light");
lcd.setCursor(0, 1);
lcd.print("Turns RED!");
delay(1500);
lcd.clear();
lcd.print("Pick a Mode");
lcd.setCursor(0, 1);
lcd.print("1, 2, 3");
}
void openDoor() {
doorServo.write(180);
doorState = 1;
}
void closeDoor() {
doorServo.write(0);
doorState = 0;
}
///%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//setup
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();
Remote = 0;
}
void changeMode() {
//Remote = 0; I think get rid of this. because after each button press, Remote is reset to 0.
while (Remote == 0) { //changed while to if
if (rec.decode()) {
Remote = rec.decodedIRData.command;
Serial.println(Remote);
rec.resume();
}
}
if (Remote == Rem1) {
MODE = EASY;
lcd.clear();
lcd.print("EASY Mode. Hold");
lcd.setCursor(0, 1);
lcd.print("Button to start");
}
else if (Remote == Rem2) {
MODE = MED;
lcd.clear();
lcd.print("MEDIUM Mode.Hold");
lcd.setCursor(0, 1);
lcd.print("Button to start");
}
else if (Remote == Rem3) {
MODE = HARD;
lcd.clear();
lcd.print("HARD Mode. Hold");
lcd.setCursor(0, 1);
lcd.print("Button to start");
}
Serial.println("hi");
}
//####LOOP#######################################################
void loop() {
changeMode();
Serial.println("before door");
closeDoor();
Serial.println("after door");
if (digitalRead(Button) == LOW)
{ // Test does not start until
// button is pushed (and held)
tone(13, 1200, 30);
delay(1400);
noTone(13);
}
if (MODE == EASY) {
threshold = EASY;
} else if (MODE == MED) {
threshold = MED;
} else if (MODE == HARD) {
threshold = HARD;
}
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 < threshold) {
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);
Remote = 0;
}
// void loop() {
// // Read IR remote commands only if the button hasn't been pressed yet
// if (!digitalRead(Button)) {
// // Wait for the button to be pressed to start the game
// while (!digitalRead(Button)) {
// delay(10);
// }
// // Start the game
// closeDoor();
// tone(13, 1200, 30);
// delay(1400);
// noTone(13);
// // Proceed with the game based on the selected mode
// 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 TME = 0;
// delay(1000);
// }
// }
//put in void loop
// Read IR remote commands only if the button hasn't been pressed yet
// while (1) {
// Remote = 0;
// if (rec.decode()) {
// Remote = rec.decodedIRData.command;
// Serial.println(Remote);
// rec.resume();
// }
// }