Arduino Led Mini Game

7.1K318

Intro: Arduino Led Mini Game

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

cables

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; }}

4 Comments

why only one push button is used in the circuit?

HI!

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

The code ended like this because while making it sometimes it didn't work as it should and I made a lot of changes.

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);
}

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.

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();
}

In the void loop I have placed other loops. If I didn't do this I would had to write the whole program button again and again every time I used this program. Except for button I also placed loops for the colors ( that wasn't necessary).

void blue1(){


if(tap == 0){


digitalWrite(13,HIGH);

delay(100);


digitalWrite(13,LOW);

tap1 = tap1 + 1;
}}

All the color loops are the same. First I put an if 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 tap1 by 1. I use tap1 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.

void button(){


tap = digitalRead(4);


Serial.print(tap);

In this part of the code in loop button I am saying to arduino witch is the value tap.

if(tap1 == 1){

digitalWrite(12,HIGH);


tone(2,100,20);


delay(1);

digitalWrite(12,LOW);
}

This if statement in loop button 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.

What part of the code makes the light pause when you press it?

I am trying hard to decode so I can better understand how this works :-)

Thank you for the break down of the rest of the code! I really do appreciate it! As a novice I am always looking to expand so I can answer the student's questions accurately!