View on GitHub

Simon Says: Tilt the Board

by Matthew Quinn and Sarah Fleischman

Introduction:

For our project, we implemented a version of the classic and addictive game, Simon. But, as the Freedom Development Board only has one button that resets the system, we decided to use the built-in accelerometer to take in user input instead. We use the x and y axis of the accelerometer to check in what direction the board is tilted. Forward is green, right is red, back is blue, and left is yellow. Both the pattern you must repeat and your inputs are displayed on the board's LED.

Gameplay:

The game starts with the board flashing one of the four colors. The user must then tilt the board in the direction of the color displayed. If successful, the board flashes a sequence with one more color in it, with the original pattern remaining the same. For example, if the last pattern was Red, Blue, Red, Green, then the next pattern could be Red, Blue, Red, Green, Yellow. If you successfully replicate the given pattern, the board blinks white twice before displaying the next pattern. If you make a mistake, the LED flashes pink and your game ends. To replay from the beginning, simply press the reset button on the board.

System Overview:

The system cycles through two states: Display and Record. In the display phase, a new color is generated pseudo-randomly. To do so, the accelerometer is polled and the x, y, and z axis values are added together. This value (mod 4) + 1 is then used to represent the new color. (1 = Green, 2 = Red, 3 = Blue, 4 = Yellow) The current sequence of LED colors are stored in a linked list, and the new value is added to the end of this list. Next, all colors in the sequence are displayed in order. Then the system transitions to the Recording phase. The recording phase repeatedly polls the accelerometer to see if the user has tilted the board. When the board is tilted in one of the four directions, (the x or y value exceeds the threshold) The board displays the inputted color on the LED. If the input is correct, the board polls again for the next color in the sequence if there is one. If the input is incorrect, the board flashes pink and ends the game. If there are no more colors in the sequence and the input was correct, the board flashes white twice, then moves back into the display phase. This cycle could go on in theory until the board runs out of space to store the sequence. Fortunately, people do not have the capacity to remember a long enough sequence for that to be an issue. To play the game again, the user must press the reset button on the board.

Project Video Demo:

You can view our video here.

Detailed Technical Description:

For the project we made two folders, Source, and Sensors. Source contains main.c, utils.c, and utils.h. Sensors contains five mma845x files.

Source:

main.c: This contains the main function that cycles through the display and record phases described above. It also contains all of the helper functions, two structs, and two important global variables.

record() - Progresses through the list of LEDs, flashes white when the user is successful, and pink when they make a mistake

select() - Reads the input and returns the number corresponding to the color the user selected by tilting

display() - Adds a random color to the end of the order linked list then displays the order on the LED

display_all() - Displays the LEDs in the list

enqueue_random() - Creates a pseudo-random new color and adds it to the end of the order linked list

struct pos - Created to hold and easily access polled x and y axis data. Contains two ints to hold the x-axis and y-axis values

struct color - Used for the order linked list. Contains an int that represents the color and a pointer to the next color object

int x_th = 825 - x threshold for tilting before the board registers an input (found through trial and error)

int y_th = 1250 - y threshold for tilting before the board registers an input (found through trial and error)

utils.c, utils.h: These files contain all of the LED functions as well as the delay functions we use in our code. We use the colors red, blue, green, yellow, pink, and white. The file contains a function to initialize the LEDs, turn on each of the colors we use, turn off the LED completely, as well as four delay functions of varying length.

Sensors: (all files in this folder were taken from the example from NXP linked below)

mma845x_drv.c, mma845x_drv.h: These files contain functions that set up I2C communication on the board, as well as functions to write, read, and finally stop I2C communication.

mma845x_poll.c, mma845x_poll.h: These files contain two functions. The first initializes communication with the accelerometer through I2C by using some of the functions from mma845x_drv. The second polls the accelerometer for data and processes the data into a readable format. We modified the polling function by removing the print statements at the end of it for our final project. However, having the board print out the accelerometer data was extremely helpful when testing.

Additional Resources Used:

To communicate with the built-in accelerometer, we modified the example code from NXP. The instructions we followed to get the code are here.

Work Distribution:

We worked together formulating a plan for the game and laying out the problems we would have to solve in order to successfully complete it. Matt played a larger role in the coding of the game while Sarah took on a larger role in doing the writeup and commenting the code. We were able to code together, however, this split of responsibilities fit the time each of us had in the past two weeks to work on the project.