1. How does it work?
3. The code
Read moreWhen 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. SchematicHere is the schematic:
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>
Comments