In this arduino project I am going to make a simple mini game with leds. In this game the leds turn on and off the one after the other very quickly. When you push the button you see in the images the led that is on remains on and all the other stop blinking. The aim of this mini game is to push the button and make the middle led (the red led) stay on. If you don't succeed in doing it you just let the button and try again.

Step 1: Materials

You will need :

Arduino uno

1 breadboard


1 pushbutton

1 piezo buzzer

1 resistor

2 blue leds

2 green leds

2 yellow leds

1 red led

Step 2: Making the Circuit

Connect a blue led from pin 13 to ground. Connect a green led from pin 12 to ground. Connect a yellow led from pin 11 to ground. Connect the red led from pin 10 to ground. Connect a yellow led from pin 9 to ground. Connect a green led from pin 8 to ground. Connect a blue led from pin 6 to ground. Connect the button to pin 4 , 5v and to the resistor to ground. Connect the piezo buzzer from pin 2 to ground.

Step 3: Code

copy the code

int tap1 = 0;
int tap = 0; void setup(){ Serial.begin(9600); pinMode(13,OUTPUT); pinMode(12,OUTPUT); pinMode(11,OUTPUT); pinMode(10,OUTPUT); pinMode(9,OUTPUT); pinMode(8,OUTPUT); pinMode(6,OUTPUT); pinMode(4,INPUT); } void loop(){ blue1(); button(); green1(); button(); yellow1(); button(); red(); button(); yellow(); button(); green(); button(); blue(); button(); green3(); button(); yellow3(); button(); red2(); button(); yellow2(); button(); green2(); button(); } void blue1(){ if(tap == 0){ digitalWrite(13,HIGH); delay(100); digitalWrite(13,LOW); tap1 = tap1 + 1; }} void button(){ tap = digitalRead(4); Serial.print(tap); if(tap == 1){ if(tap1 == 1){ digitalWrite(12,HIGH); tone(2,100,20); delay(1); digitalWrite(12,LOW); } if(tap1 == 2){ digitalWrite(11,HIGH); tone(2,100,20); delay(1); digitalWrite(11,LOW); } if(tap1 == 3){ digitalWrite(10,HIGH); tone(2,500,20); delay(1); digitalWrite(10,LOW); } if(tap1 == 4){ digitalWrite(9,HIGH); tone(2,100,20); delay(1); digitalWrite(9,LOW); } if(tap1 == 5){ digitalWrite(8,HIGH); tone(2,100,20); delay(1); digitalWrite(8,LOW); } if(tap1 == 6){ digitalWrite(6,HIGH); tone(2,100,20); delay(1); digitalWrite(6,LOW); } if(tap1 == 0){ digitalWrite(13,HIGH); tone(2,100,20); delay(1); digitalWrite(13,LOW); }}} void green1(){ if(tap == 0){ digitalWrite(12,HIGH); delay(100); digitalWrite(12,LOW); tap1 = tap1 + 1; }} void yellow1(){ if(tap == 0){ digitalWrite(11,HIGH); delay(100); digitalWrite(11,LOW ); tap1 = tap1 + 1; }} void red(){ if(tap == 0){ digitalWrite(10, HIGH); delay(100); digitalWrite(10,LOW); tap1 = tap1 + 1; }} void yellow(){ if(tap == 0){ digitalWrite(9,HIGH); delay(100); digitalWrite(9,LOW); tap1 = tap1 + 1; }} void green(){ if(tap == 0){ digitalWrite(8,HIGH); delay(100); digitalWrite(8,LOW); tap1 = tap1 + 1; }} void blue(){ if(tap == 0){ digitalWrite(6,HIGH); delay(100); digitalWrite(6,LOW); tap1 = tap1 - 1; }} void green3(){if(tap == 0){ digitalWrite(8,HIGH); delay(100); digitalWrite(8,LOW); tap1 = tap1 - 1; }} void yellow3(){ if(tap == 0){ digitalWrite(9,HIGH); delay(100); digitalWrite(9,LOW); tap1 = tap1 - 1; }} void red2(){ if(tap == 0){ digitalWrite(10,HIGH); delay(100); digitalWrite(10,LOW); tap1 = tap1 - 1; }} void yellow2(){ if(tap == 0){ digitalWrite(11,HIGH); delay(100); digitalWrite(11,LOW); tap1 = tap1 - 1; }} void green2(){ if(tap == 0){ digitalWrite(12,HIGH); delay(100); digitalWrite(12,LOW); tap1 = 0; }}
<p>Hi! </p><p>NYCSPARX was able to complete the project with a few tweaks in design! The young lady who put it together will be showcasing this at an event we are holding here in NYC on December 9th @ the Chelsea Recreation Center (6pm).</p><p>The instructions we spot on and easy for her to follow with my guidance and I was able to explain the coding to her thanks to your help!</p><p>Thank you so much for your assistance!</p>
<p>HI!</p><p>I am working on this for my students to recreate. Would you be able to explain the code some? I kind of understand, but I am not as experienced as I would like so though it makes sense, I am not sure why you did some of the code you did and why it works LOL</p>
<p> The code ended like this because while making it sometimes it didn't work as it should and I made a lot of changes.<strong> </strong></p><p><strong>void setup(){ </strong></p><p><strong>Serial.begin(9600); </strong></p><p><strong>pinMode(13,OUTPUT);</strong></p><p><strong><br>pinMode(12,OUTPUT); </strong></p><p><strong>pinMode(11,OUTPUT); </strong></p><p><strong>pinMode(10,OUTPUT); </strong></p><p><strong>pinMode(9,OUTPUT); </strong></p><p><strong>pinMode(8,OUTPUT); </strong></p><p><strong>pinMode(6,OUTPUT); </strong></p><p><strong>pinMode(4,INPUT);<br>}<br></strong></p><p>In the void setup I put the pins and if they are inputs or outputs. Also, I use the command Serial.begin so I can use later the command Serial.print and print value tap on the serial monitor. This isn't necessary, I only did it to see if the button was working correctly.</p><p><strong>void loop(){</strong></p><p><strong><br>blue1();</strong><b style="">button(); green1();</b></p><p><b style="">button(); yellow1(); button(); </b></p><p><b style="">red(); button(); yellow();</b></p><p><b style=""><br>button(); green(); button(); </b></p><p><b style="">blue(); button(); green3(); </b></p><p><b style="">button(); yellow3(); button(); </b></p><p><b style="">red2(); button(); yellow2(); </b></p><p><b style="">button(); green2(); button();<br>}</b></p><p>In the void loop I have placed other loops. If I didn't do this I would had to write the whole program<b style=""> button</b> again and again every time I used this program. Except for <b style="">button </b>I also placed loops for the colors ( that wasn't necessary).</p><p><strong>void blue1(){</strong></p><p><strong><br>if(tap == 0){</strong></p><p><strong><br>digitalWrite(13,HIGH); </strong></p><p><strong>delay(100);</strong></p><p><strong><br>digitalWrite(13,LOW); </strong></p><p><strong>tap1 = tap1 + 1;<br>}}</strong><br></p><p>All the color loops are the same. First I put an <strong>if </strong>statement to see if tap = 0 (if the button is pressed). If it is true then the led turns on then it delays for 100 and the led turns off. After that it increases<strong> tap1</strong> by 1. I use <strong>tap1 </strong>so that the arduino can know witch is the led that turned off when you pressed the button to light up the next as long as you press the button.</p><p><strong>void button(){</strong></p><p><strong><br>tap = digitalRead(4);</strong></p><p><strong><br> Serial.print(tap);</strong></p><p>In this part of the code in loop <strong>button </strong>I am saying to arduino witch is the value <strong>tap</strong>.</p><p><strong>if(tap1 == 1){ </strong></p><p><strong>digitalWrite(12,HIGH);</strong></p><p><strong><br>tone(2,100,20);</strong></p><p><strong><br>delay(1); </strong></p><p><strong> digitalWrite(12,LOW);<br>}</strong></p><p>This <strong>if </strong>statement in loop <strong>button </strong>is similar to all the others in it. If it is true whats in the parenthesis the led on this pin lights up and the buzzer makes noise. I have put for delay 1. I did this because if I hadn't put delay the led that was on while I was pressing the button would stay on until the code makes it turn off. If I had put for delay 50 and more the led would stay on for a very small time and then turn off.Also, the buzzer would sound for 1 moment and then stop for as time as the delay is. </p>
What part of the code makes the light pause when you press it?<br> <br> I am trying hard to decode so I can better understand how this works :-)<br> <br> <em><strong>Thank you for the break down of the rest of the code!</strong></em> I really do appreciate it! As a novice I am always looking to expand so I can answer the student's questions accurately!

About This Instructable




More by Peter12345678910:Arduino Led Mini Game Arduino color mixing light 
Add instructable to: