Hey everyone!
Today I am going to explain you about making a fun game with Arduino and you can also make your Arduino talk. In this game, a total of 10 random questions will be asked and you have to place the coloured sheet when the respective colour is displayed on the LCD screen, or called. You will be given 5 attempts and if you use all of them, the game will be over and you should start it again.
Hardware components used in this project
- Arduino Uno
- USB Type A/ B cable
- Ethernet Shield W5100/ SD card module
- SD card
- Solderless Breadboard Power rail - Half+
- TCS230/ TCS3200 colour sensor
- 16 x 2 LCD display module with I2C interface
- Audio Jack/ Speaker/ Amplifier
Hardware setup
Connections
TCS230/ TCS3200 Colour sensor
- GND - Ground
- OE - Ground
- S0 - D3
- S1 - D5
- S2 - D6
- S3 - D7
- Out - D8
- VCC - 5V
16 x 2 LCD display module with I2C interface
- GND - Ground
- VCC - 5V
- SDA - A4
- SCL - A5
Coding
The audio files will be stored in your SD card and you will be needing the TMRpcm library to access these files.
Libraries needed for this project:- TMRpcm
- SD - Inbuilt library
- Wire - Inbuilt library
- LiquidCrystal_I2C by fdebrabander
If you do not have these libraries, please feel free to follow the hyperlinks and download the libraries as ZIP folders. You can then add these ZIP folders to your Arduino IDE software by going to:
Sketch--->Include Library--->Add .ZIP Library...
If you are unsure about using TMRpcm, this documentation will help you:
Before moving on to the coding, you have to record your voice and convert those files from .mp3 to .wav format. You can use this online converter to convert the files before storing them in your SD card.
- Welcome - A message that plays when you start the game
- Red - Played when 'Red' is displayed in the LCD display module
- Red_c - Played when the correct Red sheet is placed
- Red_w - Played when the wrong colour sheet is placed
- Blue - Played when 'Blue' is displayed in the LCD display module
- Blue_c - Played when the correct Blue sheet is placed
- Blue_w - Played when the wrong colour sheet is placed
- Green - Played when 'Green' is displayed in the LCD display module
- Green_c - Played when the correct Green sheet is placed
- Green_w - Played when the wrong colour sheet is placed
- LBlue - Played when 'Light Blue' is displayed in the LCD display module
- LBlue_c - Played when the correct Light Blue sheet is placed
- LBlue_w - Played when the wrong colour sheet is placed
- Yellow - Played when 'Yellow' is displayed in the LCD display module
- Yellow_c - Played when the correct Yellow sheet is placed
- Yellow_w - Played when the wrong colour sheet is placed
- End - Played when the value stored in count variable (number of questions answered) reaches 10
- Gameover - Played when the value stored in tries variable reaches 5
Now let's move on to the coding. First of all, you will need to include the libraries, mentioned above, in your Arduino IDE sketch. Create TMRpcm instance. Define the colour sensor pins.Create global variables named chipSelect, Red, Green, Blue, Count, Score, Final score and Tries. Set the LCD address for a 16 chars and 2 line display. The chipSelect variable must be assigned to D4 if you are using Ethernet shield.
- Arduino Ethernet shield - D4
- Adafruit SD shields and modules - D10
- Sparkfun SD shield - D8
- MKRZero SD - SDCARD_SS_PIN
Create a new function named colour. You have to use void data type for this. This function will be used to measure the frequencies of Red, Green and Blue respectively.
void colour( ){
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
Red=pulseIn(sensorOut, LOW);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
Green=pulseIn(sensorOut, LOW);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
Blue=pulseIn(sensorOut, LOW);
}
Within void setup( ), initial the lcd and call the pinMode( ) function to configure the S0, S1, S2, and S3 pins as OUTPUT. The sensor output pin must be configured as INPUT. Set the data rate in 9600 baud for serial data transmission. Assign speakerPin to D9 if you are using Arduino Uno.
- Arduino Uno, Nano, etc. - D9
- Arduino Mega - D5, D6, D11 or D46
if(!SD.begin(SD_ChipSelectPin)){
Serial.println("SD fail");
return;
}
Use this code snippet to check if your SD card is connected to the Arduino microcontroller board.
Set the volume level of the speaker. The volume can be set from 0 to 7. Play 'welcome' file as the LCD display module shows 'Ready'. Set a delay period depending on the duration of the 'welcome' file.
Now, create a local variable named 'number' and assign it to the random number generator. You will have to generate a random number between 0 - 4 (inclusive) because we will be using five colours in this game: Blue, Green, Light Blue, Red and Yellow.
You must use the colour sensor and the colour function which you created, to measure the frequencies of Red, Green and Blue for each coloured sheet. Using these frequencies, you can call the 'if' function to help the Arduino detect the colours. If you are unsure about this, please visit my previous project: https://arduinoprojectsbyr.blogspot.com/2019/12/45-arduino-based-colour-detection.html.
Use 'switch...case' function to assign each colour to the numbers.
- 0 - Red
- 1 - Blue
- 2 - Green
- 3 - Light Blue
- 4 - Yellow
The top left corner of the LCD display must display the name of the colour, while the top right corner must display the number of tries. The number of tries will increase by one every time the wrong coloured sheet is placed within the gap. Once the number of tries reaches 5, the game will be over and the lcd display must show ' Game Over ' in the first row and ' Try again ' in the second row. The lcd display must be cleared before it shows the final score. The count, tries, and score variable must be reset to zero.
The bottom left corner of the LCD display must display the number of questions completed out of 10, and the bottom right corner must display the score. The score must increase by 10 every time the correct coloured sheet is placed in the gap. Once the number of questions completed reaches 10, your LCD display must show the final score. The count, tries, and score variable must be reset to zero.
If anyone has any questions with the coding, please feel free to send me an email at arduinoprojectsbyr@gmail.com.
Final Look
This is the final look of this project. Hope you enjoy this game!
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.
Comments
Post a Comment