Sports tracker
Dependencies: C12832_lcd CMPS03 FatFileSystem GPS MMA7660 mbed
Fork of Data_Read_Write by
main.cpp@2:775586529d5e, 2014-05-22 (annotated)
- Committer:
- GBSingh
- Date:
- Thu May 22 22:58:15 2014 +0000
- Revision:
- 2:775586529d5e
- Parent:
- 1:6a1228b44183
Sports tracker
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GBSingh | 2:775586529d5e | 1 | #include "mbed.h"//mbed library |
GBSingh | 1:6a1228b44183 | 2 | #include "MSCFileSystem.h"//usb Library |
GBSingh | 2:775586529d5e | 3 | #include "MMA7660.h"//accelerometer library |
GBSingh | 2:775586529d5e | 4 | #include "CMPS03.h"//compass library |
GBSingh | 1:6a1228b44183 | 5 | #include "GPS.h"//GPS library |
GBSingh | 1:6a1228b44183 | 6 | #include "C12832_lcd.h" // LCD screen library |
GBSingh | 1:6a1228b44183 | 7 | MSCFileSystem fs("usb"); // Mount flash drive under the name "usb" |
GBSingh | 1:6a1228b44183 | 8 | C12832_LCD lcd;// Local name for the LCD |
GBSingh | 2:775586529d5e | 9 | GPS gps(p13,p14);//pin for RX and TX of the gps |
GBSingh | 1:6a1228b44183 | 10 | Serial pc(USBTX, USBRX); |
GBSingh | 2:775586529d5e | 11 | MMA7660 MMA(p28, p27);// local name for the Accelerometer |
GBSingh | 2:775586529d5e | 12 | DigitalIn fire(p12);//pin for down movement joystick |
GBSingh | 2:775586529d5e | 13 | DigitalIn right(p16);//pin for right movement for joystick |
GBSingh | 2:775586529d5e | 14 | DigitalIn left(p13);//pin for left movement for joystick |
GBSingh | 2:775586529d5e | 15 | CMPS03 compass(p9, p10, CMPS03_DEFAULT_I2C_ADDRESS);//pin for compass |
GBSingh | 2:775586529d5e | 16 | DigitalOut connectionLed(LED1); // debug LED |
GBSingh | 1:6a1228b44183 | 17 | int lock = 0; |
GBSingh | 1:6a1228b44183 | 18 | int lock2 = 0; |
GBSingh | 1:6a1228b44183 | 19 | int lock3 = 0; |
GBSingh | 1:6a1228b44183 | 20 | int lock4 = 0; |
GBSingh | 2:775586529d5e | 21 | int choice = 0; |
GBSingh | 2:775586529d5e | 22 | int counter = 0; |
GBSingh | 2:775586529d5e | 23 | Timer writeTime;//timer |
GBSingh | 2:775586529d5e | 24 | void run(); |
GBSingh | 2:775586529d5e | 25 | |
GBSingh | 2:775586529d5e | 26 | void start()//starts and stops the game |
GBSingh | 1:6a1228b44183 | 27 | { |
GBSingh | 2:775586529d5e | 28 | |
GBSingh | 2:775586529d5e | 29 | int counter = 0; |
GBSingh | 2:775586529d5e | 30 | lcd.cls(); |
GBSingh | 2:775586529d5e | 31 | lcd.locate(0,0); |
GBSingh | 2:775586529d5e | 32 | lcd.printf("Press down to start game"); |
GBSingh | 2:775586529d5e | 33 | lcd.locate(0,20); |
GBSingh | 2:775586529d5e | 34 | lcd.printf("Hold down to stop game"); |
GBSingh | 2:775586529d5e | 35 | wait(1.5); |
GBSingh | 2:775586529d5e | 36 | while (counter == 0) { |
GBSingh | 2:775586529d5e | 37 | if(fire) { |
GBSingh | 2:775586529d5e | 38 | run(); |
GBSingh | 2:775586529d5e | 39 | counter = 1; |
GBSingh | 2:775586529d5e | 40 | } |
GBSingh | 2:775586529d5e | 41 | } |
GBSingh | 2:775586529d5e | 42 | |
GBSingh | 2:775586529d5e | 43 | } |
GBSingh | 2:775586529d5e | 44 | void run()// main funtion to get data and write it to files |
GBSingh | 2:775586529d5e | 45 | { |
GBSingh | 2:775586529d5e | 46 | lcd.cls();//clear LCD for next reading round |
GBSingh | 1:6a1228b44183 | 47 | while(1) { |
GBSingh | 2:775586529d5e | 48 | writeTime.reset();//resets timer |
GBSingh | 2:775586529d5e | 49 | writeTime.start();//starts timer |
GBSingh | 2:775586529d5e | 50 | FILE *longd = fopen( "/usb/long.txt", "a");//opens a file on the usb in append mode |
GBSingh | 1:6a1228b44183 | 51 | FILE *latd = fopen( "/usb/lat.txt","a"); |
GBSingh | 2:775586529d5e | 52 | FILE *bear = fopen( "/usb/bearing.txt","a"); |
GBSingh | 2:775586529d5e | 53 | FILE *xData = fopen( "/usb/xData.txt","a"); |
GBSingh | 2:775586529d5e | 54 | FILE *yData = fopen( "/usb/yData.txt","a"); |
GBSingh | 2:775586529d5e | 55 | FILE *zData = fopen( "/usb/zData.txt","a"); |
GBSingh | 2:775586529d5e | 56 | FILE *timeFile = fopen( "/usb/test.txt","a"); |
GBSingh | 2:775586529d5e | 57 | if(gps.sample()) {//checks if it can get a gps position |
GBSingh | 2:775586529d5e | 58 | lcd.locate(0,10);//initial LCD location |
GBSingh | 2:775586529d5e | 59 | lcd.printf("Long: %.4f ", gps.longitude);//print to LCD |
GBSingh | 2:775586529d5e | 60 | lcd.printf("Lat: %.4f", gps.latitude);//print to LCD |
GBSingh | 2:775586529d5e | 61 | fprintf(longd,"%f\n",gps.longitude);//prints gps lonitude to file |
GBSingh | 2:775586529d5e | 62 | fprintf(latd,"%f\n",gps.latitude);//prints gps latitude to file |
GBSingh | 2:775586529d5e | 63 | writeTime.stop();//stops timer |
GBSingh | 2:775586529d5e | 64 | fprintf(timeFile,"%f\t",writeTime.read());//write time to file |
GBSingh | 2:775586529d5e | 65 | fclose(timeFile); |
GBSingh | 1:6a1228b44183 | 66 | } else { |
GBSingh | 1:6a1228b44183 | 67 | lcd.cls();//clear LCD for next reading round |
GBSingh | 1:6a1228b44183 | 68 | lcd.locate(3,3);//initial LCD location for x component of acceleration |
GBSingh | 1:6a1228b44183 | 69 | lcd.printf("Oh Dear! No lock :(\n"); |
GBSingh | 1:6a1228b44183 | 70 | fprintf(longd,"%c\n","Oh Dear! No Lock :("); |
GBSingh | 2:775586529d5e | 71 | lcd.cls();//clear LCD for next reading round |
GBSingh | 1:6a1228b44183 | 72 | } |
GBSingh | 2:775586529d5e | 73 | fclose(latd);//closes file |
GBSingh | 2:775586529d5e | 74 | fclose(longd);//closes file |
GBSingh | 2:775586529d5e | 75 | lcd.locate(0,20);//initial LCD location |
GBSingh | 2:775586529d5e | 76 | lcd.printf("Bearing is: %f\n", compass.readBearing() / 10.0);//print to LCD |
GBSingh | 2:775586529d5e | 77 | lcd.locate(0,0);//initial LCD location |
GBSingh | 2:775586529d5e | 78 | lcd.printf("X:%.2f\n",MMA.x());//print x to LCD at locate position |
GBSingh | 2:775586529d5e | 79 | lcd.locate(40,0);//move LCD location for y component |
GBSingh | 2:775586529d5e | 80 | lcd.printf("Y:%.2f\n",MMA.y());//print y to LCD to new locate position |
GBSingh | 2:775586529d5e | 81 | lcd.locate(80,0);//move LCD location for z component |
GBSingh | 2:775586529d5e | 82 | lcd.printf("Z:%.2f\n",MMA.z());//print z to LCD |
GBSingh | 2:775586529d5e | 83 | fprintf(bear,"%.4f\n",compass.readBearing() / 10.0); |
GBSingh | 2:775586529d5e | 84 | fclose(bear);//closes file |
GBSingh | 2:775586529d5e | 85 | fprintf(xData,"%f\n",MMA.x());//writes to file |
GBSingh | 2:775586529d5e | 86 | fclose(xData);//closes file |
GBSingh | 2:775586529d5e | 87 | fprintf(yData,"%f\n",MMA.y()); |
GBSingh | 2:775586529d5e | 88 | fclose(yData);//closes file |
GBSingh | 2:775586529d5e | 89 | fprintf(zData,"%f\n",MMA.z()); |
GBSingh | 2:775586529d5e | 90 | fclose(zData);//closes file |
GBSingh | 2:775586529d5e | 91 | if(fire) { |
GBSingh | 2:775586529d5e | 92 | start(); |
GBSingh | 2:775586529d5e | 93 | counter = 0; |
GBSingh | 2:775586529d5e | 94 | } |
ms523 | 0:5934350323b2 | 95 | } |
GBSingh | 1:6a1228b44183 | 96 | } |
GBSingh | 2:775586529d5e | 97 | void pitchSetUp()//function to set up the pitch |
GBSingh | 1:6a1228b44183 | 98 | { |
GBSingh | 2:775586529d5e | 99 | lcd.cls(); |
GBSingh | 2:775586529d5e | 100 | FILE *set = fopen( "/usb/setup.txt","w"); |
GBSingh | 2:775586529d5e | 101 | |
GBSingh | 1:6a1228b44183 | 102 | while (lock == 0) { |
GBSingh | 1:6a1228b44183 | 103 | lcd.locate(3,3); |
GBSingh | 2:775586529d5e | 104 | lcd.printf("Move to position 1\n"); |
GBSingh | 2:775586529d5e | 105 | if(fire) {//if joystick is pressed down |
GBSingh | 2:775586529d5e | 106 | if(gps.sample()) { |
GBSingh | 2:775586529d5e | 107 | fprintf(set,"%.5f ",gps.longitude); |
GBSingh | 2:775586529d5e | 108 | fprintf(set,"%.5f\n",gps.latitude); |
GBSingh | 2:775586529d5e | 109 | wait (0.5); |
GBSingh | 2:775586529d5e | 110 | lock = 1; |
GBSingh | 2:775586529d5e | 111 | } else { |
GBSingh | 2:775586529d5e | 112 | lcd.cls(); |
GBSingh | 2:775586529d5e | 113 | lcd.printf("No Lock Please try again\n"); |
GBSingh | 2:775586529d5e | 114 | lcd.cls();//clear LCD for next reading round |
GBSingh | 2:775586529d5e | 115 | lock = 0; |
GBSingh | 2:775586529d5e | 116 | } |
GBSingh | 1:6a1228b44183 | 117 | } |
ms523 | 0:5934350323b2 | 118 | } |
GBSingh | 1:6a1228b44183 | 119 | while (lock2 == 0) { |
GBSingh | 1:6a1228b44183 | 120 | lcd.locate(3,3); |
GBSingh | 2:775586529d5e | 121 | lcd.printf("Move to position 2\n"); |
GBSingh | 1:6a1228b44183 | 122 | if(fire) { |
GBSingh | 1:6a1228b44183 | 123 | if(gps.sample()) { |
GBSingh | 1:6a1228b44183 | 124 | fprintf(set,"%.5f ",gps.longitude); |
GBSingh | 1:6a1228b44183 | 125 | fprintf(set,"%.5f\n",gps.latitude); |
GBSingh | 2:775586529d5e | 126 | wait (0.5); |
GBSingh | 1:6a1228b44183 | 127 | lock2 = 1; |
GBSingh | 1:6a1228b44183 | 128 | } else { |
GBSingh | 1:6a1228b44183 | 129 | lcd.cls(); |
GBSingh | 1:6a1228b44183 | 130 | lcd.printf("No Lock Please try again\n"); |
GBSingh | 2:775586529d5e | 131 | lcd.cls();//clear LCD for next reading round |
GBSingh | 1:6a1228b44183 | 132 | lock2 = 0; |
GBSingh | 1:6a1228b44183 | 133 | } |
GBSingh | 1:6a1228b44183 | 134 | } |
ms523 | 0:5934350323b2 | 135 | } |
GBSingh | 1:6a1228b44183 | 136 | while (lock3 == 0) { |
GBSingh | 1:6a1228b44183 | 137 | lcd.locate(3,3); |
GBSingh | 2:775586529d5e | 138 | lcd.printf("Move to position 3\n"); |
GBSingh | 1:6a1228b44183 | 139 | if(fire) { |
GBSingh | 1:6a1228b44183 | 140 | if(gps.sample()) { |
GBSingh | 1:6a1228b44183 | 141 | fprintf(set,"%.5f ",gps.longitude); |
GBSingh | 1:6a1228b44183 | 142 | fprintf(set,"%.5f\n",gps.latitude); |
GBSingh | 2:775586529d5e | 143 | wait (0.5); |
GBSingh | 1:6a1228b44183 | 144 | lock3 = 1; |
GBSingh | 1:6a1228b44183 | 145 | } else { |
GBSingh | 1:6a1228b44183 | 146 | lcd.cls(); |
GBSingh | 1:6a1228b44183 | 147 | lcd.printf("No Lock Please try again\n"); |
GBSingh | 2:775586529d5e | 148 | lcd.cls();//clear LCD for next reading round |
GBSingh | 1:6a1228b44183 | 149 | lock3 = 0; |
GBSingh | 1:6a1228b44183 | 150 | } |
GBSingh | 1:6a1228b44183 | 151 | } |
GBSingh | 1:6a1228b44183 | 152 | } |
GBSingh | 1:6a1228b44183 | 153 | while (lock4 == 0) { |
GBSingh | 1:6a1228b44183 | 154 | lcd.locate(3,3); |
GBSingh | 2:775586529d5e | 155 | lcd.printf("Move to position 4\n"); |
GBSingh | 1:6a1228b44183 | 156 | if(fire) { |
GBSingh | 1:6a1228b44183 | 157 | if(gps.sample()) { |
GBSingh | 1:6a1228b44183 | 158 | fprintf(set,"%.5f ",gps.longitude); |
GBSingh | 1:6a1228b44183 | 159 | fprintf(set,"%.5f\n",gps.latitude); |
GBSingh | 2:775586529d5e | 160 | wait (0.5); |
GBSingh | 1:6a1228b44183 | 161 | lock4 = 1; |
GBSingh | 1:6a1228b44183 | 162 | } else { |
GBSingh | 1:6a1228b44183 | 163 | lcd.cls(); |
GBSingh | 1:6a1228b44183 | 164 | lcd.printf("No Lock Please try again\n"); |
GBSingh | 2:775586529d5e | 165 | lcd.cls();//clear LCD for next reading round |
GBSingh | 1:6a1228b44183 | 166 | lock4 = 0; |
GBSingh | 1:6a1228b44183 | 167 | } |
GBSingh | 1:6a1228b44183 | 168 | } |
GBSingh | 1:6a1228b44183 | 169 | } |
GBSingh | 2:775586529d5e | 170 | fclose(set); |
GBSingh | 2:775586529d5e | 171 | start();//runs the run fution |
GBSingh | 2:775586529d5e | 172 | |
GBSingh | 2:775586529d5e | 173 | } |
GBSingh | 2:775586529d5e | 174 | int main()//main funtion |
GBSingh | 2:775586529d5e | 175 | { |
GBSingh | 2:775586529d5e | 176 | //give choice to what the user wants to do |
GBSingh | 2:775586529d5e | 177 | lcd.cls(); |
GBSingh | 2:775586529d5e | 178 | while (choice == 0) { |
GBSingh | 2:775586529d5e | 179 | int choice2 =0; |
GBSingh | 2:775586529d5e | 180 | lcd.locate(3,3); |
GBSingh | 2:775586529d5e | 181 | lcd.printf("Pitch Set Up\n"); |
GBSingh | 2:775586529d5e | 182 | if (fire) {//if joystick is pressed down |
GBSingh | 2:775586529d5e | 183 | pitchSetUp();//runs pitch setup funtion |
GBSingh | 2:775586529d5e | 184 | choice =1; |
GBSingh | 2:775586529d5e | 185 | } |
GBSingh | 2:775586529d5e | 186 | if(right) {//if joystick is pressed right |
GBSingh | 2:775586529d5e | 187 | lcd.cls(); |
GBSingh | 2:775586529d5e | 188 | lcd.locate(3,3); |
GBSingh | 2:775586529d5e | 189 | lcd.printf("Run Game\n"); |
GBSingh | 2:775586529d5e | 190 | //counter = counter++; |
GBSingh | 2:775586529d5e | 191 | wait(0.5);//wait for half a second |
GBSingh | 2:775586529d5e | 192 | |
GBSingh | 2:775586529d5e | 193 | while (choice2 == 0) { |
GBSingh | 2:775586529d5e | 194 | if (fire) { |
GBSingh | 2:775586529d5e | 195 | start();//runs the run funtion |
GBSingh | 2:775586529d5e | 196 | choice =1; |
GBSingh | 2:775586529d5e | 197 | choice2 = 1; |
GBSingh | 2:775586529d5e | 198 | } |
GBSingh | 2:775586529d5e | 199 | if (left) {//if joystick is pressed to the left |
GBSingh | 2:775586529d5e | 200 | choice2 = 1; |
GBSingh | 2:775586529d5e | 201 | choice = 0; |
GBSingh | 2:775586529d5e | 202 | main();//runs the main funtion |
GBSingh | 2:775586529d5e | 203 | } |
GBSingh | 2:775586529d5e | 204 | } |
GBSingh | 2:775586529d5e | 205 | } |
GBSingh | 2:775586529d5e | 206 | } |
ms523 | 0:5934350323b2 | 207 | } |