Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE mbed
Diff: main.cpp
- Revision:
- 1:2250e33823e2
- Parent:
- 0:7b4bbd744f6d
--- a/main.cpp Tue Oct 20 00:42:42 2015 +0000 +++ b/main.cpp Tue Oct 20 18:34:56 2015 +0000 @@ -1,4 +1,9 @@ -//Most of the Credit goes to jimblom and Judah Okeleye +//Daniel Begasse +//Kieth Nga +//ECE 4180 Lab 4 +//This game uses reading from the accelerometer on an IMU sensor to move a ball through a maze +//The goal of the game is to move to yellow ball through the maze, avoiding walls and obstacles along the way + #include "LSM9DS0.h" #include "mbed.h" #include "uLCD_4DGL.h" @@ -12,13 +17,7 @@ LSM9DS0 dof(p9, p10,LSM9DS0_G, LSM9DS0_XM); uLCD_4DGL uLCD(p28, p27, p30); //create a global lcd project - - -//Uncomment If you want to use interrupts -/*DigitalIn pinDRDYG(p14); -DigitalIn pinINTG(p13); -DigitalIn pinINT1_XM(p14); -DigitalIn pinINT2_XM(p15);*/ +Serial pc(USBTX, USBRX); // tx, rx #define PRINT_CALCULATED #define PRINT_SPEED 500 // 500 ms between prints @@ -26,175 +25,76 @@ //Init Serial port and LSM9DS0 chip void setup() { - // Use the begin() function to initialize the LSM9DS0 library. - // You can either call it with no parameters (the easy way): + pc.baud(9600);//Had to update the baud rate to match tera term uint16_t status = dof.begin(); - - //Make sure communication is working - pc.printf("LSM9DS0 WHO_AM_I's returned: 0x"); - pc.printf("%x\n",status); - pc.printf("Should be 0x49D4\n"); - pc.printf("\n"); -} - -//This function prints the temperature -void printTemp() -{ - dof.readTemp(); - //pc.printf("Temperature: "); - //pc.printf("%2f",dof.calcTemp(dof.temperature)); - //pc.printf("\n"); - - //rs232.printf("Temperature: "); - -} - -//This funtion get the accelerometer data and returns it -//I call this function in the main function to get the data and then send it to the lcd -float getAccel() -{ - - dof.readAccel(); - pc.printf("%2f",dof.calcAccel(dof.ax) - dof.abias[0]); - return dof.calcAccel(dof.ax) - dof.abias[0]; -} - -void printGyro() -{ - // To read from the gyroscope, you must first call the - // readGyro() funct ion. When this exits, it'll update the - // gx, gy, and gz variables with the most current data. - - //Uncomment code if you plan on using interrupts - /*while(pinDRDYG == 0) - { - ; - }*/ - - - dof.readGyro(); - - // Now we can use the gx, gy, and gz variables as we please. - // Either print them as raw ADC values, or calculated in DPS. - pc.printf("Gyroscope: "); - //uLCD.printf("Gyroscope: "); -#ifdef PRINT_CALCULATED - // If you want to print calculated values, you can use the - // calcGyro helper function to convert a raw ADC value to - // DPS. Give the function the value that you want to convert. - //pc.printf("The bias: %2f, %2f, %2f \n", dof.gbias[0], dof.gbias[1], dof.gbias[2]); - pc.printf("%2f",dof.calcGyro(dof.gx) - dof.gbias[0]); - pc.printf(", "); - pc.printf("%2f",dof.calcGyro(dof.gy) - dof.gbias[1]); - pc.printf(", "); - pc.printf("%2f\n",dof.calcGyro(dof.gz) - dof.gbias[2]); -#elif defined PRINT_RAW - pc.printf("%d", dof.gx); - pc.printf(", "); - pc.printf("%d",dof.gy); - pc.printf(", "); - pc.printf("%d\n",dof.gz); -#endif -} - -void printAccel() -{ - //Uncomment code if you plan on using interrupts - /*while(pinINT1_XM == 0) - { - ; - }*/ - - - dof.readAccel(); - - - // Now we can use the ax, ay, and az variables as we please. - // Either print them as raw ADC values, or calculated in g's. - pc.printf("Accellerometer Data: "); -//#ifdef PRINT_CALCULATED - // If you want to print calculated values, you can use the - // calcAccel helper function to convert a raw ADC value to - // g's. Give the function the value that you want to convert. - //pc.printf("The bias: %2f, %2f, %2f \n", dof.abias[0], dof.abias[1], dof.abias[2]); - pc.printf("%2f\n",dof.calcAccel(dof.ax) - dof.abias[0]); - //pc.printf(", "); - //pc.printf("%2f",dof.calcAccel(dof.ay) - dof.abias[1]); - //pc.printf(", "); - //pc.printf("%2f\n",dof.calcAccel(dof.az) - dof.abias[2]); -//#elif defined PRINT_RAW - //pc.printf("%d",dof.ax); - // pc.printf(", "); - //pc.printf("%d",dof.ay); - //pc.printf(", "); - //pc.printf("%d\n",dof.az); -//#endif - -} - -void printMag() -{ - //Uncomment code if you plan on using interrupts - /*while(pinINT2_XM == 0) - { - ; - }*/ - - // To read from the magnetometer, you must first call the - // readMag() function. When this exits, it'll update the - // mx, my, and mz variables with the most current data. - dof.readMag(); - - // Now we can use the mx, my, and mz variables as we please. - // Either print them as raw ADC values, or calculated in Gauss. - pc.printf("M: "); -#ifdef PRINT_CALCULATED - // If you want to print calculated values, you can use the - // calcMag helper function to convert a raw ADC value to - // Gauss. Give the function the value that you want to convert. - pc.printf("%2f",dof.calcMag(dof.mx)); - pc.printf(", "); - pc.printf("%2f",dof.calcMag(dof.my)); - pc.printf(", "); - pc.printf("%2f\n",dof.calcMag(dof.mz)); -#elif defined PRINT_RAW - pc.printf("%d", dof.mx); - pc.printf(", "); - pc.printf("%d", dof.my); - pc.printf(", "); - pc.printf("%d\n", dof.mz); -#endif -} - -void loop() -{ - //pc.printf("\n\nDATA READINGS\n"); - //printGyro(); // Print "G: gx, gy, gz" - //printAccel(); // Print "A: ax, ay, az" - //printMag(); // Print "M: mx, my, mz" - //printTemp(); - //wait_ms(PRINT_SPEED); - } + +void drawMaze(){ + + uLCD.filled_rectangle(20, 0, 23, 40 , 0x00FF00); //Wall 1 + uLCD.filled_rectangle(0, 105, 40, 108 , 0x00FF00); //Wall 2 + uLCD.filled_rectangle(25, 70, 127, 73 , 0x00FF00); //Wall 3 + uLCD.filled_rectangle(45, 40, 48, 70 , 0x00FF00); //Wall 4 + uLCD.filled_rectangle(45, 40, 75, 43 , 0x00FF00); //Wall 5 + uLCD.filled_rectangle(75, 73, 78, 108 , 0x00FF00); //Wall 6 + uLCD.filled_rectangle(90, 15, 127, 18 , 0x00FF00); //Wall 7 + uLCD.filled_circle(100, 100, 5, BLUE);//End destination + uLCD.filled_circle(100, 50, 7, DGREY); //Hole #1 + uLCD.filled_circle(60, 90, 7, DGREY); //Hole #2 + +} + +void displayRules(){ + + uLCD.printf(" Labyrinth Danger!\n\n\n"); + wait(3); + uLCD.locate(5,3); + uLCD.printf("THE RULES\n"); + wait(1.5); + uLCD.printf("1.)Avoid the walls\n"); + wait(1.5); + uLCD.printf("2.)Avoid the holes\n"); + wait(1.5); + uLCD.printf("3.)Get the yellow ball in the blue hole to win"); + wait(6); + uLCD.cls(); + + } + +void youLose(){ + + uLCD.cls(); + uLCD.locate(1,4); + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.printf("YOU LOSE\n"); + } + +void youWin(){ + + uLCD.cls(); + uLCD.locate(1,4); + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.printf("YOU WIN!\n"); + } + int main() { + displayRules();//Show the rules before the start of the game setup(); //Setup sensor and Serial - //int content; - - - + drawMaze(); //Draw the maze - - //************************************************ - //Below is the code for the uLCD bubble level int circlex = 0; - int circley = 60; - int circler = 15; - int last_circlex = 0; - float accel; + int circley = 0; + int circler = 3; + int last_circlex = 64; + int last_circley = 64; + float accelx; + float accely; @@ -202,19 +102,99 @@ { dof.readAccel(); - accel = dof.calcAccel(dof.ax) - dof.abias[0]; + accelx = dof.calcAccel(dof.ax) - dof.abias[0]; + accely = dof.calcAccel(dof.ay) - dof.abias[1]; //pc.printf("%2f\n", accel); wait(0.05); - + + //Get rid of the old circle + uLCD.filled_circle(last_circlex, last_circley, circler, BLACK); + + circlex = 128 - ((1 + accelx) * 64); + circley = (1 + accely) * 64; + + + //This checks if the game has been won + if( (circlex >= 96 && circlex <= 104) && (circley >= 96 && circley <= 104) ){ + + youWin(); + break; + } + - uLCD.filled_circle(last_circlex, circley, circler, BLACK); - circlex = (1 + accel) * 64; + //This checks if the ball has hit wall number 1 + if( (circlex >= 18 && circlex <= 25) && (circley >= 0 && circley <=42) ){ + + youLose(); + break; + } + //This checks if the ball has hit wall number 2 + if( (circlex >= 0 && circlex <= 42) && (circley >= 103 && circley <= 110) ){ + + youLose(); + break; + } + + //This checks if the ball has hit wall number 3 + if( (circlex >= 23 && circlex <= 129) && (circley >= 68 && circley <=75) ){ + + youLose(); + break; + } + + //This checks if the ball has hit wall number 4 + if( (circlex >= 43 && circlex <= 50) && (circley >= 38 && circley <= 72) ){ + + youLose(); + break; + } + + //This checks if the ball has hit wall number 5 + if( (circlex >= 43 && circlex <= 77) && (circley >= 38 && circley <= 45) ){ + + youLose(); + break; + } + + //This checks if the ball has hit wall number 6 + if( (circlex >= 73 && circlex <= 80) && (circley >= 72 && circley <= 110) ){ + + youLose(); + break; + } + + //This checks if the ball has hit wall number 7 + if( (circlex >= 88 && circlex <= 129) && (circley >= 13 && circley <= 20) ){ + + youLose(); + break; + } + + //This checks if the ball fell into hole #1 + if( (circlex >= 95 && circlex <= 105) && (circley >= 45 && circley <= 55) ){ + + youLose(); + break; + } + + //This checks if the ball fell into hole #2 + if( (circlex >= 55 && circlex <= 65) && (circley >= 85 && circley <= 95) ){ + + youLose(); + break; + } + + //If the player has not won or hit a wall, update the location of the ball + else{ + last_circlex = circlex; + last_circley = circley; uLCD.filled_circle(circlex, circley, circler, 0xFFFF00); + } } - //************************************************* + }