Arduino: Game Using LEDS

This Arduino project is a simple memory game using LEDS. To play the game you need to press the button on the upper right area and a chain of 5 LEDS will light up. In order to win you will need to press them in the exact same order. You will be able to tell if you got it right from the two red and green LEDS to the right. Finally you can change the speed that the LEDS light up and how many light up each round to make the game a bit more challenging.

Step 1: Parts

To make this project you will ned the following:


> 1 Arduino UNO board

> 1 breadboard

> 7 LEDS

> 1 piezo

> 6 buttons

> 13 resistors (optional)

> A lot of cables (approximately 25)

Step 2: Circuit

If the schematic is unclear do here is a clearer explanation:

> LEDS (left to right):

// Game LEDS

- Green LED -- > pin 7

- Red LED -- > pin 8

- Yellow LED -- > pin 9

- Red LED -- > pin 10

- Green LED -- > pin 11

// Correct or false LEDS

- Green LED -- > pin A0 (14)

- Red LED -- > pin A1 (15)

> Buttons (left to right):

// Game buttons:

- Button 1 -- > pin 6

- Button 2 -- > pin 5

- Button 3 -- > pin 4

- Button 4 -- > pin 3

- Button 5 -- > pin 2

- Button (reset) -- > pin 12

> Piezo:

- piezo -- > pin 13

Step 3: Code

// Sorry for the format it broke when I copied it (press ctrl + t while selecting all the text to fix if it bothers you)

//from left to right LEDS
const int led1 = 7;

const int led2 = 8;

const int led3 = 9;

const int led4 = 10;

const int led5 = 11;

//from left to right BUTTONS

const int b1 = 6;

const int b2 = 5;

const int b3 = 4;

const int b4 = 3;

const int b5 = 2;

//restart game button const

int restart = 12;

//correct or false

const int ledGreen = 14;

const int ledRed = 15;

boolean played = false;

boolean out = false;

boolean winner = false;

int i;

int z;

//piezo pin const

int piezo = 13;

//button states

int buttonState1;

int buttonState2;

int buttonState3;

int buttonState4;

int buttonState5;

int restartButtonState;

//check LEDS

int ledON; //check which LEDS are on

int ledPrevious = -1; //dont have duplicates (optional)

//scoring system

int score = 0; //scoring system

int rounds = 20; //rounds

int countRound = 1;

//speed of LEDS

int s = 500; //speed of LEDS (suggested speed 800 for easy, 600 normal, <600 hard)

int a = 0; //accelaration of LEDS (every round)

int digits = 4; //how many leds on every round (its -1)

int sp = 50; //speed when playing

//timer

unsigned long startMillis;

const unsigned long period = 1000;

boolean once = false;

//arrays with saved rounds

int savedOrder[5];

int savedLed[5];

void setup () {

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(led5, OUTPUT);

pinMode(b1, INPUT_PULLUP);

pinMode(b2, INPUT_PULLUP);

pinMode(b3, INPUT_PULLUP);

pinMode(b4, INPUT_PULLUP);

pinMode(b5, INPUT_PULLUP);

pinMode(ledGreen, OUTPUT);

pinMode(ledRed, OUTPUT);

pinMode(restart, INPUT_PULLUP);

Serial.begin(9600);

}

void loop () {

restartButtonState = digitalRead(restart);

if (restartButtonState == LOW) {

digitalWrite(ledGreen, LOW);

digitalWrite(ledRed, LOW);

for (int j = 0 ; j <= digits ; j++) {

randomSeed(analogRead(A5));

do { //optional same LED twice or not

ledPrevious = ledON;

ledON = random(1, 6);

}

while(ledPrevious == ledON);

savedLed[j] = ledON;

if (savedLed[j] == 1) {

digitalWrite(led1, HIGH);

delay(s);

digitalWrite(led1, LOW);

delay(s);

}

else if (savedLed[j] == 2) {

digitalWrite(led2, HIGH);

delay(s);

digitalWrite(led2, LOW);

delay(s);

}

else if (savedLed[j] == 3) {

digitalWrite(led3, HIGH);

delay(s);

digitalWrite(led3, LOW);

delay(s);

}

else if (savedLed[j] == 4) {

digitalWrite(led4, HIGH);

delay(s);

digitalWrite(led4, LOW);

delay(s);

}

else if (savedLed[j] == 5) {

digitalWrite(led5, HIGH);

delay(s);

digitalWrite(led5, LOW);

delay(s);

}

savedOrder[j] = j;

if (j == digits) {

played = true;

i = 0;

}

}

}

if (played == true) {

do {

played = false;

readButtonStates();

if (buttonState1 == LOW) {

if (savedLed[i] == 1 && savedOrder[i] == i) {

digitalWrite(led1, HIGH);

digitalWrite(ledGreen, HIGH);

delay(s);

digitalWrite(led1, LOW);

digitalWrite(ledGreen, LOW);

delay(sp);

i++;

}

else {

digitalWrite(ledRed, HIGH);

delay(s);

digitalWrite(ledRed, LOW);

delay(sp);

out = true;

}

}

else if (buttonState2 == LOW) {

if (savedLed[i] == 2 && savedOrder[i] == i) {

digitalWrite(led2, HIGH);

digitalWrite(ledGreen, HIGH);

delay(s);

digitalWrite(led2, LOW);

digitalWrite(ledGreen, LOW);

delay(sp);

i++;

}

else {

digitalWrite(ledRed, HIGH);

delay(sp);

digitalWrite(ledRed, LOW);

delay(sp);

out = true;

}

}

else if (buttonState3 == LOW) {

if (savedLed[i] == 3 && savedOrder[i] == i) {

digitalWrite(led3, HIGH);

digitalWrite(ledGreen, HIGH);

delay(s);

digitalWrite(led3, LOW);

digitalWrite(ledGreen, LOW);

delay(sp);

i++;

}

else {

digitalWrite(ledRed, HIGH);

delay(s);

digitalWrite(ledRed, LOW);

delay(sp);

out = true;

}

}

else if (buttonState4 == LOW) {

if (savedLed[i] == 4 && savedOrder[i] == i) {

digitalWrite(led4, HIGH);

digitalWrite(ledGreen, HIGH);

delay(s);

digitalWrite(led4, LOW);

digitalWrite(ledGreen, LOW);

delay(sp);

i++;

}

else {

digitalWrite(ledRed, HIGH);

delay(s);

digitalWrite(ledRed, LOW);

delay(sp);

out = true;

}

}

else if (buttonState5 == LOW) {

if (savedLed[i] == 5 && savedOrder[i] == i) {

digitalWrite(led5, HIGH);

digitalWrite(ledGreen, HIGH);

delay(s);

digitalWrite(led5, LOW);

digitalWrite(ledGreen, LOW);

delay(sp);

i++;

}

else {

digitalWrite(ledRed, HIGH);

delay(s);

digitalWrite(ledRed, LOW);

delay(sp);

out = true;

}

}

if (i == digits+1) {

Serial.write("win");

winner = true;

}

}

while (out == false && winner == false);

}

if (winner == true) {

tone(piezo, 2000);

delay(200);

noTone(piezo);

delay(200);

digitalWrite(ledGreen, HIGH);

winner = false;

}

else if (out == true) {

tone(piezo, 200);

delay(200);

noTone(piezo);

delay(200);

digitalWrite(ledRed, HIGH);

out = false;

}

}

void readButtonStates() {

buttonState1 = digitalRead(b1);

buttonState2 = digitalRead(b2);

buttonState3 = digitalRead(b3);

buttonState4 = digitalRead(b4); buttonState5 = digitalRead(b5);

}

Share

    Recommendations

    • Tape Contest

      Tape Contest
    • Trash to Treasure

      Trash to Treasure
    • Arduino Contest 2019

      Arduino Contest 2019

    Discussions