ThumbIt
Lab4
by Paul Wilson and James Knowles, III.
Overview
ThumbIt is a game that allows users to direct a ball through a maze utilizing a thumb joystick. The joystick is interfaced to an mbed device via analog input. To generate ball movements, the application reads in horizontal and vertical, x and y respectively, analog data generated from the joystick. Once the input values pass a threshold, the ball is moved to its next respectful position. A shiftbrite is used as a visualizer to convey the ball's movement status. Read more here about shiftbrites. This game was created to satisfy the mini design project component of the fourth lab for our Embedded Systems Design course.
Goals
The main objective for the game is to move the ball from the top left corner of the screen to the bottom right corner of the screen while avoiding walls. There are a total of 3 rounds. Pass all 3 rounds and you win!
Running into walls limits your movement. You'll know when you hit a wall because the shiftbrite will turn red. Otherwise it should be green. Try your best to stay on course.
Components
There are 2 major components to this project: generating the maze(s) for the user to navigate the ball through and interfacing with the joystick with precision so the ball responds appropriately.
Generating the Maze
Each round has its own maze. Each of the mazes are built using a grid. Here's an example of the grid for round 1.
void createGrid() { uLCD.background_color(WHITE); uLCD.cls(); // Max a 7x7 Grid // 18 pix width int count = 0; for(int i = 0; i < 127; i=i+18) { xLocs[count] = i; yLocs[count] = i; uLCD.line(i,0,i, 127, BLACK); uLCD.line(0, i,127, i, BLACK); count++; } }
Square borders in each position in grid
void drawBorder() { for(int e = 0; e < 8; e++) { for(int f = 0; f < 8; f++) { if(grid_field[e][f] == 1) { //Draw a filled square in the block using the values in the locations uLCD.filled_rectangle(yLocs[e],yLocs[f], yLocs[e+1], yLocs[f+1], BLACK); } } } }
Interfacing with Joystick
The thumb joystick passes analog data to the mbed device. Directional movements generate from two potentiometers - one for each axis. Thus the analog X and Y inputs control the movement of the ball. When either X or Y input is greater than the upper threshold, or less than the lower threshold, the ball moves its positioning in the corresponding direction.
Other Important Snippets of Code
Redrawing the ball during movement
// Redraw the ball if(movement) { x = (yLocs[prevPosX] + yLocs[prevPosX + 1]) / 2; y = (yLocs[prevPosY] + yLocs[prevPosY + 1]) / 2; uLCD.circle(x, y, radius, WHITE); x = (yLocs[posX] + yLocs[posX + 1]) / 2; y = (yLocs[posY] + yLocs[posY + 1]) / 2; uLCD.circle(x, y, radius, BLUE); prevPosX = posX; prevPosY = posY; movement = false; }
LCD Shiftbrite Setup
void lcdSetup() { uLCD.baudrate(3000000); //jack up baud rate to max for fast display uLCD.background_color(WHITE); uLCD.textbackground_color(WHITE); uLCD.color(BLUE); uLCD.text_width(2); uLCD.text_height(2); // Setup shiftbrite spi.format(16,0); spi.frequency(500000); enable=0; latch=0; uLCD.cls(); uLCD.printf("Maze Game\n"); uLCD.printf("Try to get from the top, left to the bottom, right.\n"); wait(3); uLCD.printf("\nLet's Begin.."); wait(2); uLCD.cls(); }
If ball hits wall, set shiftbrite to red
//Use SPI hardware to write color values to LED driver chip void RGB_LED(int red, int green, int blue) { unsigned int low_color=0; unsigned int high_color=0; high_color=(blue<<4)|((red&0x3C0)>>6); low_color=(((red&0x3F)<<10)|(green)); spi.write(high_color); spi.write(low_color); latch=1; latch=0; }
Repository
Import program4180_Lab4
ThumbIt Repository - use thumb joystick to navigate ball through maze.
Result
Please log in to post comments.