Project showcase

A Simple Simon Says Game © GPL3+

This lesson aims to show how to make a simple Arduino based Simon Says light game.

  • 8,010 views
  • 4 comments
  • 7 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
Breadboard 830 points with Wire Kit
×1
10 Red Led 5mm
×1
10 Yellow Led 5mm
×1
10 Green Led 5mm
×1
10 Blue Led 5mm
×1
5 PushButton 6x6
×1
10 jumper wires 100mm male
×1

About this project

1. How does it work?

When the button attached to A4 pin is pressed a sequence is generated and the software wait for the same sequence. If the two sequence are different the LEDs will blink for three times very fast otherwise the sequence is correct. If the sequence is correct the game goes ahead and the difficulty and velocity are incremented.

The two sequence are stored in two vectors and compared.

2. Schematic

Here is the schematic:

3. The code

Here is the code:

<p>/*This sketch is a simple version of the famous Simon Says game. You can use it and improved it adding<br />
levels and everything you want to increase the diffuculty!</p>
<p>There are five buttons connected to A0, A1, A2, A3 and A4.<br />
The buttons from A0 to A3 are used to insert the right sequence while A4 to start the game.</p>
<p>When a wrong sequence is inserted all the leds will blink for three time very fast otherwhise the<br />
inserted sequence is correct.</p>
<p>author : Arturo Guadalupi <a.guadalupi@arduino.cc><br />
Hardware needed:<br />
5x pushbuttons<br />
1x Blue led<br />
1x Yellow led<br />
1x Red led<br />
1x Green Led<br />
4x 1k resistors<br />
4x 10k resisors<br />
10x jumpers<br />
*/</p>
<p>const int MAX_LEVEL = 100;<br />
int sequence[MAX_LEVEL];<br />
int your_sequence[MAX_LEVEL];<br />
int level = 1;</p>
<p>int velocity = 1000;</p>
<p>void setup() {<br />
pinMode(A0, INPUT);<br />
pinMode(A1, INPUT);<br />
pinMode(A2, INPUT);<br />
pinMode(A3, INPUT);</p>
<p>pinMode(2, OUTPUT);<br />
pinMode(3, OUTPUT);<br />
pinMode(4, OUTPUT);<br />
pinMode(5, OUTPUT);</p>
<p>digitalWrite(2, LOW);<br />
digitalWrite(3, LOW);<br />
digitalWrite(4, LOW);<br />
digitalWrite(5, LOW);<br />
}</p>
<p>void loop()<br />
{<br />
if (level == 1)<br />
generate_sequence();//generate a sequence;</p>
<p>if (digitalRead(A4) == LOW || level != 1) //If start button is pressed or you're winning<br />
{<br />
show_sequence();    //show the sequence<br />
get_sequence();     //wait for your sequence<br />
}<br />
}</p>
<p>void show_sequence()<br />
{<br />
digitalWrite(2, LOW);<br />
digitalWrite(3, LOW);<br />
digitalWrite(4, LOW);<br />
digitalWrite(5, LOW);</p>
<p>for (int i = 0; i < level; i++)<br />
{<br />
digitalWrite(sequence[i], HIGH);<br />
delay(velocity);<br />
digitalWrite(sequence[i], LOW);<br />
delay(200);<br />
}<br />
}</p>
<p>void get_sequence()<br />
{<br />
int flag = 0; //this flag indicates if the sequence is correct</p>
<p>for (int i = 0; i < level; i++)<br />
{<br />
flag = 0;<br />
while(flag == 0)<br />
{<br />
if (digitalRead(A0) == LOW)<br />
{<br />
digitalWrite(5, HIGH);<br />
your_sequence[i] = 5;<br />
flag = 1;<br />
delay(200);<br />
if (your_sequence[i] != sequence[i])<br />
{<br />
wrong_sequence();<br />
return;<br />
}<br />
digitalWrite(5, LOW);<br />
}</p>
<p>if (digitalRead(A1) == LOW)<br />
{<br />
digitalWrite(4, HIGH);<br />
your_sequence[i] = 4;<br />
flag = 1;<br />
delay(200);<br />
if (your_sequence[i] != sequence[i])<br />
{<br />
wrong_sequence();<br />
return;<br />
}<br />
digitalWrite(4, LOW);<br />
}</p>
<p>if (digitalRead(A2) == LOW)<br />
{<br />
digitalWrite(3, HIGH);<br />
your_sequence[i] = 3;<br />
flag = 1;<br />
delay(200);<br />
if (your_sequence[i] != sequence[i])<br />
{<br />
wrong_sequence();<br />
return;<br />
}<br />
digitalWrite(3, LOW);<br />
}</p>
<p>if (digitalRead(A3) == LOW)<br />
{<br />
digitalWrite(2, HIGH);<br />
your_sequence[i] = 2;<br />
flag = 1;<br />
delay(200);<br />
if (your_sequence[i] != sequence[i])<br />
{<br />
wrong_sequence();<br />
return;<br />
}<br />
digitalWrite(2, LOW);<br />
}</p>
<p>}<br />
}<br />
right_sequence();<br />
}</p>
<p>void generate_sequence()<br />
{<br />
randomSeed(millis()); //in this way is really random!!!</p>
<p>for (int i = 0; i < MAX_LEVEL; i++)<br />
{<br />
sequence[i] = random(2,6);<br />
}<br />
}<br />
void wrong_sequence()<br />
{<br />
for (int i = 0; i < 3; i++)<br />
{<br />
digitalWrite(2, HIGH);<br />
digitalWrite(3, HIGH);<br />
digitalWrite(4, HIGH);<br />
digitalWrite(5, HIGH);<br />
delay(250);<br />
digitalWrite(2, LOW);<br />
digitalWrite(3, LOW);<br />
digitalWrite(4, LOW);<br />
digitalWrite(5, LOW);<br />
delay(250);<br />
}<br />
level = 1;<br />
velocity = 1000;<br />
}</p>
<p>void right_sequence()<br />
{<br />
digitalWrite(2, LOW);<br />
digitalWrite(3, LOW);<br />
digitalWrite(4, LOW);<br />
digitalWrite(5, LOW);<br />
delay(250);</p>
<p>digitalWrite(2, HIGH);<br />
digitalWrite(3, HIGH);<br />
digitalWrite(4, HIGH);<br />
digitalWrite(5, HIGH);<br />
delay(500);<br />
digitalWrite(2, LOW);<br />
digitalWrite(3, LOW);<br />
digitalWrite(4, LOW);<br />
digitalWrite(5, LOW);<br />
delay(500);</p>
<p>if (level < MAX_LEVEL);<br />
level++;</p>
<p>velocity -= 50; //increase difficulty<br />
}</p>

Code

Code snippet #1Arduino
/*This sketch is a simple version of the famous Simon Says game. You can use it and improved it adding
levels and everything you want to increase the diffuculty!

There are five buttons connected to A0, A1, A2, A3 and A4.
The buttons from A0 to A3 are used to insert the right sequence while A4 to start the game.

When a wrong sequence is inserted all the leds will blink for three time very fast otherwhise the
inserted sequence is correct.

Hardware needed:
5x pushbuttons
1x Blue led
1x Yellow led
1x Red led
1x Green Led
4x 1k resistors
4x 10k resisors
10x jumpers
*/

const int MAX_LEVEL = 100;
int sequence[MAX_LEVEL];
int your_sequence[MAX_LEVEL];
int level = 1;

int velocity = 1000;

void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}

void loop()
{
if (level == 1)
generate_sequence();//generate a sequence;

if (digitalRead(A4) == LOW || level != 1) //If start button is pressed or you're winning
{
show_sequence();    //show the sequence
get_sequence();     //wait for your sequence
}
}

void show_sequence()
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

for (int i = 0; i < level; i++)
{
digitalWrite(sequence[i], HIGH);
delay(velocity);
digitalWrite(sequence[i], LOW);
delay(200);
}
}

void get_sequence()
{
int flag = 0; //this flag indicates if the sequence is correct

for (int i = 0; i < level; i++)
{
flag = 0;
while(flag == 0)
{
if (digitalRead(A0) == LOW)
{
digitalWrite(5, HIGH);
your_sequence[i] = 5;
flag = 1;
delay(200);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(5, LOW);
}

if (digitalRead(A1) == LOW)
{
digitalWrite(4, HIGH);
your_sequence[i] = 4;
flag = 1;
delay(200);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(4, LOW);
}

if (digitalRead(A2) == LOW)
{
digitalWrite(3, HIGH);
your_sequence[i] = 3;
flag = 1;
delay(200);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(3, LOW);
}

if (digitalRead(A3) == LOW)
{
digitalWrite(2, HIGH);
your_sequence[i] = 2;
flag = 1;
delay(200);
if (your_sequence[i] != sequence[i])
{
wrong_sequence();
return;
}
digitalWrite(2, LOW);
}

}
}
right_sequence();
}

void generate_sequence()
{
randomSeed(millis()); //in this way is really random!!!

for (int i = 0; i < MAX_LEVEL; i++)
{
sequence[i] = random(2,6);
}
}
void wrong_sequence()
{
for (int i = 0; i < 3; i++)
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(250);
}
level = 1;
velocity = 1000;
}

void right_sequence()
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(250);

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(500);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(500);

if (level < MAX_LEVEL);
level++;

velocity -= 50; //increase difficulty
}

Schematics

Schematic 1
750x750 555453168f2b43 61252307 rvmuwsr8yz

Comments

Similar projects you might like

Arduino Uno-Based, Easy-to-Build Pet Feeder

Project tutorial by edr1924

  • 2,153 views
  • 3 comments
  • 20 respects

HOW PIR SENSOR WORK

Project tutorial by DIY Partners

  • 2,797 views
  • 6 comments
  • 21 respects

4-Stroke Digital Clock With Arduino

Project tutorial by LAGSILVA

  • 7,541 views
  • 8 comments
  • 30 respects

RGB Camera Backlight Control with Android Mobile

Project tutorial by DIY Partners

  • 176 views
  • 0 comments
  • 2 respects

"Living Art" Arduino-Controlled Desk Lamp

Project tutorial by Modustrial Maker

  • 1,678 views
  • 0 comments
  • 5 respects
Add projectSign up / Login