Hey Everyone!
I hope that everyone's safe during this pandemic. Today I am going to explain you about the second version of my Buzz wire game. People who are bored of staying indoors can find this game pretty interesting and fun to play.
If you are a beginner, you can start off with my first version of this project : 51. Buzz wire game using Arduino
Hardware components used in this project
- Arduino Uno
- Solderless Breadboard (x2) - Full/ Full+
- LCD display module with I2C module - 16x2
- Potentiometer - B20K
- Push-buttons (x3)
- Resistor - 220 Ω
- Active Buzzer module (KY-012)
- LED - Red
- Copper Wire - 19/ 20 gauge thick
- Male-to-Male Jumper wires - 10cm and 20cm
- Wire - Long enough to connect the loop of copper wire to ground
Other tools required for this project
- Cutting pliers - To cut the right amount of copper wire
- Round-nose pliers - To bend the copper wire and make a maze and loop
Hardware setup
Connections
- Wire Maze - D2
- Wire loop - Ground (GND)
B20K Potentiometer
- S - A0
- (+) - 5V
- (-) - Ground
16x2 LCD display module
- GND - Ground
- VCC - 5V
- SDA - A4
- SCL - A5
Push-buttons
- Button 01 (To change between modes) - D7
- Button 02 - D3
- Button 03 - D4
- (-) - Ground
*Note: I have used INPUT_PULLUP for my push-buttons so they will not be needing the 10kΩ resistors. The push-buttons will not be connected to 5V in this case.
Active Buzzer module (KY-012)
- S - D5
- (+) - 5V
- (-) - Ground (GND)
LED
- Anode (+) - D6
- Cathode (-) - Ground (GND)
Coding
I have received many emails requesting the code for the first version. For those who do not understand how this works, I will explain you the basic concept of this game before moving on to the coding.
One end of the wire maze is connected to D2 and is configured as INPUT_PULLUP. When the loop touches the maze, there will be a conductivity and the circuit will be complete. Once the circuit is complete, the maze will send a LOW signal to the Arduino Uno microcontroller. A LOW signal is sent because of the INPUT_PULLUP mode. A HIGH signal will be sent to your microcontroller when the loop is not in contact with the wire maze. To learn about the INPUT_PULLUP mode, please visit this website: https://www.arduino.cc/en/Tutorial/DigitalPins.
You can check this by using the code in the image below:
I have used three push-buttons in this project. One button will be used to change between different game modes.
Game Modes
- Mode 01: One life - You touch, you lose
- Mode 02: Five lives - You will be given 5 lives to win this game
- Mode 03: Timer - This is time-based. You must finish the game successfully within the given time
For the first two modes, the button connected to D3 will be used to finish the game and get your scores; the second button will be used to reset the game for the second mode. In the third mode, the D3 button will be used to set the timer, while the second button will be used to start the game. After you complete the game, press the first button again to show you the amount of time taken to complete the game.
Back to the coding...
You will need the Arduino LiquidCrystal I2C library and Wire library for this project. You can download the .ZIP version of this library from GitHub by following the hyperlink. The Wire library is a built-in library. To add this .ZIP folder to your Arduino IDE, go to sketch----> include library---->Add .ZIP library.
Set the LCD address to 0x27 for a 16 chars and 2 line display. You will be need to create global variables for your components and to store the values produced by the sensors.
Within void setup( ), print "Welcome" on your LCD, and call pinMode( ) to configure the pins of your components as INPUT, OUTPUT, and INPUT_PULLUP.
- Wire Maze: INPUT_PULLUP
- Push-buttons: INPUT_PULLUP
- Potentiometer: INPUT
- Active Buzzer: OUTPUT
- LED: OUTPUT
Within void loop( ), you will first need to program your mode button to debounce. Even though the push-buttons are configured as INPUT_PULLUP, these codes work fine during debouncing. The state must increase by 1 for each button press till it reaches 2.
When the value stored in the state variable is:
- 0: Mode 01 (Default mode)
- 1: Mode 02
- 2: Mode 03
After the value stored in the state variable exceeds 2, it must be assigned to 0. Now I'll move on to the coding for each game mode.
Mode 01: One Life
Program your LCD to display something similar to that shown in the image above. As I already explained earlier, the wire maze sends a LOW signal when touched by the loop. When a LOW signal is sent, the buzzer and LED must be set HIGH for one second to make it sound like an alarm. The LCD should display "You lose!". If you complete the game without touching the wire maze, press the push-button connected to D3. Pressing the button will send a LOW signal to your microcontroller, and once the LOW signal is received, the LCD should display "Congratulations!" in the first row and "You won!" in the second row.
Mode 02: Five Lives
Program your LCD to display "Mode 02:" in the first row and "05 Lives" in the second row. When the wire maze sends a LOW signal, the count must decrease by 1, and the Red LED and buzzer must be set HIGH for every second. Every time your loop touches the wire maze, the LCD should display the number of lives left. The number of lives left should be calculated by subtracting the counts from 5. When the number of counts reaches 5, the LCD must display "You Lose!" and "Game Over". If you complete the game before the number of counts reaches 5, press the push-button connected to D3 to display your final score. The score must be calculated by multiplying the number of lives left by 20. If you have lost the game, press the push-button connected to D4 to reset the game. To reset the game, you must assign the count variable to 0.
Score: [ (5-count)/5 * 100 ] % = [ (5-count) * 20 ] %
Mode 03: Time-based
This mode will use the countdown timer and you must complete the game before the countdown reaches zero. Whenever the loop touches the maze, the time left will decrease by 2 instead of 1. Press the push-button connected to D3 to set the countdown timer. The countdown timer can be set by the potentiometer. The analog reading produced by the potentiometer will be mapped to the number of seconds for the countdown timer. For this process, you can use the map function. Press this button again to stop the process. Press the push-button connected to D4 to start the countdown timer. After you complete the game, press the push-button connected to D3 to display the number of time taken to complete the game.
To learn how to make a countdown timer, please visit my previous post.
I hope that you would have understood about the coding of this project, but if you have any questions, please do not hesitate to comment below or send me an email at arduinoprojectsbyr@gmail.com.
Final Look
If anyone has any questions or suggestions about this project, please feel free to comment below or send me an email at arduinoprojectsbyr@gmail.com.
Güzel çalışma olmuş.4 yıldır bu projeyi arıyordum.
ReplyDeleteRica etsem kodları gönderbilirmisin
Selamlar