Basic LCD
Game
This project is a simple reflex-based dodge game using an Arduino and an ILI9341 TFT LCD display. The player controls a white square using push buttons to dodge falling red circles of varying sizes and speeds. Each dodged circle increases the score. Over time, the difficulty increases. Two abilities can be triggered: a speed boost (button A) and an invincibility mode (button B), both with cooldowns. If the square touches a circle while not invincible, the game ends and displays a game over screen. Pressing button A resets the game.
​​
Materials
To build a basic game with an Arduino and an LCD screen, you'll need the following materials:
Components:
​​​
1) Arduino Uno or Mega:
-
Runs the game logic and handles input/output
2) ILI9341 TFT LCD (SPI):
-
Displays the game graphics.
3) 6x Push Buttons:
-
For directional movement and special abilities (Up, Down, Left, Right, A, B).
4) 6x 10kΩ Resistors:
-
Pull-down resistors for the push buttons.
5) Breadboard and Jumper Wires:
-
For connecting all components.
​
6) USB Cable:
-
Connects the Arduino to your computer for programming and power.
Basic Setup


1) ILI9341 LCD:
-
VCC → 5V
-
GND → GND
-
CS → Pin 10
-
RESET → Pin 8
-
Ax / DC → Pin 9
-
SDA / SDI/MOSI→ Pin 11
-
SCK → Pin 13
-
LED → 5V (through 100Ω resistor optional)
​
2) Push Buttons:
-
UP → Pin 2
-
DOWN → Pin 3
-
LEFT → Pin 4
-
RIGHT → Pin 5
-
A → Pin 6 (Speed Boost)
-
B → Pin 7 (Invincibility)
Each push button is wired as follows:
-
Connect one side of the button to its assigned Arduino pin
-
Connect the other side of the button to 5V
-
Place a 10kΩ pull-down resistor between the Arduino pin and GND
This ensures:
-
When the button is not pressed, the Arduino pin is LOW (via the resistor)
-
When the button is pressed, it connects to 5V and reads HIGH
This configuration prevents floating inputs and false triggers.
​​
​Software Setup
​​
1) How to Install the Adafruit GFX Library
-
Open the Arduino IDE.
-
Go to Sketch → Include Library → Manage Libraries...
-
In the Library Manager, type “Adafruit GFX” in the search bar.
-
Look for “Adafruit GFX Library by Adafruit”.
-
Click Install.
-
Wait for the installation to complete.
​
2) How to Install the Adafruit ILI9341 Library
-
Open the Arduino IDE.
-
Go to Sketch → Include Library → Manage Libraries...
-
In the Library Manager, type “ILI9341” in the search bar.
-
Look for “Adafruit ILI9341 by Adafruit”.
-
Click Install.
-
Wait for the installation to complete.
​
3) SPI Library should already be installed
-
The SPI library is built into the Arduino IDE.
-
You do not need to install it manually.
-
You can include it in your code with:
#include <SPI.h>
CODE BREAK-DOWN
#include <SoftwareSerial.h>;
-
Includes the SoftwareSerial library, which allows the Arduino to use other digital pins for serial communication (used for Bluetooth here).
​
#define RED_PIN 9;
#define GREEN_PIN 10;
#define BLUE_PIN 11;
-
Assigns pin numbers for the red, green, and blue channels of the RGB LED.
​
#define MODE_BUTTON 2;
#define POWER_BUTTON 3;
-
Assigns pin numbers for the mode switch and power switch push buttons.
​
#define POT_RED A0;
#define POT_GREEN A1;
#define POT_BLUE A2;
-
Assigns analog pins to the potentiometers that manually control the red, green, and blue colours.
​
bool mode = false;
-
Declares a boolean variable to keep track of the mode — auto cycle or manual.
​
bool lampOn = true;
-
Declares a boolean to track whether the lamp is currently on or off.
​
SoftwareSerial BTSerial(6, 7);
-
Sets up software serial on pins 6 and 7 for Bluetooth communication using the HC-06 module.
​
void setup();
-
Begins the setup function, which runs once when the Arduino is powered on.
​
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
-
Sets the RGB LED pins as outputs so they can control the LED brightness.
​
pinMode(MODE_BUTTON, INPUT);
pinMode(POWER_BUTTON, INPUT);
-
Sets the mode and power buttons as inputs, assuming external pull-down resistors are used.
​
Serial.begin(9600);
BTSerial.begin(9600);
-
Starts serial communication on both the standard and Bluetooth serial ports at 9600 baud.
​
Serial.println("Bluetooth Mood Lamp Ready");
-
Prints a message to the Serial Monitor for confirmation that the program is running.
​
void loop();
-
Begins the main loop that runs repeatedly.
​
if (digitalRead(POWER_BUTTON) == HIGH);
-
Checks if the power button was pressed.
​
lampOn = !lampOn;
-
Toggles the lamp's on/off state.
​
Serial.println(lampOn ? "Lamp ON" : "Lamp OFF");
BTSerial.println(lampOn ? "Lamp ON" : "Lamp OFF");
-
Sends a message to the serial monitor and over Bluetooth about the lamp’s state.
​
delay(300);
-
Adds a short delay to debounce the button press.
​
if (digitalRead(MODE_BUTTON) == HIGH);
-
Checks if the mode button was pressed.
​
mode = !mode;
-
Toggles between auto cycle mode and manual control mode.
​
Serial.println(mode ? "Manual Mode" : "Auto Mode");
BTSerial.println(mode ? "Manual Mode" : "Auto Mode");
-
Sends the current mode to the serial monitor and Bluetooth.
​
delay(300);
-
Debounce delay for the mode button.
​
if (BTSerial.available());
-
Checks if there’s any incoming data from the Bluetooth module.
​
char command = BTSerial.read();
-
Reads the incoming character from Bluetooth.
​
handleBluetoothCommand(command);
-
Passes the received character to a function that handles different Bluetooth commands.
​
if (lampOn);
-
Checks if the lamp is supposed to be on.
​
if (mode);
-
If in manual mode…
​
manualControl();
-
Call the function that reads the potentiometers and sets the RGB LED.
​
else;
-
If in auto cycle mode…
​
colorCycle();
-
Call the function that automatically cycles through colours.
​
else;
-
If the lamp is off…
​
setRGB(0, 0, 0);
void colorCycle();
-
Starts the function that cycles through colours automatically.
​
for (int i = 0; i < 256; i++);
-
Gradually increases the red and decreases the green from 0 to 255.
​
setRGB(i, 255 - i, 128);
-
Updates the LED to a mix of red and green with fixed blue.
​
delay(10);
-
Small delay to slow down the transition.
​
for (int i = 255; i >= 0; i--);
-
Then fades the red out and blue in.
​
setRGB(i, 128, 255 - i);
-
Changes the colours again with fixed green.
​
delay(10);
-
Small delay for the second transition.
​
void manualControl();
-
Starts the function that reads potentiometers for manual control.
​
int red = analogRead(POT_RED) / 4;
-
Reads and scales the red potentiometer value (0–1023 to 0–255).
​
int green = analogRead(POT_GREEN) / 4;
-
Reads and scales the green potentiometer.
​
int blue = analogRead(POT_BLUE) / 4;
-
Reads and scales the blue potentiometer.
​
setRGB(red, green, blue);
-
Sets the RGB LED using the potentiometer values.
​
void setRGB(int r, int g, int b);
-
Starts a function to set the brightness of each RGB channel.
​
analogWrite(RED_PIN, r);
-
Sends PWM signal to red LED.
​
analogWrite(GREEN_PIN, g);
-
Sends PWM signal to green LED.
​
analogWrite(BLUE_PIN, b);
-
Sends PWM signal to blue LED.
​
void handleBluetoothCommand(char command);
-
Starts a function to handle commands from the Bluetooth terminal.
​
switch (command);
-
Begins checking which command was received.
​
case 'Power':
lampOn = !lampOn;
Serial.println(lampOn ? "Lamp ON" : "Lamp OFF");
BTSerial.println(lampOn ? "Lamp ON" : "Lamp OFF");
-
Toggles the lamp and reports status.
break;
​
case 'Mode':
mode = !mode;
Serial.println(mode ? "Manual Mode" : "Auto Mode");
BTSerial.println(mode ? "Manual Mode" : "Auto Mode");
-
Toggles the mode and reports it.
break;
​
case 'Red':
setRGB(255, 0, 0);
-
Sets the lamp to red.
break;
​
case 'Green':
setRGB(0, 255, 0);
-
Sets the lamp to green.
break;
​
case 'Blue':
setRGB(0, 0, 255);
-
Sets the lamp to blue.
break;
​
case 'Yellow':
setRGB(255, 255, 0);
-
Sets the lamp to yellow.
break;
​
case 'Cyan':
setRGB(0, 255, 255);
-
Sets the lamp to cyan.
break;
​
case 'Magenta':
setRGB(255, 0, 255);
-
Sets the lamp to magenta.
break;
​
case 'White':
setRGB(255, 255, 255);
-
Sets the lamp to white.
break;
