Difference between revisions of "F24: Tilt Maze"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Schedule)
(Project Source Code)
 
(80 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Project Title ==
+
 
Tilt Maze
+
[[File:maze.jpg|thumb|450px|caption|right|Tilt maze Logo]]<BR/>
 +
 
 +
== Hardware and gameplay snapshots ==
 +
 
 +
<center>
 +
<table>
 +
    <tr>
 +
        <td>
 +
            [[File:Main Tilt_maze.jpg|240px|thumb|left|Game Introduction]]
 +
        </td>
 +
        <td>
 +
            [[File:Maze1.jpg|240px|thumb|center|Maze1]]
 +
        </td>
 +
        <td>
 +
            [[File:Maze2.jpeg|240px|thumb|left|Maze2]]
 +
        </td>
 +
        <td>
 +
            [[File:Maze3.jpeg|240px|thumb|left|Maze3]]
 +
        </td>
 +
    </tr>
 +
    <tr>
 +
        <td>
 +
            [[File:levelup.jpg|240px|thumb|left|Level Up]]
 +
        </td>
 +
        <td>
 +
            [[File:You lose.jpg |240px|thumb|center|You Lose]]
 +
        </td>
 +
        <td>
 +
          [[File:You Win.jpg|240px|thumb|center|You Win]]
 +
        </td>
 +
    </tr>
 +
</table>
 +
</center>
  
 
== Abstract ==
 
== Abstract ==
Line 6: Line 38:
  
 
== Objectives & Introduction ==
 
== Objectives & Introduction ==
Show list of your objectives. This section includes the high level details of your project.  You can write about the various sensors or peripherals you used to get your project completed.
+
The Tilt Maze Game combines hardware and software to create an interactive puzzle experience. It uses an ADXL345 accelerometer for tilt-based movement control, navigating a character through a maze displayed on a 64x64 LED matrix. FreeRTOS manages concurrent tasks like accelerometer input, display updates, and game logic, ensuring smooth and responsive gameplay. Game states, collision detection, and immersive audio feedback via an MP3 decoder enhance the experience. Semaphores and mutexes ensure thread-safe resource management, while debug outputs provide insights during development. This project demonstrates advanced integration of peripherals and real-time systems in a cohesive gaming application.
  
=== Team Members & Responsibilities ===
+
== '''TEAM MEMBERS AND RESPONSIBILITIES''' ==
* Shreya Belide
+
*'''Shreya Belide''' 
**   
+
**  Developed code for MP3 Decoder Driver to play background music and sound effects. 
*  Jyoshna Mallineni
+
**  Designed and implemented Game Architecture and State Machine logic. 
**   
+
**  Developed collision detection logic to ensure smooth player interactions. 
*  Pavan Charith
+
**  Bug fixes in MP3 integration with FreeRTOS tasks.  
**
+
**  Integrated subsystems including audio, display, and game logic. 
 +
**  Code Cleanup and Optimization. 
 +
**  Game Packaging and Presentation Preparation.
 +
 
 +
*'''Jyoshna Mallineni''' 
 +
**  Developed Accelerometer Driver for tilt-based control. 
 +
**  Designed Maze Layouts and Game Graphics. 
 +
**  Implemented logic for dynamic player movement and boundary restrictions. 
 +
**  Developed and tested display rendering functions for the LED Matrix. 
 +
**  Debugged issues with accelerometer sensitivity and movement logic. 
 +
**  Assisted in subsystem integration and game logic testing.  
 +
**  Code Cleanup and Documentation.
 +
 
 +
*'''Pavan Charith Devarapalli''' 
 +
**   Developed LED Matrix Driver for rendering the game visuals. 
 +
**  Implemented multi-level gameplay logic and player progression. 
 +
**  Designed logic for traps and goal conditions in the maze. 
 +
**  Integrated MP3 Decoder with SPI communication for audio playback. 
 +
**  Debugged synchronization issues between subsystems. 
 +
**  Tested and verified power supply stability for all components. 
 +
**  Assisted with subsystem integration and final game testing. 
 +
**  Managed Git Repository and Final Report Preparation.
  
 
== Schedule ==
 
== Schedule ==
Line 117: Line 170:
 
|
 
|
 
* <span style="color:green">Completed</span>
 
* <span style="color:green">Completed</span>
* <span style="color:orange">In progress</span>
+
* <span style="color:green">Completed</span>
 
|-
 
|-
 
! scope="row"| 8
 
! scope="row"| 8
Line 129: Line 182:
 
* Update the wiki page.
 
* Update the wiki page.
 
|
 
|
* <span style="color:orange">In Progess</span>
+
* <span style="color:green">Completed</span>
* <span style="color:orange">In progess</span>
+
* <span style="color:green">Completed</span>
* <span style="color:orange">In progess</span>
+
* <span style="color:green">Completed</span>
 
|-
 
|-
 
! scope="row"| 9
 
! scope="row"| 9
Line 142: Line 195:
 
*Test pause/play functionality
 
*Test pause/play functionality
 
|
 
|
* <span style="color:orange">In Progess</span>
+
* <span style="color:green">Completed</span>
* <span style="color:red">Not started</span>
+
* <span style="color:green">Completed</span>
* <span style="color:red">Not started</span>
+
* <span style="color:green">Completed</span>
 
|-
 
|-
 
! scope="row"| 10
 
! scope="row"| 10
Line 153: Line 206:
 
*12/14/2024
 
*12/14/2024
 
|
 
|
*12/16/2024
+
*12/18/2024
*12/16/2024
+
*12/18/2024
*12/16/2024
+
*12/18/2024
*12/16/2024
+
*12/18/2024
 
|
 
|
 
*Final Demo
 
*Final Demo
Line 163: Line 216:
 
*Update the wiki page.
 
*Update the wiki page.
 
|
 
|
* <span style="color:red">Not started</span>
+
* <span style="color:green">Completed</span>
* <span style="color:red">Not started</span>
+
* <span style="color:green">Completed</span>
* <span style="color:red">Not started</span>
+
* <span style="color:green">Completed</span>
* <span style="color:red">Not started</span>
+
* <span style="color:green">Completed</span>
 
|-
 
|-
 
|}
 
|}
Line 172: Line 225:
 
<BR/>
 
<BR/>
  
== Parts List & Cost ==
+
== '''BILL OF MATERIALS''' ==
Give a simple list of the cost of your project broken down by components. Do not write long stories here.
+
{| class="wikitable"
 +
|-
 +
! scope="col"| Part
 +
! scope="col"| #
 +
! scope="col"| Cost
 +
! scope="col"| Source
 +
|-
 +
| scope="row"| SJ2 Board
 +
| 1
 +
| $50.00
 +
| Preet
 +
|-
 +
| Sparkfun RGB (64x64) LED Matrix Display
 +
| 1
 +
| $32.72
 +
| [https://www.amazon.com/Matrix-Raspberry-192x192-Adjustable-Brightness/dp/B0BYJHMFSQ?pd_rd_w=NmMS9&content-id=amzn1.sym.55f2405b-2aa3-4fa1-95e2-48a0da8f4e9a&pf_rd_p=55f2405b-2aa3-4fa1-95e2-48a0da8f4e9a&pf_rd_r=KM222Q1XK3A6W1CQFCZ0&pd_rd_wg=mDBXC&pd_rd_r=b2e3ab88-9bd0-42a9-9333-bdf1a382c58e&pd_rd_i=B0BYJHMFSQ&psc=1&ref_=pd_bap_d_grid_rp_0_2_t Amazon]
 +
|-
 +
|Accelerometer
 +
|1
 +
| $10.99
 +
|[https://www.amazon.com/EC-Buying-Accelerometer-acceleration-Inclination/dp/B0CZRKMWJ6?pd_rd_w=s6gDO&content-id=amzn1.sym.55f2405b-2aa3-4fa1-95e2-48a0da8f4e9a&pf_rd_p=55f2405b-2aa3-4fa1-95e2-48a0da8f4e9a&pf_rd_r=2WHYH8ZV2B5RMV3048D5&pd_rd_wg=M0L51&pd_rd_r=ff1b1a8c-f89a-41e9-b09d-28894a7f3360&pd_rd_i=B0CZRKMWJ6&psc=1&ref_=pd_bap_d_grid_rp_0_1_pr_t Amazon]
 +
|-
 +
 
 +
| 12v DC Power Jack Adapter Connector
 +
| 1
 +
| $8.90
 +
| [https://www.amazon.com/Facmogu-Switching-Transformer-Compatible-5-5x2-1mm/dp/B087LY41PV?pd_rd_w=1Xhyw&content-id=amzn1.sym.55f2405b-2aa3-4fa1-95e2-48a0da8f4e9a&pf_rd_p=55f2405b-2aa3-4fa1-95e2-48a0da8f4e9a&pf_rd_r=DQFQDBDTENPKGMDAGG43&pd_rd_wg=H3giI&pd_rd_r=6c473ce8-3ba9-45f8-8f41-a758faaffb24&pd_rd_i=B087LY41PV&ref_=pd_bap_d_grid_rp_0_3_i&th=1 Amazon]
 +
|-
 +
| MP3 Decoder
 +
| 1
 +
| 6.90
 +
| [https://www.amazon.com/HiLetgo-Lossless-Decoders-Decoding-Amplifier/dp/B01DK9SL6C/ref=sr_1_5?crid=U2L84PXXYK3D&dib=eyJ2IjoiMSJ9.HxUtC3QbUkhr71XZFU1lSs0pwJLDKhP7AqHReBu0DMQH5m9g5hCAYRipgp8v3-W4Mt2v1i8101SPm-h33os9OVcBw5Axj4kOsu73UIXO-fqALYwiQ9lH2lvrli3Rtf5z3aSDa1hPbQxPUhZW0xPWK7B_vNu425CYtYOJWetc61jiXLlHUS6UcpRP4hNys3i54dvQ4ihJzwggyhGB7UjevL6-riwATgQ70aAmNgdQJ-I.0diWaio-K43an3PQy-Y72Bmlx1xl9ZG6IraVuBYHTJc&dib_tag=se&keywords=mp3+decoder+board&qid=1734906036&sprefix=mp3+decoder+%2Caps%2C177&sr=8-5 Amazon]
 +
|-
 +
| Packaging
 +
| 1
 +
| $12
 +
| Target
 +
|-
 +
| Jumper Wires
 +
| 1
 +
| $6.99
 +
| [https://www.amazon.com/EDGELEC-Breadboard-Optional-Assorted-Multicolored/dp/B07GD2BWPY/ref=sr_1_1_sspa?crid=1Y52WIGYO7XDK&dib=eyJ2IjoiMSJ9.tjHxIQLJsk16_0YVtUGN6S6iuxj4sNk3ew3kIjjT563F6ii_i5SGnTjyBAFWsS_C9W2RECAGZD3i33XGrbvJLJPhiCpWuKaAALpmsCJ5WXntOWkK9xnpwEzeilqQous0ZEJO2bQFAZeuSc2LVErEsC1Ksh7TOyj6OQEUBSq6Pm-HRC5BpyCTykn1PqhxDfJdZRY6xpyQI46Xr3lE6izXV3CP82HFbNzfS6Nfp3HIx_8.4Sk8yWdUTj6qW03LwWgtIFh1aem_Lz4yWG9TOqy-XXA&dib_tag=se&keywords=jumper%2Bwires&qid=1734906075&sprefix=jumper%2Bwires%2Caps%2C250&sr=8-1-spons&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY&th=1 Amazon]
 +
|-
 +
| Total Cost
 +
|
 +
| $128.5
 +
|
 +
|}
  
 
== Design & Implementation ==
 
== Design & Implementation ==
Line 180: Line 280:
 
=== Hardware Design ===
 
=== Hardware Design ===
 
Discuss your hardware design here.  Show detailed schematics, and the interface here.
 
Discuss your hardware design here.  Show detailed schematics, and the interface here.
 +
 +
===RGB LED MATRIX===
 +
 +
It is composed of two upper and lower sectional LED pannels. Each pannel has R, G, B led channels and A, B, C, and D row control registers. Addtionally, column shift is controlled by Latch bit, clock is controled by CLK, and OE turns LED off when switching rows.
 +
 +
<center>
 +
<table>
 +
<tr>
 +
<td>
 +
Connectivity Table LED Matrix to Sj2 Board:
 +
{| class="wikitable"
 +
|-
 +
! align="center"|RGB LED Matrix
 +
! align="center"|Pin Description
 +
! align="center"|SJ2 Board
 +
|-
 +
| scope="row" align="center"|R1
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P1_14
 +
|-
 +
| scope="row" align="center"|G1
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P4_29
 +
|-
 +
| scope="row" align="center"|B1
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P0_7
 +
|-
 +
| scope="row" align="center"|R2
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P0_9
 +
|-
 +
| scope="row" align="center"|G2
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P0_25
 +
|-
 +
| scope="row" align="center"|B2
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P1_30
 +
|-
 +
| scope="row" align="center"|A
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P1_23
 +
|-
 +
| scope="row" align="center"|B
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P1_29
 +
|-
 +
| scope="row" align="center"|C
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P2_4
 +
|-
 +
| scope="row" align="center"|D
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P2_6
 +
|-
 +
| scope="row" align="center"|CLK
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P2_8
 +
|-
 +
| scope="row" align="center"|LAT
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P0_17
 +
|-
 +
| scope="row" align="center"|OE
 +
| scope="row" align="center"|GPIO
 +
| scope="row" align="center"|P0_16
 +
|-
 +
| scope="row" align="center"|VCC
 +
| scope="row" align="center"|5VIN
 +
| scope="row" align="center"|External Power Supply
 +
|-
 +
| scope="row" align="center"|GND
 +
| scope="row" align="center"|GND
 +
| scope="row" align="center"|On Board
 +
|-
 +
|}
 +
</td>
 +
<td>
 +
[[File:rgbfront.jpg|500px|thumb|left|LED Matrix Front]]
 +
</td>
 +
<td>
 +
</td>
 +
<td>
 +
[[File:rgbback.jpg|500px|thumb|right|LED Matrix Rear]]
 +
</td>
 +
</tr>
 +
</table>
 +
</center>
 +
 +
===Accelerometer===
 +
 +
The ADXL345 accelerometer plays a central role in detecting tilt and translating it into game interactions. Here's a detailed breakdown of its purpose and usage:
 +
Tilt Detection: The ADXL345 measures acceleration along the X, Y, and Z axes, allowing you to detect the orientation and movement of the system.
 +
Game Control: The tilt input can serve as a controller for your tilt maze game. By tilting the device, you can move an object (like a ball) on the LED matrix to navigate the maze.
 +
 +
<center>
 +
<table>
 +
<tr>
 +
<td>
 +
Connectivity Table LED Matrix to Sj2 Board:
 +
{| class="wikitable"
 +
|-
 +
! align="center"|ADXL345 Pin
 +
! align="center"|Pin Description
 +
! align="center"|SJ2 Board
 +
|-
 +
| scope="row" align="center"|VCC
 +
| scope="row" align="center"|Power supply for the accelerometer
 +
| scope="row" align="center"|P1_14
 +
|-
 +
| scope="row" align="center"|GND
 +
| scope="row" align="center"|Ground Connections
 +
| scope="row" align="center"|P4_29
 +
|-
 +
| scope="row" align="center"|SCL
 +
| scope="row" align="center"|cLOCK Signal for i2C
 +
| scope="row" align="center"|P0_7
 +
|-
 +
| scope="row" align="center"|SDA
 +
| scope="row" align="center"|Data Signal fro I2C
 +
| scope="row" align="center"|P0_9
 +
|-
 +
| scope="row" align="center"|CS
 +
| scope="row" align="center"|To enable to i2C mode
 +
| scope="row" align="center"|P
 +
|-
 +
|}
 +
 +
<td>
 +
[[File:Accelerometer.jpg|200px|thumb|left|ADXL345 Accelerometer]]
 +
</td>
 +
<td>
 +
</td>
 +
</tr>
 +
</table>
 +
</center>
 +
 +
===MP3 Decoder===
 +
The MP3 decoder model Hiletgo GD3300 adds audio functionality, such as background music or sound effects for tilt maze game. Here's a concise explanation of its role and how to integrate it:
 +
 +
Background Music: Play ambient or thematic music to enhance the gaming experience.
 +
Sound Effects: Trigger sounds (e.g., collision, level-up, or maze completion) based on game events.
 +
 +
<table>
 +
<tr>
 +
<td>
 +
Connectivity Table MP3 Decoder  to Sj2 Board:
 +
{| class="wikitable"
 +
|-
 +
! align="center"|Decoder pin
 +
! align="center"|Pin Description
 +
! align="center"|SJ2 Board
 +
|-
 +
| scope="row" align="center"|VCC
 +
| scope="row" align="center"|Power supply for the accelerometer
 +
| scope="row" align="center"|5V
 +
|-
 +
| scope="row" align="center"|GND
 +
| scope="row" align="center"|Ground Connections
 +
| scope="row" align="center"|GND
 +
|-
 +
| scope="row" align="center"|TX
 +
| scope="row" align="center"|UART transmit pin (sends status and responses to the microcontroller).
 +
| scope="row" align="center"|P0_0
 +
|-
 +
| scope="row" align="center"|RX
 +
| scope="row" align="center"|UART receive pin (receives commands from the microcontroller).
 +
| scope="row" align="center"|P0_1
 +
|-
 +
|}
 +
 +
<td>
 +
[[File:mp3 decoder.jpg|200px|thumb|left|MP3 Decoder Hiletgo GD3300 ]]
 +
</td>
 +
<td>
 +
</td>
 +
</tr>
 +
</table>
 +
</center>
  
 
=== Hardware Interface ===
 
=== Hardware Interface ===
In this section, you can describe how your hardware communicates, such as which BUSes used.  You can discuss your driver implementation here, such that the '''Software Design''' section is isolated to talk about high level workings rather than inner working of your project.
+
* **LED Matrix Display**: 13 GPIO channels on SJ2 microcontroller
 +
* **Accelerometer (ADXL345)**: I2C communication on SJ2 microcontroller (SCL, SDA)
 +
* **MP3 Decoder**: SPI communication using MOSI, CS, SCK on SJ2 microcontroller
 +
* **Speaker**: AUX cord for audio output
 +
* **Power Supply**: 5V/4A adapter for powering the LED matrix and SJ2 microcontroller
  
 
=== Software Design ===
 
=== Software Design ===
Show your software designFor example, if you are designing an MP3 Player, show the tasks that you are using, and what they are doing at a high level. Do not show the details of the code. For example, do not show exact code, but you may show psuedocode and fragments of code. Keep in mind that you are showing DESIGN of your software, not the inner workings of it. 
+
* **LED Matrix: 
 +
**1. Initialized LED matrix connected pins to board IOs. 
 +
**2. Designed matrix driver for screen display by rendering maze patterns and player movements. 
 +
 
 +
* **Accelerometer**: 
 +
**1. Initialized I2C communication for ADXL345 accelerometer. 
 +
**2. Configured accelerometer in measurement mode and set sensitivity to ±2g. 
 +
**3. Processed tilt data to calculate real-time player movement commands. 
 +
 
 +
* **Mp3 Player**: 
 +
**1. Initialized using UART2. 
 +
**2. Set the device to the selected SD card and configured volume level.   
 +
**3. Played background music and sound effects based on game state.
 +
 
 +
===Implementation ===
 +
* **LED Matrix Driver Functions**:
 +
* LED Matrix:
 +
**1. Initialized LED matrix connected pins to board IOs.
 +
**2. Designed matrix driver for screen display by reading an matrix.
 +
**3. Designed pause menu, main menu and gameplay environment for different states of game.
 +
**4. Used GPIO pins as buttons as controls in main menu
 +
 
 +
 
 +
* **Accelerometer Driver**:
 +
**1. accelerometer_init: Configures the ADXL345 accelerometer with ±2g sensitivity and sets up I2C communication with semaphores for thread safety
 +
**2. accelerometer_task: Periodically reads acceleration data, applies smoothing, and updates player position based on tilt movements
 +
 
 +
// Add acceleration smoothing
 +
static int16_t last_x = 0;
 +
static int16_t last_y = 0;
 +
const float smoothing_factor = 0.7f; // Increased for better responsiveness
 +
 
 +
void accelerometer_init(void) {
 +
  // Initialize I2C at 400kHz for faster response
 +
  i2c__initialize(I2C__2, 400 * 1000, 96 * 1000 * 1000UL, &binary_sem_memory, &mutex_memory);
 +
  // Create mutex for thread-safe movement
 +
  movement_mutex = xSemaphoreCreateMutexStatic(&mutex_memory);
 +
 
 +
  // Configure ADXL345 accelerometer
 +
  i2c__write_single(I2C__2, ADXL345_WRITE, REG_DATA_FORMAT, 0x00); // ±2g range
 +
  i2c__write_single(I2C__2, ADXL345_WRITE, REG_POWER_CTL, 0x08);  // Measurement mode
 +
 
 +
  // Initialize smoothing variables
 +
  last_x = 0;
 +
  last_y = 0;
 +
}
 +
 
 +
 
 +
* Maze Logic:
 +
**1. `get_maze_layout`: Retrieves the maze pattern for the current level
 +
**2. `is_wall_at`: Checks if a specific position in the maze contains a wall, used to constrain player movement
 +
**3. `is_goal_at`: Determines if the player has reached the maze's goal position to proceed to the next level
 +
 
 +
 
 +
 +
 
 +
* **Game Logic**:
 +
**1. set_player_to_start : Resets the player position to the starting point of the current level
 +
**2. handle_collisions: Detects collisions with walls, traps, and goals, triggering state changes like `GAME_STATE_GAME_OVER` or GAME_STATE_LEVEL_UP
 +
**3. change_game_state: Manages game states such as `GAME_STATE_TITLE`, `GAME_STATE_PLAYING`, and `GAME_STATE_WIN`, and handles music transitions
 +
 
 +
switch (new_state) {
 +
  case GAME_STATE_TITLE:
 +
    stop_gameplay_music();
 +
    mp3_decoder__play_song_at_index(SOUND_TITLE);
 +
    enable_movement(false);
 +
    break;
 +
 
 +
  case GAME_STATE_PLAYING:
 +
    start_gameplay_music();
 +
    enable_movement(true);
 +
    break;
 +
 
 +
  case GAME_STATE_LEVEL_UP:
 +
    stop_gameplay_music();
 +
    mp3_decoder__play_song_at_index(SOUND_LEVELUP);
 +
    enable_movement(false);
 +
    break;
 +
 
 +
  case GAME_STATE_GAME_OVER:
 +
    stop_gameplay_music();
 +
    mp3_decoder__play_song_at_index(SOUND_LOSE);
 +
    enable_movement(false);
 +
    break;
  
=== Implementation ===
+
  case GAME_STATE_WIN:
This section includes implementation, but again, not the details, just the high level.  For example, you can list the steps it takes to communicate over a sensor, or the steps needed to write a page of memory onto SPI Flash.  You can include sub-sections for each of your component implementation.
+
    stop_gameplay_music();
 +
    mp3_decoder__play_song_at_index(SOUND_WIN);
 +
    enable_movement(false);
 +
    break;
 +
  }
 +
}
  
== Testing & Technical Challenges ==
+
* **MP3 Decoder**:
Describe the challenges of your project. What advise would you give yourself or someone else if your project can be started from scratch again?
+
**1. mp3_decoder__init: Initializes the MP3 decoder, sets the default volume, and selects the storage device
Make a smooth transition to testing section and described what it took to test your project.
+
**2. mp3_decoder__play_song_at_index: Plays a specific song based on its index in single-cycle mode
 +
**3. mp3_decoder__play_song_with_mode: Allows playback in loop or single-cycle mode, depending on the game state
 +
**4. mp3_decoder__stop_playback: Stops any active song playback
 +
**5. mp3_decoder__volume_set_level: Adjusts the volume level of the MP3 decoder
 +
 
 +
typedef enum {
 +
  mp3_decoder__next_song = 0x01,
 +
  mp3_decoder__prev_song = 0x02,
 +
  mp3_decoder__play_at_index = 0x03,
 +
  mp3_decoder__play_loop = 0x08, // Added loop command
 +
  mp3_decoder__set_volume = 0x06,
 +
  mp3_decoder__select_device = 0x09,
 +
  mp3_decoder__reset = 0x0C,
 +
  mp3_decoder__stop = 0x16, // Added stop command
 +
} mp3_decoder__commands_e;
 +
 
 +
// Added playback mode enum
 +
typedef enum { MP3_PLAY_ONCE, MP3_PLAY_LOOP } mp3_playback_mode_t;
 +
 
 +
== '''Testing & Technical Challenges''' ==
 +
The most challenging part of the Tilt Maze game was the integration and calibration of the accelerometer. Reading accurate tilt data and translating it into smooth, responsive movements for the game character required careful implementation. Additionally, ensuring the player's movement was constrained within the maze boundaries while avoiding unintended behavior added complexity.
 +
 
 +
=== Bug/Issue Name ===
 +
{| class="wikitable"
 +
|-
 +
! scope="col" | Bug Number
 +
! scope="col" | Description
 +
! scope="col" | Solution
 +
|-
 +
|
 +
* 1
 +
|
 +
* Accelerometer readings were noisy and caused jittery player movement.
 +
|
 +
* Implemented a weighted smoothing algorithm to filter out noise and ensure stable position updates.
 +
|-
 +
|
 +
* 2
 +
|
 +
* Player sometimes moved through walls due to rapid tilt changes.
 +
|
 +
* Added collision detection checks at every update to ensure movements are blocked if a wall is detected in the new position.
 +
|-
 +
|
 +
* 3
 +
|
 +
* Accelerometer's orientation detection was inconsistent at certain tilt angles.
 +
|
 +
* Calibrated the accelerometer and adjusted the thresholds for tilt detection to ensure reliable orientation changes.
 +
|-
 +
|
 +
* 4
 +
|
 +
* The game became unresponsive when the accelerometer task encountered delays.
 +
|
 +
* Used FreeRTOS semaphores to synchronize accelerometer updates with game logic and ensured proper task prioritization.
 +
|}
  
Include sub-sections that list out a problem and solution, such as:
+
== '''Conclusion''' ==
 +
Building the Tilt Maze game on a microcontroller proved to be a rewarding and challenging experience. Developing custom drivers for the accelerometer, LED matrix, and MP3 decoder required a deep understanding of embedded systems. Implementing FreeRTOS tasks to handle concurrent updates for accelerometer input, game logic, and display rendering added complexity but ensured smooth and responsive gameplay. One of the most challenging aspects was achieving accurate and stable accelerometer readings for tilt detection, which required calibration, noise filtering, and careful logic for player movement.
  
=== <Bug/issue name> ===
+
Throughout the project, we encountered various issues, including synchronization conflicts, noisy sensor data, and priority balancing in FreeRTOS. These challenges taught us the importance of debugging, modular design, and leveraging RTOS APIs effectively. As embedded engineers, we learned that creating a system from scratch involves meticulous attention to both hardware and software integration.
Discuss the issue and resolution.
 
  
== Conclusion ==
+
In the end, we successfully implemented a fun and interactive game that showcases the power of embedded systems. Future improvements could include adding a scoring system, dynamic maze generation, and more refined accelerometer controls to further enhance gameplay.
Conclude your project here. You can recap your testing and problems.  You should address the "so what" part here to indicate what you ultimately learnt from this project. How has this project increased your knowledge?
 
  
 
=== Project Video ===
 
=== Project Video ===
Upload a video of your project and post the link here.
+
[Watch the Tilt Maze game demo]
  
 
=== Project Source Code ===
 
=== Project Source Code ===
*  [https://sourceforge.net/projects/sjsu/files/CmpE_S2016/ Sourceforge Source Code Link]
+
https://gitlab.com/shreya.belide/tilt-maze-240.git
  
 
== References ==
 
== References ==
 
=== Acknowledgement ===
 
=== Acknowledgement ===
Any acknowledgement that you may wish to provide can be included here.
+
Special thanks to Mr. Preet Kang for his lessons and detailed-documentation website on microcontrollers.
  
 
=== References Used ===
 
=== References Used ===
List any references used in project.
+
*1. Mp3 user manual: https://usermanual.wiki/Pdf/Serial20MP320Player20v10120Manual.2117229468/view
 +
*2. LPC40xx_FreeRtos Github: https://gitlab.com/sjtwo-c-dev/sjtwo-c
  
 
=== Appendix ===
 
=== Appendix ===
You can list the references you used.
+
*1. LPC40xx_FreeRtos Github: https://gitlab.com/sjtwo-c-dev/sjtwo-c
 +
*2. FreeRTOS: https://www.freertos.org/a00116.html

Latest revision as of 08:31, 23 December 2024

Tilt maze Logo

Hardware and gameplay snapshots

Game Introduction
Maze1
Maze2
Maze3
Level Up
You Lose
You Win

Abstract

Tilt Maze is a motion-controlled puzzle game that challenges players to navigate a luminous ball through procedurally generated mazes using device tilting mechanics. Players must reach the exit within time constraints while maneuvering around obstacles and collecting power-ups that provide temporary advantages. The game combines physical device control with strategic gameplay elements, offering high replayability through its randomized level design and emphasizing skills in balance, spatial reasoning, and quick decision-making.

Objectives & Introduction

The Tilt Maze Game combines hardware and software to create an interactive puzzle experience. It uses an ADXL345 accelerometer for tilt-based movement control, navigating a character through a maze displayed on a 64x64 LED matrix. FreeRTOS manages concurrent tasks like accelerometer input, display updates, and game logic, ensuring smooth and responsive gameplay. Game states, collision detection, and immersive audio feedback via an MP3 decoder enhance the experience. Semaphores and mutexes ensure thread-safe resource management, while debug outputs provide insights during development. This project demonstrates advanced integration of peripherals and real-time systems in a cohesive gaming application.

TEAM MEMBERS AND RESPONSIBILITIES

  • Shreya Belide
    • Developed code for MP3 Decoder Driver to play background music and sound effects.
    • Designed and implemented Game Architecture and State Machine logic.
    • Developed collision detection logic to ensure smooth player interactions.
    • Bug fixes in MP3 integration with FreeRTOS tasks.
    • Integrated subsystems including audio, display, and game logic.
    • Code Cleanup and Optimization.
    • Game Packaging and Presentation Preparation.
  • Jyoshna Mallineni
    • Developed Accelerometer Driver for tilt-based control.
    • Designed Maze Layouts and Game Graphics.
    • Implemented logic for dynamic player movement and boundary restrictions.
    • Developed and tested display rendering functions for the LED Matrix.
    • Debugged issues with accelerometer sensitivity and movement logic.
    • Assisted in subsystem integration and game logic testing.
    • Code Cleanup and Documentation.
  • Pavan Charith Devarapalli
    • Developed LED Matrix Driver for rendering the game visuals.
    • Implemented multi-level gameplay logic and player progression.
    • Designed logic for traps and goal conditions in the maze.
    • Integrated MP3 Decoder with SPI communication for audio playback.
    • Debugged synchronization issues between subsystems.
    • Tested and verified power supply stability for all components.
    • Assisted with subsystem integration and final game testing.
    • Managed Git Repository and Final Report Preparation.

Schedule

Week# Start Date End Date Task Status
1
  • 10/20/2024
  • 10/27/2024
  • 10/20/2024
  • 10/27/2024
  • Completed
  • Completed
2
  • 10/27/2024
  • 11/02/2024
  • Order necessary parts - LED Matrix , Speaker , Accelerometer
  • Completed
3
  • 11/02/2024
  • 11/08/2024
  • Read and familiarize with LED Matrix Datasheet
  • Completed
4
  • 11/02/2024
  • 11/08/2024
  • Develop graphics driver for LED matrix and implement initial game objects
  • Completed
5
  • 11/09/2024
  • 11/09/2024
  • 11/09/2024
  • 11/09/2024
  • 11/10/2024
  • 11/15/2024
  • 11/15/2024
  • 11/15/2024
  • Finalize wiki schedule
  • Order circuit boards components and complete the design for printing
  • circuit board and component assembly
  • Circuit board testing
  • Additional accessories if required and finalization of hardware
  • Completed
  • Completed
  • Completed
  • Completed
  • Completed
6
  • 11/16/2024
  • 11/22/2024
  • Integration of circuit boards and microcontroller
  • Game logic development
  • Testing and debugging the game logic
  • Completed
  • Completed
  • Completed
7
  • 11/23/2024
  • 11/29/2024
  • Integrate game logic code with LED matrix
  • Integrate game sounds with game logic
  • Completed
  • Completed
8
  • 11/30/2024
  • 12/06/2024
  • Integrate subsystem
  • Finalizing the video game
  • Update the wiki page.
  • Completed
  • Completed
  • Completed
9
  • 12/07/2024
  • 12/13/2024
  • Address bugs during testing of integrated system
  • Test pause/play functionality
  • Completed
  • Completed
  • Completed
10
  • 12/14/2024
  • 12/14/2024
  • 12/14/2024
  • 12/14/2024
  • 12/18/2024
  • 12/18/2024
  • 12/18/2024
  • 12/18/2024
  • Final Demo
  • Update Gitlab repo with final code.
  • Update test video.
  • Update the wiki page.
  • Completed
  • Completed
  • Completed
  • Completed


BILL OF MATERIALS

Part # Cost Source
SJ2 Board 1 $50.00 Preet
Sparkfun RGB (64x64) LED Matrix Display 1 $32.72 Amazon
Accelerometer 1 $10.99 Amazon
12v DC Power Jack Adapter Connector 1 $8.90 Amazon
MP3 Decoder 1 6.90 Amazon
Packaging 1 $12 Target
Jumper Wires 1 $6.99 Amazon
Total Cost $128.5

Design & Implementation

The design section can go over your hardware and software design. Organize this section using sub-sections that go over your design and implementation.

Hardware Design

Discuss your hardware design here. Show detailed schematics, and the interface here.

RGB LED MATRIX

It is composed of two upper and lower sectional LED pannels. Each pannel has R, G, B led channels and A, B, C, and D row control registers. Addtionally, column shift is controlled by Latch bit, clock is controled by CLK, and OE turns LED off when switching rows.

Connectivity Table LED Matrix to Sj2 Board:

RGB LED Matrix Pin Description SJ2 Board
R1 GPIO P1_14
G1 GPIO P4_29
B1 GPIO P0_7
R2 GPIO P0_9
G2 GPIO P0_25
B2 GPIO P1_30
A GPIO P1_23
B GPIO P1_29
C GPIO P2_4
D GPIO P2_6
CLK GPIO P2_8
LAT GPIO P0_17
OE GPIO P0_16
VCC 5VIN External Power Supply
GND GND On Board
LED Matrix Front
LED Matrix Rear

Accelerometer

The ADXL345 accelerometer plays a central role in detecting tilt and translating it into game interactions. Here's a detailed breakdown of its purpose and usage: Tilt Detection: The ADXL345 measures acceleration along the X, Y, and Z axes, allowing you to detect the orientation and movement of the system. Game Control: The tilt input can serve as a controller for your tilt maze game. By tilting the device, you can move an object (like a ball) on the LED matrix to navigate the maze.

Connectivity Table LED Matrix to Sj2 Board:

ADXL345 Pin Pin Description SJ2 Board
VCC Power supply for the accelerometer P1_14
GND Ground Connections P4_29
SCL cLOCK Signal for i2C P0_7
SDA Data Signal fro I2C P0_9
CS To enable to i2C mode P
ADXL345 Accelerometer

MP3 Decoder

The MP3 decoder model Hiletgo GD3300 adds audio functionality, such as background music or sound effects for tilt maze game. Here's a concise explanation of its role and how to integrate it:

Background Music: Play ambient or thematic music to enhance the gaming experience. Sound Effects: Trigger sounds (e.g., collision, level-up, or maze completion) based on game events.

Connectivity Table MP3 Decoder to Sj2 Board:

Decoder pin Pin Description SJ2 Board
VCC Power supply for the accelerometer 5V
GND Ground Connections GND
TX UART transmit pin (sends status and responses to the microcontroller). P0_0
RX UART receive pin (receives commands from the microcontroller). P0_1
MP3 Decoder Hiletgo GD3300

</center>

Hardware Interface

  • **LED Matrix Display**: 13 GPIO channels on SJ2 microcontroller
  • **Accelerometer (ADXL345)**: I2C communication on SJ2 microcontroller (SCL, SDA)
  • **MP3 Decoder**: SPI communication using MOSI, CS, SCK on SJ2 microcontroller
  • **Speaker**: AUX cord for audio output
  • **Power Supply**: 5V/4A adapter for powering the LED matrix and SJ2 microcontroller

Software Design

  • **LED Matrix:
    • 1. Initialized LED matrix connected pins to board IOs.
    • 2. Designed matrix driver for screen display by rendering maze patterns and player movements.
  • **Accelerometer**:
    • 1. Initialized I2C communication for ADXL345 accelerometer.
    • 2. Configured accelerometer in measurement mode and set sensitivity to ±2g.
    • 3. Processed tilt data to calculate real-time player movement commands.
  • **Mp3 Player**:
    • 1. Initialized using UART2.
    • 2. Set the device to the selected SD card and configured volume level.
    • 3. Played background music and sound effects based on game state.

Implementation

  • **LED Matrix Driver Functions**:
  • LED Matrix:
    • 1. Initialized LED matrix connected pins to board IOs.
    • 2. Designed matrix driver for screen display by reading an matrix.
    • 3. Designed pause menu, main menu and gameplay environment for different states of game.
    • 4. Used GPIO pins as buttons as controls in main menu


  • **Accelerometer Driver**:
    • 1. accelerometer_init: Configures the ADXL345 accelerometer with ±2g sensitivity and sets up I2C communication with semaphores for thread safety
    • 2. accelerometer_task: Periodically reads acceleration data, applies smoothing, and updates player position based on tilt movements

// Add acceleration smoothing static int16_t last_x = 0; static int16_t last_y = 0; const float smoothing_factor = 0.7f; // Increased for better responsiveness

void accelerometer_init(void) {

 // Initialize I2C at 400kHz for faster response
 i2c__initialize(I2C__2, 400 * 1000, 96 * 1000 * 1000UL, &binary_sem_memory, &mutex_memory);
 // Create mutex for thread-safe movement
 movement_mutex = xSemaphoreCreateMutexStatic(&mutex_memory);
 // Configure ADXL345 accelerometer
 i2c__write_single(I2C__2, ADXL345_WRITE, REG_DATA_FORMAT, 0x00); // ±2g range
 i2c__write_single(I2C__2, ADXL345_WRITE, REG_POWER_CTL, 0x08);   // Measurement mode
 // Initialize smoothing variables
 last_x = 0;
 last_y = 0;

}


  • Maze Logic:
    • 1. `get_maze_layout`: Retrieves the maze pattern for the current level
    • 2. `is_wall_at`: Checks if a specific position in the maze contains a wall, used to constrain player movement
    • 3. `is_goal_at`: Determines if the player has reached the maze's goal position to proceed to the next level



  • **Game Logic**:
    • 1. set_player_to_start : Resets the player position to the starting point of the current level
    • 2. handle_collisions: Detects collisions with walls, traps, and goals, triggering state changes like `GAME_STATE_GAME_OVER` or GAME_STATE_LEVEL_UP
    • 3. change_game_state: Manages game states such as `GAME_STATE_TITLE`, `GAME_STATE_PLAYING`, and `GAME_STATE_WIN`, and handles music transitions

switch (new_state) {

 case GAME_STATE_TITLE:
   stop_gameplay_music();
   mp3_decoder__play_song_at_index(SOUND_TITLE);
   enable_movement(false);
   break;
 case GAME_STATE_PLAYING:
   start_gameplay_music();
   enable_movement(true);
   break;
 case GAME_STATE_LEVEL_UP:
   stop_gameplay_music();
   mp3_decoder__play_song_at_index(SOUND_LEVELUP);
   enable_movement(false);
   break;
 case GAME_STATE_GAME_OVER:
   stop_gameplay_music();
   mp3_decoder__play_song_at_index(SOUND_LOSE);
   enable_movement(false);
   break;
 case GAME_STATE_WIN:
   stop_gameplay_music();
   mp3_decoder__play_song_at_index(SOUND_WIN);
   enable_movement(false);
   break;
 }

}

  • **MP3 Decoder**:
    • 1. mp3_decoder__init: Initializes the MP3 decoder, sets the default volume, and selects the storage device
    • 2. mp3_decoder__play_song_at_index: Plays a specific song based on its index in single-cycle mode
    • 3. mp3_decoder__play_song_with_mode: Allows playback in loop or single-cycle mode, depending on the game state
    • 4. mp3_decoder__stop_playback: Stops any active song playback
    • 5. mp3_decoder__volume_set_level: Adjusts the volume level of the MP3 decoder

typedef enum {

 mp3_decoder__next_song = 0x01,
 mp3_decoder__prev_song = 0x02,
 mp3_decoder__play_at_index = 0x03,
 mp3_decoder__play_loop = 0x08, // Added loop command
 mp3_decoder__set_volume = 0x06,
 mp3_decoder__select_device = 0x09,
 mp3_decoder__reset = 0x0C,
 mp3_decoder__stop = 0x16, // Added stop command

} mp3_decoder__commands_e;

// Added playback mode enum typedef enum { MP3_PLAY_ONCE, MP3_PLAY_LOOP } mp3_playback_mode_t;

Testing & Technical Challenges

The most challenging part of the Tilt Maze game was the integration and calibration of the accelerometer. Reading accurate tilt data and translating it into smooth, responsive movements for the game character required careful implementation. Additionally, ensuring the player's movement was constrained within the maze boundaries while avoiding unintended behavior added complexity.

Bug/Issue Name

Bug Number Description Solution
  • 1
  • Accelerometer readings were noisy and caused jittery player movement.
  • Implemented a weighted smoothing algorithm to filter out noise and ensure stable position updates.
  • 2
  • Player sometimes moved through walls due to rapid tilt changes.
  • Added collision detection checks at every update to ensure movements are blocked if a wall is detected in the new position.
  • 3
  • Accelerometer's orientation detection was inconsistent at certain tilt angles.
  • Calibrated the accelerometer and adjusted the thresholds for tilt detection to ensure reliable orientation changes.
  • 4
  • The game became unresponsive when the accelerometer task encountered delays.
  • Used FreeRTOS semaphores to synchronize accelerometer updates with game logic and ensured proper task prioritization.

Conclusion

Building the Tilt Maze game on a microcontroller proved to be a rewarding and challenging experience. Developing custom drivers for the accelerometer, LED matrix, and MP3 decoder required a deep understanding of embedded systems. Implementing FreeRTOS tasks to handle concurrent updates for accelerometer input, game logic, and display rendering added complexity but ensured smooth and responsive gameplay. One of the most challenging aspects was achieving accurate and stable accelerometer readings for tilt detection, which required calibration, noise filtering, and careful logic for player movement.

Throughout the project, we encountered various issues, including synchronization conflicts, noisy sensor data, and priority balancing in FreeRTOS. These challenges taught us the importance of debugging, modular design, and leveraging RTOS APIs effectively. As embedded engineers, we learned that creating a system from scratch involves meticulous attention to both hardware and software integration.

In the end, we successfully implemented a fun and interactive game that showcases the power of embedded systems. Future improvements could include adding a scoring system, dynamic maze generation, and more refined accelerometer controls to further enhance gameplay.

Project Video

[Watch the Tilt Maze game demo]

Project Source Code

https://gitlab.com/shreya.belide/tilt-maze-240.git

References

Acknowledgement

Special thanks to Mr. Preet Kang for his lessons and detailed-documentation website on microcontrollers.

References Used

Appendix