End of Term Arduino Game Project

For the last two weeks of term Aidan set us a project in groups of four to create a game using an Arduino that involves both an input and an output. Me and my group initially brainstormed a range of possibilities including a game which involves the player having to raise and lower their arm to a particular height, a Bop-it game and a a memory game. but the idea we decided to proceed with was the ‘Red light, Green light’ game. This game requires the user to begin by standing in front of a PIR (Passive Infrared) Sensor which sits next to a Red and Green LED and a push button. When the green light is lit the player can walk forward, and when the red light is lit the player must stand still. If the PIR Sensor senses movement when light is red then the player has lost and the game restarts. If you can reach the button and press it without moving during the red light being lit, you win the game. This can be Multi-player if multiple sensors are used.

This project has encouraged me massively to continue practicing with Arduino coding since I definitely didn’t find creating the code for this the easiest task, and hopefully one day with practice I will find it easier.

With the help of Aidan we managed to get this Code:

int ledList[2] = {0,1}; //declaring array for LED’s
int ledPins[2] = {11,12}; //declaring pins for LED
int pirPin = 10; //declaring sensor
int buttonpin = 9; //declaring pin for button
int buttonstate = 0; //declaring button state
boolean pirState = 0; //declaring pir state for change
long timer = 0;
long lastTimer = 0;
long interval = 2000;

void setup() {

pinMode(ledPins[0], OUTPUT); //setting LEDs both off
pinMode(ledPins[1], OUTPUT);
Serial.begin(9600);
Serial.print(“Calibrating”);
delay(40000);
Serial.print(“Calibration complete”); //initialising game
pinMode(pirPin, INPUT); //setting input pins for pir sensor
Serial.begin(9600);
delay(1000); //allowing 5 seconds for pir sensor to ouput

}

void loop() {
//pirState = digitalRead(pirPin);
//Serial.println(pirState); //setting pirpin to output the pir state

//digitalWrite(ledPins[0], LOW); //turning LED’s off
//digitalWrite(ledPins[1], LOW);

timer = millis();

if(timer – lastTimer >= interval) {
int randNumber = random(2); //selected randon LED
Serial.println(randNumber); //output randon LED
if (randNumber == 0){
ledList[0] = 1;
ledList[1] = 0;
}
else {
ledList[0] = 0;
ledList[1] = 1;
}
lastTimer = timer;
if(ledList[0] == 0){ // Green On
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[0], LOW); //Tunrs Red LED on
Serial.println(“Stop Moving!”); //outputs message

}
else { // Red On
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[0], HIGH); //Tunrs Green LED on
Serial.println(“You Can Move!”); //Outputs messege
//delay (1000); //delays output to give people time
pirState = digitalRead(pirPin);
Serial.println(pirState); //outputs pir sensor reading
}
//delay(2000);
}

pirState = digitalRead(pirPin);
if (ledList[1] == 1 && pirState == 1) {
Serial.println (“GAME OVER, YOU LOSE”);
}

buttonstate = digitalRead(buttonpin); //sets an output for the button state

if(buttonstate == HIGH) { // 2 equals signs means a test where 1 menas its true
Serial.println(“GAME OVER!!, You are the winner”); //outputs a winning messege when the button is pressed
}

}

I’d like to develop this code further now this project is further, there are many obvious improvements that can be make including;

  • Adding a timer so that the player has time to step back once the game has started
  • Creating the code so that multiple sensors can be added
  • Difficulty settings such as having a more LEDs and alternating flashing time.

As a group we found the hardware side of this project much more simple, below is the wiring set set up including, a bread board, an Arduino, 2 LED’s in pins 11 and 12, the button in pin 9 and the PIR sensor in pin 10.game

When I finalize the code I would like to design the game to have a casing to make it more presentable and aesthetically beautiful, making it a more complete product.