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.
Fork of DocTest by
main.cpp@3:b78d65300f24, 2015-05-10 (annotated)
- Committer:
- 200784123
- Date:
- Sun May 10 20:07:18 2015 +0000
- Revision:
- 3:b78d65300f24
- Parent:
- 2:5446154a4028
Sorted a few notation issues
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| eencae | 0:b85460bc73b9 | 1 | /** |
| eencae | 0:b85460bc73b9 | 2 | @file main.cpp |
| eencae | 0:b85460bc73b9 | 3 | |
| eencae | 0:b85460bc73b9 | 4 | @brief Program implementation |
| eencae | 0:b85460bc73b9 | 5 | |
| eencae | 0:b85460bc73b9 | 6 | */ |
| eencae | 0:b85460bc73b9 | 7 | #include "main.h" |
| eencae | 0:b85460bc73b9 | 8 | |
| 200784123 | 2:5446154a4028 | 9 | int main(){ |
| 200784123 | 2:5446154a4028 | 10 | ///minimal amount of code in the main, see All() for more |
| 200784123 | 2:5446154a4028 | 11 | All(); |
| 200784123 | 2:5446154a4028 | 12 | } |
| 200784123 | 3:b78d65300f24 | 13 | ///all of the function that would normally reside in the main |
| 200784123 | 2:5446154a4028 | 14 | void All(){ |
| 200784123 | 2:5446154a4028 | 15 | calibrateJoystick(); ///get the centred values of the joystick |
| 200784123 | 2:5446154a4028 | 16 | bright=5; |
| eencae | 0:b85460bc73b9 | 17 | while(1) { |
| 200784123 | 2:5446154a4028 | 18 | if(screen) |
| 200784123 | 2:5446154a4028 | 19 | lcd.inverseMode(); |
| 200784123 | 2:5446154a4028 | 20 | if(!screen) |
| 200784123 | 2:5446154a4028 | 21 | lcd.normalMode(); |
| 200784123 | 2:5446154a4028 | 22 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 23 | Initialise(); |
| 200784123 | 2:5446154a4028 | 24 | start(); |
| 200784123 | 2:5446154a4028 | 25 | } |
| 200784123 | 2:5446154a4028 | 26 | } |
| 200784123 | 2:5446154a4028 | 27 | |
| 200784123 | 2:5446154a4028 | 28 | ///all of the variable, flags, tickers, and anything else that needs to set each time it's powered up |
| 200784123 | 2:5446154a4028 | 29 | void Initialise(){ |
| 200784123 | 2:5446154a4028 | 30 | lcd.init(); |
| 200784123 | 2:5446154a4028 | 31 | Power.attach(&powerDown,1.0);///increases the power counter every second |
| 200784123 | 2:5446154a4028 | 32 | backlight.attach(&brightness,1.0/10.0);///updates the brightness 10 times a second |
| 200784123 | 2:5446154a4028 | 33 | pollJoystick.attach(&updateJoystick,1.0/10.0); /// read joystick 10 times per second |
| 200784123 | 2:5446154a4028 | 34 | i=1; |
| 200784123 | 2:5446154a4028 | 35 | j=24; |
| 200784123 | 2:5446154a4028 | 36 | life=3; |
| 200784123 | 2:5446154a4028 | 37 | winFlag=0; |
| 200784123 | 2:5446154a4028 | 38 | startFlag=1; |
| 200784123 | 2:5446154a4028 | 39 | buzzer.period(0.002); |
| 200784123 | 2:5446154a4028 | 40 | c=1; |
| 200784123 | 2:5446154a4028 | 41 | } |
| 200784123 | 2:5446154a4028 | 42 | |
| 200784123 | 3:b78d65300f24 | 43 | ///goes into deep power down if nothing happens for a set time |
| 200784123 | 3:b78d65300f24 | 44 | void powerDown(){ |
| 200784123 | 2:5446154a4028 | 45 | power++; |
| 200784123 | 2:5446154a4028 | 46 | if(power==55){ |
| 200784123 | 2:5446154a4028 | 47 | red1=0; |
| 200784123 | 2:5446154a4028 | 48 | red2=0.1*bright; |
| 200784123 | 2:5446154a4028 | 49 | red3=0; |
| 200784123 | 2:5446154a4028 | 50 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 51 | buzzer=0.4; |
| 200784123 | 2:5446154a4028 | 52 | } |
| 200784123 | 2:5446154a4028 | 53 | if(power==56){ |
| 200784123 | 2:5446154a4028 | 54 | red1=0.1*bright; |
| 200784123 | 2:5446154a4028 | 55 | red2=0; |
| 200784123 | 2:5446154a4028 | 56 | red3=0.1*bright; |
| 200784123 | 2:5446154a4028 | 57 | yel=0; |
| 200784123 | 2:5446154a4028 | 58 | buzzer=0.9; |
| 200784123 | 2:5446154a4028 | 59 | } |
| 200784123 | 2:5446154a4028 | 60 | if(power==57){ |
| 200784123 | 2:5446154a4028 | 61 | red1=0; |
| 200784123 | 2:5446154a4028 | 62 | red2=0.1*bright; |
| 200784123 | 2:5446154a4028 | 63 | red3=0; |
| 200784123 | 2:5446154a4028 | 64 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 65 | buzzer=0.4; |
| 200784123 | 2:5446154a4028 | 66 | } |
| 200784123 | 2:5446154a4028 | 67 | if(power==58){ |
| 200784123 | 2:5446154a4028 | 68 | red1=0.1*bright; |
| 200784123 | 2:5446154a4028 | 69 | red2=0; |
| 200784123 | 2:5446154a4028 | 70 | red3=0.1*bright; |
| 200784123 | 2:5446154a4028 | 71 | yel=0; |
| 200784123 | 2:5446154a4028 | 72 | buzzer=0.9; |
| 200784123 | 2:5446154a4028 | 73 | } |
| 200784123 | 2:5446154a4028 | 74 | if(power==59){ |
| 200784123 | 2:5446154a4028 | 75 | red1=0; |
| 200784123 | 2:5446154a4028 | 76 | red2=0.1*bright; |
| 200784123 | 2:5446154a4028 | 77 | red3=0; |
| 200784123 | 2:5446154a4028 | 78 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 79 | buzzer=0.4; |
| 200784123 | 2:5446154a4028 | 80 | } |
| 200784123 | 3:b78d65300f24 | 81 | if(power>=60){ |
| 200784123 | 3:b78d65300f24 | 82 | red1=0.1*bright; |
| 200784123 | 3:b78d65300f24 | 83 | red2=0; |
| 200784123 | 3:b78d65300f24 | 84 | red3=0.1*bright; |
| 200784123 | 3:b78d65300f24 | 85 | yel=0; |
| 200784123 | 2:5446154a4028 | 86 | buzzer=0.9; |
| 200784123 | 2:5446154a4028 | 87 | DeepPowerDown(); |
| eencae | 0:b85460bc73b9 | 88 | } |
| eencae | 0:b85460bc73b9 | 89 | } |
| eencae | 0:b85460bc73b9 | 90 | |
| eencae | 0:b85460bc73b9 | 91 | |
| 200784123 | 2:5446154a4028 | 92 | ///the start screen, shows up at the begining and when the player runs out of lives |
| 200784123 | 3:b78d65300f24 | 93 | ///links to the options menu, the highscores, the level select etc. |
| 200784123 | 2:5446154a4028 | 94 | void start(){ |
| 200784123 | 2:5446154a4028 | 95 | power=0; |
| 200784123 | 2:5446154a4028 | 96 | clear(); |
| 200784123 | 2:5446154a4028 | 97 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 98 | butt.rise(&menus); |
| 200784123 | 2:5446154a4028 | 99 | push.rise(&menus); |
| 200784123 | 2:5446154a4028 | 100 | startFlag=1; |
| 200784123 | 2:5446154a4028 | 101 | while(c) { |
| 200784123 | 2:5446154a4028 | 102 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 103 | lcd.printString("Click",25,2); |
| 200784123 | 2:5446154a4028 | 104 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 105 | } |
| 200784123 | 2:5446154a4028 | 106 | clear(); |
| 200784123 | 2:5446154a4028 | 107 | while(startFlag) { |
| 200784123 | 2:5446154a4028 | 108 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 109 | while(joystick.direction!=CENTRE) { |
| 200784123 | 2:5446154a4028 | 110 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 111 | ///do nothing, stay on current screen |
| 200784123 | 2:5446154a4028 | 112 | lcd.printString("^ Options ^",10,0); |
| 200784123 | 2:5446154a4028 | 113 | lcd.printString("< Score Inv. >",0,2); |
| 200784123 | 2:5446154a4028 | 114 | lcd.printString("v Levels v",12,5); |
| 200784123 | 2:5446154a4028 | 115 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 116 | } |
| 200784123 | 2:5446154a4028 | 117 | if(joystick.direction==CENTRE) { |
| 200784123 | 2:5446154a4028 | 118 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 119 | lcd.printString("^ Options ^",10,0); |
| 200784123 | 2:5446154a4028 | 120 | lcd.printString("< Score Inv. >",0,2); |
| 200784123 | 2:5446154a4028 | 121 | lcd.printString("v Levels v",12,5); |
| 200784123 | 2:5446154a4028 | 122 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 123 | ///for some reason if UP is used to change the flag then it goes straight to the maze |
| 200784123 | 2:5446154a4028 | 124 | } |
| 200784123 | 2:5446154a4028 | 125 | if(joystick.direction==UP) { |
| 200784123 | 2:5446154a4028 | 126 | power=0; |
| 200784123 | 2:5446154a4028 | 127 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 128 | toggle(); |
| 200784123 | 2:5446154a4028 | 129 | Options(); |
| 200784123 | 2:5446154a4028 | 130 | } |
| 200784123 | 2:5446154a4028 | 131 | if (joystick.direction==DOWN) { |
| 200784123 | 2:5446154a4028 | 132 | power=0; |
| 200784123 | 2:5446154a4028 | 133 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 134 | toggle(); |
| 200784123 | 2:5446154a4028 | 135 | Levels(); |
| 200784123 | 2:5446154a4028 | 136 | } |
| 200784123 | 2:5446154a4028 | 137 | if(joystick.direction==LEFT) { |
| 200784123 | 2:5446154a4028 | 138 | power=0; |
| 200784123 | 2:5446154a4028 | 139 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 140 | toggle(); |
| 200784123 | 2:5446154a4028 | 141 | Score(); |
| 200784123 | 2:5446154a4028 | 142 | } |
| 200784123 | 2:5446154a4028 | 143 | if(joystick.direction==RIGHT) { |
| 200784123 | 2:5446154a4028 | 144 | power=0; |
| 200784123 | 2:5446154a4028 | 145 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 146 | ///go to the inverse function |
| 200784123 | 2:5446154a4028 | 147 | screen=!screen; |
| 200784123 | 2:5446154a4028 | 148 | if(screen) |
| 200784123 | 2:5446154a4028 | 149 | lcd.inverseMode(); |
| 200784123 | 2:5446154a4028 | 150 | if(!screen) |
| 200784123 | 2:5446154a4028 | 151 | lcd.normalMode(); |
| 200784123 | 2:5446154a4028 | 152 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 153 | } |
| 200784123 | 2:5446154a4028 | 154 | } |
| 200784123 | 2:5446154a4028 | 155 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 156 | clear(); |
| 200784123 | 2:5446154a4028 | 157 | } |
| 200784123 | 3:b78d65300f24 | 158 | ///function called to escape back to the main menu |
| 200784123 | 2:5446154a4028 | 159 | void menus(){ |
| 200784123 | 2:5446154a4028 | 160 | power=0; |
| 200784123 | 2:5446154a4028 | 161 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 162 | clear(); |
| 200784123 | 2:5446154a4028 | 163 | timeout.attach_us(&toggle,10); |
| 200784123 | 2:5446154a4028 | 164 | } |
| 200784123 | 3:b78d65300f24 | 165 | ///function called to toggle, clear, or set various flags |
| 200784123 | 2:5446154a4028 | 166 | void toggle(){ |
| 200784123 | 2:5446154a4028 | 167 | power=0; |
| 200784123 | 2:5446154a4028 | 168 | l=!l; |
| 200784123 | 2:5446154a4028 | 169 | r=!r; |
| 200784123 | 2:5446154a4028 | 170 | u=!u; |
| 200784123 | 2:5446154a4028 | 171 | d=!d; |
| 200784123 | 2:5446154a4028 | 172 | c=0; |
| 200784123 | 2:5446154a4028 | 173 | b=!b; |
| 200784123 | 2:5446154a4028 | 174 | startFlag=1; |
| 200784123 | 2:5446154a4028 | 175 | } |
| 200784123 | 3:b78d65300f24 | 176 | ///the options menu, links to the reset, brightness, and end game |
| 200784123 | 2:5446154a4028 | 177 | void Options(){ |
| 200784123 | 2:5446154a4028 | 178 | power=0; |
| 200784123 | 2:5446154a4028 | 179 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 180 | clear(); |
| 200784123 | 2:5446154a4028 | 181 | while(u) { |
| 200784123 | 2:5446154a4028 | 182 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 183 | while(joystick.direction!=CENTRE) { |
| 200784123 | 2:5446154a4028 | 184 | ///do nothing, just sit on current screen |
| 200784123 | 2:5446154a4028 | 185 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 186 | lcd.printString("~~~~~~~~~~~~~~",0,0); |
| 200784123 | 2:5446154a4028 | 187 | lcd.printString("^ Brightness ^",0,1); |
| 200784123 | 2:5446154a4028 | 188 | lcd.printString("< Reset",10,2); |
| 200784123 | 2:5446154a4028 | 189 | lcd.printString("End >",30,3); |
| 200784123 | 2:5446154a4028 | 190 | lcd.printString("v Back v",15,4); |
| 200784123 | 2:5446154a4028 | 191 | lcd.printString("~~~~~~~~~~~~~~",0,5); |
| 200784123 | 2:5446154a4028 | 192 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 193 | } |
| 200784123 | 2:5446154a4028 | 194 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 195 | lcd.printString("~~~~~~~~~~~~~~",0,0); |
| 200784123 | 2:5446154a4028 | 196 | lcd.printString("^ Brightness ^",0,1); |
| 200784123 | 2:5446154a4028 | 197 | lcd.printString("< Reset",10,2); |
| 200784123 | 2:5446154a4028 | 198 | lcd.printString("End >",30,3); |
| 200784123 | 2:5446154a4028 | 199 | lcd.printString("v Back v",15,4); |
| 200784123 | 2:5446154a4028 | 200 | lcd.printString("~~~~~~~~~~~~~~",0,5); |
| 200784123 | 2:5446154a4028 | 201 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 202 | if(joystick.direction==DOWN) { |
| 200784123 | 2:5446154a4028 | 203 | power=0; |
| 200784123 | 2:5446154a4028 | 204 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 205 | ///escape back to the first menu |
| 200784123 | 2:5446154a4028 | 206 | start(); |
| 200784123 | 2:5446154a4028 | 207 | } |
| 200784123 | 2:5446154a4028 | 208 | if (joystick.direction==UP) { |
| 200784123 | 2:5446154a4028 | 209 | power=0; |
| 200784123 | 2:5446154a4028 | 210 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 211 | ///go to the brightness function |
| 200784123 | 2:5446154a4028 | 212 | clear(); |
| 200784123 | 2:5446154a4028 | 213 | while(b) { |
| 200784123 | 2:5446154a4028 | 214 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 215 | red1=0.1*bright; |
| 200784123 | 2:5446154a4028 | 216 | red2=0.1*bright; |
| 200784123 | 2:5446154a4028 | 217 | red3=0.1*bright; |
| 200784123 | 2:5446154a4028 | 218 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 219 | push.rise(&Dark); |
| 200784123 | 2:5446154a4028 | 220 | butt.rise(&Light); |
| 200784123 | 2:5446154a4028 | 221 | lcd.printString("click joy - up",0,1); |
| 200784123 | 2:5446154a4028 | 222 | lcd.printString("or pink - down",0,3); |
| 200784123 | 2:5446154a4028 | 223 | lcd.printString("v Back v",15,5); |
| 200784123 | 2:5446154a4028 | 224 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 225 | if(joystick.direction==DOWN) { |
| 200784123 | 2:5446154a4028 | 226 | Options(); |
| 200784123 | 2:5446154a4028 | 227 | } |
| 200784123 | 2:5446154a4028 | 228 | } |
| 200784123 | 2:5446154a4028 | 229 | } |
| 200784123 | 2:5446154a4028 | 230 | if (joystick.direction==LEFT) { |
| 200784123 | 2:5446154a4028 | 231 | power=0; |
| 200784123 | 2:5446154a4028 | 232 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 233 | toggle(); |
| 200784123 | 2:5446154a4028 | 234 | score1=0; |
| 200784123 | 2:5446154a4028 | 235 | scoreWrite1(); |
| 200784123 | 2:5446154a4028 | 236 | score2=0; |
| 200784123 | 2:5446154a4028 | 237 | scoreWrite2(); |
| 200784123 | 2:5446154a4028 | 238 | score3=0; |
| 200784123 | 2:5446154a4028 | 239 | scoreWrite3(); |
| 200784123 | 2:5446154a4028 | 240 | } |
| 200784123 | 2:5446154a4028 | 241 | if(joystick.direction==RIGHT) { |
| 200784123 | 2:5446154a4028 | 242 | power=0; |
| 200784123 | 2:5446154a4028 | 243 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 244 | winFlag=1; |
| 200784123 | 2:5446154a4028 | 245 | endGame(); |
| 200784123 | 2:5446154a4028 | 246 | } |
| 200784123 | 2:5446154a4028 | 247 | } |
| 200784123 | 2:5446154a4028 | 248 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 249 | clear(); |
| 200784123 | 2:5446154a4028 | 250 | } |
| 200784123 | 3:b78d65300f24 | 251 | ///debouncing code for the light() function |
| 200784123 | 2:5446154a4028 | 252 | void Light(){ |
| 200784123 | 2:5446154a4028 | 253 | timeout.attach_us(&light,100); |
| 200784123 | 2:5446154a4028 | 254 | } |
| 200784123 | 3:b78d65300f24 | 255 | ///debouncing code for the dark() function |
| 200784123 | 2:5446154a4028 | 256 | void Dark(){ |
| 200784123 | 2:5446154a4028 | 257 | timeout.attach_us(&dark,100); |
| 200784123 | 2:5446154a4028 | 258 | } |
| 200784123 | 3:b78d65300f24 | 259 | ///function called to increase the LED and backlight birghtness variable |
| 200784123 | 2:5446154a4028 | 260 | void light(){ |
| 200784123 | 2:5446154a4028 | 261 | bright++; |
| 200784123 | 2:5446154a4028 | 262 | if(bright>10) |
| 200784123 | 2:5446154a4028 | 263 | bright=10; |
| 200784123 | 2:5446154a4028 | 264 | } |
| 200784123 | 3:b78d65300f24 | 265 | ///function called to decrease the LED and backlight brightness variable |
| 200784123 | 2:5446154a4028 | 266 | void dark(){ |
| 200784123 | 2:5446154a4028 | 267 | bright--; |
| 200784123 | 2:5446154a4028 | 268 | if(bright<1) |
| 200784123 | 2:5446154a4028 | 269 | bright=1; |
| 200784123 | 2:5446154a4028 | 270 | } |
| 200784123 | 3:b78d65300f24 | 271 | ///function called to set the LCD backlight brightness from the brigtness variable |
| 200784123 | 2:5446154a4028 | 272 | void brightness(){ |
| 200784123 | 2:5446154a4028 | 273 | lcd.setBrightness(0.1*bright); |
| 200784123 | 2:5446154a4028 | 274 | } |
| 200784123 | 3:b78d65300f24 | 275 | ///the level select menu, allows the player to choose between three levels by moveing the joystick left, down, or right |
| 200784123 | 2:5446154a4028 | 276 | void Levels(){ |
| 200784123 | 2:5446154a4028 | 277 | power=0; |
| 200784123 | 2:5446154a4028 | 278 | push.rise(&menus); |
| 200784123 | 2:5446154a4028 | 279 | butt.rise(&menus); |
| 200784123 | 2:5446154a4028 | 280 | clear(); |
| 200784123 | 2:5446154a4028 | 281 | while(d) { |
| 200784123 | 2:5446154a4028 | 282 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 283 | while(joystick.direction!=CENTRE) { |
| 200784123 | 2:5446154a4028 | 284 | ///do nothing, stay on current screen |
| 200784123 | 2:5446154a4028 | 285 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 286 | lcd.printString("~~~~~~~~~~~~~~",0,0); |
| 200784123 | 2:5446154a4028 | 287 | lcd.printString("Left = 1",10,2); |
| 200784123 | 2:5446154a4028 | 288 | lcd.printString("Down = 2",10,3); |
| 200784123 | 2:5446154a4028 | 289 | lcd.printString("Right = 3",10,4); |
| 200784123 | 2:5446154a4028 | 290 | lcd.printString("^ Back ^",15,1); |
| 200784123 | 2:5446154a4028 | 291 | lcd.printString("~~~~~~~~~~~~~~",0,5); |
| 200784123 | 2:5446154a4028 | 292 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 293 | } |
| 200784123 | 2:5446154a4028 | 294 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 295 | lcd.printString("~~~~~~~~~~~~~~",0,0); |
| 200784123 | 2:5446154a4028 | 296 | lcd.printString("Left = 1",10,2); |
| 200784123 | 2:5446154a4028 | 297 | lcd.printString("Down = 2",10,3); |
| 200784123 | 2:5446154a4028 | 298 | lcd.printString("Right = 3",10,4); |
| 200784123 | 2:5446154a4028 | 299 | lcd.printString("^ Back ^",15,1); |
| 200784123 | 2:5446154a4028 | 300 | lcd.printString("~~~~~~~~~~~~~~",0,5); |
| 200784123 | 2:5446154a4028 | 301 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 302 | if(joystick.direction==UP) { |
| 200784123 | 2:5446154a4028 | 303 | power=0; |
| 200784123 | 2:5446154a4028 | 304 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 305 | ///escape back to the first menu |
| 200784123 | 2:5446154a4028 | 306 | toggle(); |
| 200784123 | 2:5446154a4028 | 307 | start(); |
| 200784123 | 2:5446154a4028 | 308 | } |
| 200784123 | 2:5446154a4028 | 309 | if(joystick.direction==LEFT) { |
| 200784123 | 2:5446154a4028 | 310 | power=0; |
| 200784123 | 2:5446154a4028 | 311 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 312 | ///go to the preview screen for level 1 |
| 200784123 | 2:5446154a4028 | 313 | clear(); |
| 200784123 | 2:5446154a4028 | 314 | while(joystick.direction==LEFT) { |
| 200784123 | 2:5446154a4028 | 315 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 316 | lcd.printString("Level 1",15,2); |
| 200784123 | 2:5446154a4028 | 317 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 318 | mazeFlag=1; |
| 200784123 | 2:5446154a4028 | 319 | } |
| 200784123 | 2:5446154a4028 | 320 | clear(); |
| 200784123 | 2:5446154a4028 | 321 | mainGame(); |
| 200784123 | 2:5446154a4028 | 322 | } |
| 200784123 | 2:5446154a4028 | 323 | if(joystick.direction==DOWN) { |
| 200784123 | 2:5446154a4028 | 324 | power=0; |
| 200784123 | 2:5446154a4028 | 325 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 326 | ///go to the preview screen for level 2 |
| 200784123 | 2:5446154a4028 | 327 | clear(); |
| 200784123 | 2:5446154a4028 | 328 | while(joystick.direction==DOWN) { |
| 200784123 | 2:5446154a4028 | 329 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 330 | lcd.printString("Level 2",15,2); |
| 200784123 | 2:5446154a4028 | 331 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 332 | mazeFlag=2; |
| 200784123 | 2:5446154a4028 | 333 | } |
| 200784123 | 2:5446154a4028 | 334 | clear(); |
| 200784123 | 2:5446154a4028 | 335 | mainGame(); |
| 200784123 | 2:5446154a4028 | 336 | } |
| 200784123 | 2:5446154a4028 | 337 | if(joystick.direction==RIGHT) { |
| 200784123 | 2:5446154a4028 | 338 | power=0; |
| 200784123 | 2:5446154a4028 | 339 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 340 | ///go to the preview screen for level 3 |
| 200784123 | 2:5446154a4028 | 341 | clear(); |
| 200784123 | 2:5446154a4028 | 342 | while(joystick.direction==RIGHT) { |
| 200784123 | 2:5446154a4028 | 343 | power=0; |
| 200784123 | 2:5446154a4028 | 344 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 345 | lcd.printString("Level 3",15,2); |
| 200784123 | 2:5446154a4028 | 346 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 347 | mazeFlag=3; |
| 200784123 | 2:5446154a4028 | 348 | } |
| 200784123 | 2:5446154a4028 | 349 | clear(); |
| 200784123 | 2:5446154a4028 | 350 | mainGame(); |
| 200784123 | 2:5446154a4028 | 351 | } |
| 200784123 | 2:5446154a4028 | 352 | |
| 200784123 | 2:5446154a4028 | 353 | } |
| 200784123 | 2:5446154a4028 | 354 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 355 | clear(); |
| 200784123 | 2:5446154a4028 | 356 | } |
| 200784123 | 3:b78d65300f24 | 357 | ///the scores menu, displays the currently saved high score for the three levels |
| 200784123 | 3:b78d65300f24 | 358 | /// moving the joystick different directions displays the scores for the different levels |
| 200784123 | 2:5446154a4028 | 359 | void Score(){ |
| 200784123 | 2:5446154a4028 | 360 | power=0; |
| 200784123 | 2:5446154a4028 | 361 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 362 | push.rise(&menus); |
| 200784123 | 2:5446154a4028 | 363 | butt.rise(&menus); |
| 200784123 | 2:5446154a4028 | 364 | clear(); |
| 200784123 | 2:5446154a4028 | 365 | while(l) { |
| 200784123 | 2:5446154a4028 | 366 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 367 | while(joystick.direction!=CENTRE) { |
| 200784123 | 2:5446154a4028 | 368 | ///do nothing, stay on current screen |
| 200784123 | 2:5446154a4028 | 369 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 370 | lcd.printString("~~~~~~~~~~~~~~",0,0); |
| 200784123 | 2:5446154a4028 | 371 | lcd.printString("High Score",10,1); |
| 200784123 | 2:5446154a4028 | 372 | lcd.printString("> Back >",15,4); |
| 200784123 | 2:5446154a4028 | 373 | lcd.printString("~~~~~~~~~~~~~~",0,5); |
| 200784123 | 2:5446154a4028 | 374 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 375 | } |
| 200784123 | 2:5446154a4028 | 376 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 377 | lcd.printString("~~~~~~~~~~~~~~",0,0); |
| 200784123 | 2:5446154a4028 | 378 | lcd.printString("High Score",10,1); |
| 200784123 | 2:5446154a4028 | 379 | lcd.printString("> Back >",15,4); |
| 200784123 | 2:5446154a4028 | 380 | lcd.printString("~~~~~~~~~~~~~~",0,5); |
| 200784123 | 2:5446154a4028 | 381 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 382 | if(joystick.direction==RIGHT) { |
| 200784123 | 2:5446154a4028 | 383 | power=0; |
| 200784123 | 2:5446154a4028 | 384 | ///escape back to the first menu |
| 200784123 | 2:5446154a4028 | 385 | start(); |
| 200784123 | 2:5446154a4028 | 386 | } |
| 200784123 | 2:5446154a4028 | 387 | if(joystick.direction==UP) { |
| 200784123 | 2:5446154a4028 | 388 | power=0; |
| 200784123 | 2:5446154a4028 | 389 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 390 | ///show the high score/fastest time for level 1 |
| 200784123 | 2:5446154a4028 | 391 | clear(); |
| 200784123 | 2:5446154a4028 | 392 | while(joystick.direction!=DOWN){ |
| 200784123 | 2:5446154a4028 | 393 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 394 | lcd.printString("Level 1 best",7,1); |
| 200784123 | 2:5446154a4028 | 395 | scoreRead1(); |
| 200784123 | 2:5446154a4028 | 396 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 397 | } |
| 200784123 | 2:5446154a4028 | 398 | clear(); |
| 200784123 | 2:5446154a4028 | 399 | }else if(joystick.direction==LEFT) { |
| 200784123 | 2:5446154a4028 | 400 | power=0; |
| 200784123 | 2:5446154a4028 | 401 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 402 | ///show the high score/fastest time for level 2 |
| 200784123 | 2:5446154a4028 | 403 | clear(); |
| 200784123 | 2:5446154a4028 | 404 | while(joystick.direction!=RIGHT){ |
| 200784123 | 2:5446154a4028 | 405 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 406 | lcd.printString("Level 2 best",7,1); |
| 200784123 | 2:5446154a4028 | 407 | scoreRead2(); |
| 200784123 | 2:5446154a4028 | 408 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 409 | } |
| 200784123 | 2:5446154a4028 | 410 | clear(); |
| 200784123 | 2:5446154a4028 | 411 | }else if(joystick.direction==DOWN) { |
| 200784123 | 2:5446154a4028 | 412 | power=0; |
| 200784123 | 2:5446154a4028 | 413 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 414 | ///show the high score/fastest time for level 3 |
| 200784123 | 2:5446154a4028 | 415 | clear(); |
| 200784123 | 2:5446154a4028 | 416 | while(joystick.direction!=UP){ |
| 200784123 | 2:5446154a4028 | 417 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 418 | lcd.printString("Level 3 best",7,1); |
| 200784123 | 2:5446154a4028 | 419 | scoreRead3(); |
| 200784123 | 2:5446154a4028 | 420 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 421 | } |
| 200784123 | 2:5446154a4028 | 422 | clear(); |
| 200784123 | 2:5446154a4028 | 423 | } |
| 200784123 | 2:5446154a4028 | 424 | } |
| 200784123 | 2:5446154a4028 | 425 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 426 | clear(); |
| 200784123 | 2:5446154a4028 | 427 | } |
| 200784123 | 2:5446154a4028 | 428 | |
| 200784123 | 2:5446154a4028 | 429 | |
| 200784123 | 2:5446154a4028 | 430 | ///writes the score for the first level to file |
| 200784123 | 2:5446154a4028 | 431 | void scoreWrite1(){ |
| 200784123 | 2:5446154a4028 | 432 | FILE *fp = fopen("/local/score1.txt","w");///opens the file score1.txt |
| 200784123 | 2:5446154a4028 | 433 | ///if the file doesn't exist it creates it, if it exists it updates |
| 200784123 | 2:5446154a4028 | 434 | fprintf(fp,"%i",score1);///prints the integer value for the score to file |
| 200784123 | 2:5446154a4028 | 435 | fclose(fp); |
| 200784123 | 2:5446154a4028 | 436 | } |
| 200784123 | 3:b78d65300f24 | 437 | ///reads the currently saved score for the first level |
| 200784123 | 2:5446154a4028 | 438 | void scoreRead1(){ |
| 200784123 | 2:5446154a4028 | 439 | int Score1;///tells it that the value to be read is an integer value |
| 200784123 | 2:5446154a4028 | 440 | |
| 200784123 | 2:5446154a4028 | 441 | FILE *fp1 = fopen("/local/score1.txt","r");///opens score1.txt |
| 200784123 | 3:b78d65300f24 | 442 | fscanf(fp1,"%i",&Score1);///searches the file for an integer |
| 200784123 | 2:5446154a4028 | 443 | fclose(fp1); |
| 200784123 | 2:5446154a4028 | 444 | |
| 200784123 | 2:5446154a4028 | 445 | char Points1[14];///bufer to store the points value |
| 200784123 | 2:5446154a4028 | 446 | sprintf(Points1,"%i",Score1);///sends the score to the buffer |
| 200784123 | 2:5446154a4028 | 447 | lcd.printString(Points1,30,3);///prints the score to the LCD |
| 200784123 | 2:5446154a4028 | 448 | } |
| 200784123 | 2:5446154a4028 | 449 | |
| 200784123 | 2:5446154a4028 | 450 | ///writes the score for the second level to file |
| 200784123 | 2:5446154a4028 | 451 | void scoreWrite2(){ |
| 200784123 | 2:5446154a4028 | 452 | FILE *fp2 = fopen("/local/score2.txt","w");///opens the file score2.txt |
| 200784123 | 2:5446154a4028 | 453 | ///if the file doesn't exist it creates it, if it exists it updates |
| 200784123 | 2:5446154a4028 | 454 | fprintf(fp2,"%i",score2);///prints the integer value for the score to file |
| 200784123 | 2:5446154a4028 | 455 | fclose(fp2); |
| 200784123 | 2:5446154a4028 | 456 | } |
| 200784123 | 3:b78d65300f24 | 457 | ///reads the currently saved score for the second level |
| 200784123 | 2:5446154a4028 | 458 | void scoreRead2(){ |
| 200784123 | 2:5446154a4028 | 459 | int Score2;///tells it that the value to be read is an integer value |
| 200784123 | 2:5446154a4028 | 460 | |
| 200784123 | 2:5446154a4028 | 461 | FILE *fp3 = fopen("/local/score2.txt","r");///opens score2.txt |
| 200784123 | 3:b78d65300f24 | 462 | fscanf(fp3,"%i",&Score2);///searches the file for an integer |
| 200784123 | 2:5446154a4028 | 463 | fclose(fp3); |
| 200784123 | 2:5446154a4028 | 464 | |
| 200784123 | 2:5446154a4028 | 465 | char Points2[14];///bufer to store the points value |
| 200784123 | 2:5446154a4028 | 466 | sprintf(Points2,"%i",Score2);///sends the score to the buffer |
| 200784123 | 2:5446154a4028 | 467 | lcd.printString(Points2,30,3);///prints the score to the LCD |
| 200784123 | 2:5446154a4028 | 468 | } |
| 200784123 | 2:5446154a4028 | 469 | |
| 200784123 | 2:5446154a4028 | 470 | ///writes the score for the third level to file |
| 200784123 | 2:5446154a4028 | 471 | void scoreWrite3(){ |
| 200784123 | 2:5446154a4028 | 472 | FILE *fp4 = fopen("/local/score3.txt","w");///opens the file score3.txt |
| 200784123 | 2:5446154a4028 | 473 | ///if the file doesn't exist it creates it, if it exists it updates |
| 200784123 | 2:5446154a4028 | 474 | fprintf(fp4,"%i",score3);///prints the integer value for the score to file |
| 200784123 | 2:5446154a4028 | 475 | fclose(fp4); |
| 200784123 | 2:5446154a4028 | 476 | } |
| 200784123 | 3:b78d65300f24 | 477 | ///reads the curently saved score for the third level |
| 200784123 | 2:5446154a4028 | 478 | void scoreRead3(){ |
| 200784123 | 2:5446154a4028 | 479 | int Score3;///tells it that the value to be read is an integer value |
| 200784123 | 2:5446154a4028 | 480 | |
| 200784123 | 2:5446154a4028 | 481 | FILE *fp5 = fopen("/local/score3.txt","r");///opens score3.txt |
| 200784123 | 3:b78d65300f24 | 482 | fscanf(fp5,"%i",&Score3);///searches the file for an integer |
| 200784123 | 2:5446154a4028 | 483 | fclose(fp5); |
| eencae | 0:b85460bc73b9 | 484 | |
| 200784123 | 2:5446154a4028 | 485 | char Points3[14];///bufer to store the points value |
| 200784123 | 2:5446154a4028 | 486 | sprintf(Points3,"%i",Score3);///sends the score to the buffer |
| 200784123 | 2:5446154a4028 | 487 | lcd.printString(Points3,30,3);///prints the score to the LCD |
| 200784123 | 2:5446154a4028 | 488 | } |
| 200784123 | 3:b78d65300f24 | 489 | ///function called to quite the game and return to the menu |
| 200784123 | 3:b78d65300f24 | 490 | ///, has the same effect on the game as running out of lives while playing |
| 200784123 | 2:5446154a4028 | 491 | void pause(){ |
| 200784123 | 2:5446154a4028 | 492 | power=0; |
| 200784123 | 2:5446154a4028 | 493 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 494 | i=1; |
| 200784123 | 2:5446154a4028 | 495 | j=24; |
| 200784123 | 2:5446154a4028 | 496 | winFlag=0; |
| 200784123 | 2:5446154a4028 | 497 | life=0; |
| 200784123 | 2:5446154a4028 | 498 | } |
| 200784123 | 2:5446154a4028 | 499 | ///all of the functions that make up the main game |
| 200784123 | 3:b78d65300f24 | 500 | void mainGame(){ |
| 200784123 | 2:5446154a4028 | 501 | count.attach(&counter,1.0);///counts every second for the lives function etc. |
| 200784123 | 2:5446154a4028 | 502 | flash.attach(&Flash,0.25); |
| 200784123 | 2:5446154a4028 | 503 | push.rise(&pause); |
| 200784123 | 2:5446154a4028 | 504 | butt.rise(&pause); |
| 200784123 | 2:5446154a4028 | 505 | life=3; |
| 200784123 | 2:5446154a4028 | 506 | startFlag=0; |
| 200784123 | 2:5446154a4028 | 507 | winFlag=0; |
| 200784123 | 2:5446154a4028 | 508 | clear(); |
| 200784123 | 2:5446154a4028 | 509 | maze1(); |
| 200784123 | 2:5446154a4028 | 510 | i=1; |
| 200784123 | 2:5446154a4028 | 511 | j=24; |
| 200784123 | 2:5446154a4028 | 512 | while(life&&!winFlag) { |
| 200784123 | 2:5446154a4028 | 513 | power=0; |
| 200784123 | 2:5446154a4028 | 514 | border(); |
| 200784123 | 2:5446154a4028 | 515 | if (printFlag) { |
| 200784123 | 2:5446154a4028 | 516 | printFlag=0; |
| 200784123 | 2:5446154a4028 | 517 | move(); |
| 200784123 | 2:5446154a4028 | 518 | } |
| 200784123 | 2:5446154a4028 | 519 | collide1(); |
| 200784123 | 2:5446154a4028 | 520 | winning(); |
| 200784123 | 2:5446154a4028 | 521 | lives(); |
| 200784123 | 2:5446154a4028 | 522 | if(flashFlag) |
| 200784123 | 2:5446154a4028 | 523 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 524 | if(!flashFlag||joystick.direction!=CENTRE) |
| 200784123 | 2:5446154a4028 | 525 | lcd.setPixel(i,j); |
| 200784123 | 2:5446154a4028 | 526 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 527 | } |
| 200784123 | 2:5446154a4028 | 528 | if(mazeFlag==1){ |
| 200784123 | 2:5446154a4028 | 529 | score1=mazeFlag*life*(100-countFlag);///change this equation to change how the score is set |
| 200784123 | 2:5446154a4028 | 530 | if(screen) |
| 200784123 | 2:5446154a4028 | 531 | score1=score1+50;///change this equation to change how the score is set |
| 200784123 | 2:5446154a4028 | 532 | |
| 200784123 | 2:5446154a4028 | 533 | int Score1;///tells it that the value to be read is an integer value |
| 200784123 | 2:5446154a4028 | 534 | |
| 200784123 | 2:5446154a4028 | 535 | FILE *fp1 = fopen("/local/score1.txt","r");///opens score1.txt |
| 200784123 | 2:5446154a4028 | 536 | fscanf(fp1,"%i",&Score1);///searches the file for an integer followed by a string |
| 200784123 | 2:5446154a4028 | 537 | fclose(fp1); |
| 200784123 | 2:5446154a4028 | 538 | |
| 200784123 | 2:5446154a4028 | 539 | if(score1>Score1) |
| 200784123 | 2:5446154a4028 | 540 | scoreWrite1(); |
| 200784123 | 2:5446154a4028 | 541 | } |
| 200784123 | 2:5446154a4028 | 542 | if(mazeFlag==2){ |
| 200784123 | 2:5446154a4028 | 543 | score2=mazeFlag*life*(100-countFlag);///change this equation to change how the score is set |
| 200784123 | 2:5446154a4028 | 544 | if(screen) |
| 200784123 | 2:5446154a4028 | 545 | score2=score2+50;///change this equation to change how the score is set |
| 200784123 | 2:5446154a4028 | 546 | |
| 200784123 | 2:5446154a4028 | 547 | int Score2;///tells it that the value to be read is an integer value |
| 200784123 | 2:5446154a4028 | 548 | |
| 200784123 | 2:5446154a4028 | 549 | FILE *fp3 = fopen("/local/score2.txt","r");///opens score2.txt |
| 200784123 | 2:5446154a4028 | 550 | fscanf(fp3,"%i",&Score2);///searches the file for an integer |
| 200784123 | 2:5446154a4028 | 551 | fclose(fp3); |
| 200784123 | 2:5446154a4028 | 552 | |
| 200784123 | 2:5446154a4028 | 553 | if(score2>Score2) |
| 200784123 | 2:5446154a4028 | 554 | scoreWrite2(); |
| 200784123 | 2:5446154a4028 | 555 | } |
| 200784123 | 2:5446154a4028 | 556 | if(mazeFlag==3){ |
| 200784123 | 2:5446154a4028 | 557 | score3=mazeFlag*life*(100-countFlag);///change this equation to change how the score is set |
| 200784123 | 2:5446154a4028 | 558 | if(screen) |
| 200784123 | 2:5446154a4028 | 559 | score3=score3+50;///change this equation to change how the score is set |
| 200784123 | 2:5446154a4028 | 560 | |
| 200784123 | 2:5446154a4028 | 561 | int Score3;///tells it that the value to be read is an integer value |
| 200784123 | 2:5446154a4028 | 562 | |
| 200784123 | 2:5446154a4028 | 563 | FILE *fp5 = fopen("/local/score3.txt","r");///opens score3.txt |
| 200784123 | 2:5446154a4028 | 564 | fscanf(fp5,"%i",&Score3);///searches the file for an integer followed by a string |
| 200784123 | 2:5446154a4028 | 565 | fclose(fp5); |
| 200784123 | 2:5446154a4028 | 566 | |
| 200784123 | 2:5446154a4028 | 567 | if(score3>Score3) |
| 200784123 | 2:5446154a4028 | 568 | scoreWrite3(); |
| 200784123 | 2:5446154a4028 | 569 | } |
| 200784123 | 2:5446154a4028 | 570 | clear(); |
| 200784123 | 2:5446154a4028 | 571 | } |
| 200784123 | 2:5446154a4028 | 572 | |
| 200784123 | 3:b78d65300f24 | 573 | ///mini game fashioned after the style of etch-a-sketch |
| 200784123 | 2:5446154a4028 | 574 | void endGame(){ |
| 200784123 | 2:5446154a4028 | 575 | flash.attach(&Flash,0.25); |
| 200784123 | 2:5446154a4028 | 576 | red1=0; |
| 200784123 | 2:5446154a4028 | 577 | red2=0; |
| 200784123 | 2:5446154a4028 | 578 | red3=0; |
| 200784123 | 2:5446154a4028 | 579 | yel=0; |
| 200784123 | 2:5446154a4028 | 580 | buzzer=0; |
| 200784123 | 2:5446154a4028 | 581 | push.rise(&menus); |
| 200784123 | 2:5446154a4028 | 582 | butt.rise(&clear); |
| 200784123 | 2:5446154a4028 | 583 | lcd.clear(); |
| 200784123 | 2:5446154a4028 | 584 | i=42; |
| 200784123 | 2:5446154a4028 | 585 | j=24; |
| 200784123 | 2:5446154a4028 | 586 | while(b) { |
| 200784123 | 2:5446154a4028 | 587 | power=0; |
| 200784123 | 2:5446154a4028 | 588 | endBorder(); |
| 200784123 | 2:5446154a4028 | 589 | lcd.setPixel(i,j); |
| 200784123 | 2:5446154a4028 | 590 | if(flashFlag) |
| 200784123 | 2:5446154a4028 | 591 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 592 | if(!flashFlag||joystick.direction!=CENTRE) |
| 200784123 | 2:5446154a4028 | 593 | lcd.setPixel(i,j); |
| 200784123 | 2:5446154a4028 | 594 | if(printFlag) { |
| 200784123 | 2:5446154a4028 | 595 | printFlag=0; |
| 200784123 | 2:5446154a4028 | 596 | endMove(); |
| 200784123 | 2:5446154a4028 | 597 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 598 | } |
| 200784123 | 2:5446154a4028 | 599 | } |
| 200784123 | 2:5446154a4028 | 600 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 601 | clear(); |
| 200784123 | 2:5446154a4028 | 602 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 603 | i=1; |
| 200784123 | 2:5446154a4028 | 604 | j=24; |
| 200784123 | 2:5446154a4028 | 605 | } |
| 200784123 | 2:5446154a4028 | 606 | |
| 200784123 | 2:5446154a4028 | 607 | ///function called to clear the screen in the mini game if the external button is pressed |
| 200784123 | 2:5446154a4028 | 608 | ///and anywhere else its called |
| 200784123 | 2:5446154a4028 | 609 | void clear(){ |
| 200784123 | 2:5446154a4028 | 610 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 611 | lcd.clear(); |
| 200784123 | 2:5446154a4028 | 612 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 613 | } |
| 200784123 | 2:5446154a4028 | 614 | |
| 200784123 | 2:5446154a4028 | 615 | ///function linked to the flash ticker |
| 200784123 | 2:5446154a4028 | 616 | ///toggles the flag between 0 and 1 and sets the pixel to flash |
| 200784123 | 2:5446154a4028 | 617 | void Flash(){ |
| 200784123 | 2:5446154a4028 | 618 | flashFlag=!flashFlag; |
| 200784123 | 2:5446154a4028 | 619 | lcd.setPixel(i,j); |
| 200784123 | 2:5446154a4028 | 620 | } |
| 200784123 | 2:5446154a4028 | 621 | |
| 200784123 | 2:5446154a4028 | 622 | ///function called to check if the goal area has been reached in the time |
| 200784123 | 2:5446154a4028 | 623 | ///if not then the player is sent back to the begining of the level |
| 200784123 | 2:5446154a4028 | 624 | ///also indicates when time is running out on the buzzer and yellow LED |
| 200784123 | 2:5446154a4028 | 625 | ///added a safe zone at the start of the maze so the counter doesn't start straight away |
| 200784123 | 2:5446154a4028 | 626 | ///being in the safe zone stops the counter and flashes the yellow LED |
| 200784123 | 2:5446154a4028 | 627 | void counter(){ |
| 200784123 | 2:5446154a4028 | 628 | buzzer=0; |
| 200784123 | 2:5446154a4028 | 629 | if((i==0||i==1||i==2||i==3)){ |
| 200784123 | 2:5446154a4028 | 630 | yel=!yel; |
| 200784123 | 2:5446154a4028 | 631 | countFlag=0; |
| 200784123 | 2:5446154a4028 | 632 | } |
| 200784123 | 2:5446154a4028 | 633 | if(!(i==0||i==1||i==2||i==3)) |
| 200784123 | 2:5446154a4028 | 634 | yel=0; |
| 200784123 | 2:5446154a4028 | 635 | if(startFlag){ |
| 200784123 | 2:5446154a4028 | 636 | red1=0; |
| 200784123 | 2:5446154a4028 | 637 | red2=0; |
| 200784123 | 2:5446154a4028 | 638 | red3=0; |
| 200784123 | 2:5446154a4028 | 639 | yel=0; |
| 200784123 | 2:5446154a4028 | 640 | } |
| 200784123 | 2:5446154a4028 | 641 | /////the possition of the safe zone |
| 200784123 | 2:5446154a4028 | 642 | if(!startFlag&&!(i==0||i==1||i==2||i==3)) |
| 200784123 | 2:5446154a4028 | 643 | countFlag++; |
| 200784123 | 2:5446154a4028 | 644 | /////if the current pixel is in the safe zone |
| 200784123 | 2:5446154a4028 | 645 | if(mazeFlag==3){ |
| 200784123 | 2:5446154a4028 | 646 | if(!winFlag&&(countFlag==37||countFlag==38||countFlag==39)) {///change these numbers to change the warning time before the time limit |
| 200784123 | 2:5446154a4028 | 647 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 648 | buzzer=0.9; |
| 200784123 | 2:5446154a4028 | 649 | } |
| 200784123 | 2:5446154a4028 | 650 | if(countFlag==40) {///change this number to change the time limi for the level |
| 200784123 | 2:5446154a4028 | 651 | countFlag=0; |
| 200784123 | 2:5446154a4028 | 652 | yel=0; |
| 200784123 | 2:5446154a4028 | 653 | buzzer=0; |
| 200784123 | 2:5446154a4028 | 654 | life--; |
| 200784123 | 2:5446154a4028 | 655 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 656 | if(!winFlag) { |
| 200784123 | 2:5446154a4028 | 657 | i=1; |
| 200784123 | 2:5446154a4028 | 658 | j=24; |
| 200784123 | 2:5446154a4028 | 659 | } |
| 200784123 | 2:5446154a4028 | 660 | } |
| 200784123 | 2:5446154a4028 | 661 | } |
| 200784123 | 2:5446154a4028 | 662 | if(mazeFlag==2){ |
| 200784123 | 2:5446154a4028 | 663 | if(!winFlag&&(countFlag==42||countFlag==43||countFlag==44)) {///change these numbers to change the warning time before the time limit |
| 200784123 | 2:5446154a4028 | 664 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 665 | buzzer=0.9; |
| 200784123 | 2:5446154a4028 | 666 | } |
| 200784123 | 2:5446154a4028 | 667 | if(countFlag==45) {///change this number to change the time limi for the level |
| 200784123 | 2:5446154a4028 | 668 | countFlag=0; |
| 200784123 | 2:5446154a4028 | 669 | yel=0; |
| 200784123 | 2:5446154a4028 | 670 | buzzer=0; |
| 200784123 | 2:5446154a4028 | 671 | life--; |
| 200784123 | 2:5446154a4028 | 672 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 673 | if(!winFlag) { |
| 200784123 | 2:5446154a4028 | 674 | i=1; |
| 200784123 | 2:5446154a4028 | 675 | j=24; |
| 200784123 | 2:5446154a4028 | 676 | } |
| 200784123 | 2:5446154a4028 | 677 | } |
| 200784123 | 2:5446154a4028 | 678 | } |
| 200784123 | 2:5446154a4028 | 679 | if(mazeFlag==1){ |
| 200784123 | 2:5446154a4028 | 680 | if(!winFlag&&(countFlag==27||countFlag==28||countFlag==29)) {///change these numbers to change the warning time before the time limit |
| 200784123 | 2:5446154a4028 | 681 | yel=0.1*bright; |
| 200784123 | 2:5446154a4028 | 682 | buzzer=0.9; |
| 200784123 | 2:5446154a4028 | 683 | } |
| 200784123 | 2:5446154a4028 | 684 | if(countFlag==30) {///change this number to change the time limi for the level |
| 200784123 | 2:5446154a4028 | 685 | countFlag=0; |
| 200784123 | 2:5446154a4028 | 686 | yel=0; |
| 200784123 | 2:5446154a4028 | 687 | buzzer=0; |
| 200784123 | 2:5446154a4028 | 688 | life--; |
| 200784123 | 2:5446154a4028 | 689 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 690 | if(!winFlag) { |
| 200784123 | 2:5446154a4028 | 691 | i=1; |
| 200784123 | 2:5446154a4028 | 692 | j=24; |
| 200784123 | 2:5446154a4028 | 693 | } |
| 200784123 | 2:5446154a4028 | 694 | } |
| 200784123 | 2:5446154a4028 | 695 | } |
| 200784123 | 2:5446154a4028 | 696 | } |
| 200784123 | 2:5446154a4028 | 697 | |
| 200784123 | 2:5446154a4028 | 698 | ///displays the number of remaining level attempts, or lives, on the red LEDS |
| 200784123 | 2:5446154a4028 | 699 | void lives(){ |
| 200784123 | 2:5446154a4028 | 700 | if(life==3) { |
| 200784123 | 2:5446154a4028 | 701 | red1=0.1*bright; |
| 200784123 | 2:5446154a4028 | 702 | red2=0.1*bright; |
| 200784123 | 2:5446154a4028 | 703 | red3=0.1*bright; |
| 200784123 | 2:5446154a4028 | 704 | } else if(life==2) { |
| 200784123 | 2:5446154a4028 | 705 | red1=0.0; |
| 200784123 | 2:5446154a4028 | 706 | red2=0.1*bright; |
| 200784123 | 2:5446154a4028 | 707 | red3=0.1*bright; |
| 200784123 | 2:5446154a4028 | 708 | } else if(life==1) { |
| 200784123 | 2:5446154a4028 | 709 | red1=0.0; |
| 200784123 | 2:5446154a4028 | 710 | red2=0.0; |
| 200784123 | 2:5446154a4028 | 711 | red3=0.1*bright; |
| 200784123 | 2:5446154a4028 | 712 | } else { |
| 200784123 | 2:5446154a4028 | 713 | red1=0.0; |
| 200784123 | 2:5446154a4028 | 714 | red2=0.0; |
| 200784123 | 2:5446154a4028 | 715 | red3=0.0; |
| 200784123 | 2:5446154a4028 | 716 | start(); |
| 200784123 | 2:5446154a4028 | 717 | } |
| 200784123 | 2:5446154a4028 | 718 | } |
| 200784123 | 2:5446154a4028 | 719 | |
| 200784123 | 2:5446154a4028 | 720 | ///function that checks if the goal area has been reached |
| 200784123 | 2:5446154a4028 | 721 | void winning(){ |
| 200784123 | 3:b78d65300f24 | 722 | if(j==0){///if the pixel reaches the top top of the screen |
| 200784123 | 2:5446154a4028 | 723 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 724 | i=1; |
| 200784123 | 2:5446154a4028 | 725 | j=24; |
| 200784123 | 2:5446154a4028 | 726 | winFlag=1; |
| 200784123 | 2:5446154a4028 | 727 | } |
| 200784123 | 2:5446154a4028 | 728 | } |
| 200784123 | 2:5446154a4028 | 729 | |
| 200784123 | 2:5446154a4028 | 730 | ///displays the maze by opening a picture and setting the pixels accordingly |
| 200784123 | 2:5446154a4028 | 731 | void maze1(){ |
| 200784123 | 2:5446154a4028 | 732 | for (int x = 0; x <= ny-1; x++) { |
| 200784123 | 2:5446154a4028 | 733 | if(mazeFlag==1){ |
| 200784123 | 2:5446154a4028 | 734 | img = fopen("/local/TEST.BMP", "r");///TEST.BMP, 3x3 walls, 5x5 corridoors, if the corners are solid then the player dies when they go into one beacause the number of neighbouring pixels is the same as a collision |
| 200784123 | 2:5446154a4028 | 735 | } |
| 200784123 | 2:5446154a4028 | 736 | if(mazeFlag==2){ |
| 200784123 | 2:5446154a4028 | 737 | img = fopen("/local/MAZE1.BMP", "r");///MAZE1.BMP, 3x3 walls, 4x4 corridoors, if the corners are solid then the player dies when they go into one beacause the number of neighbouring pixels is the same as a collision |
| 200784123 | 2:5446154a4028 | 738 | } |
| 200784123 | 2:5446154a4028 | 739 | if(mazeFlag==3){ |
| 200784123 | 2:5446154a4028 | 740 | img = fopen("/local/MAZE2.BMP", "r");////MAZE2.BMP, 3x3 walls, 3x3 corridorrs, if the corners are solid then the player dies when they go into one beacause the number of neighbouring pixels is the same as a collision |
| 200784123 | 2:5446154a4028 | 741 | } |
| 200784123 | 2:5446154a4028 | 742 | fseek (img , 54 + (nx*x*3) , SEEK_SET); |
| 200784123 | 2:5446154a4028 | 743 | fread(imgbuffer, (nx*3), 1, img); |
| 200784123 | 2:5446154a4028 | 744 | fclose(img); |
| 200784123 | 2:5446154a4028 | 745 | for (int y = 0; y <= nx-1; y++) { |
| 200784123 | 2:5446154a4028 | 746 | int red = imgbuffer[y*3]; |
| 200784123 | 2:5446154a4028 | 747 | if (red == 0) { |
| 200784123 | 2:5446154a4028 | 748 | lcd.setPixel(y,(ny-1)-x); |
| 200784123 | 2:5446154a4028 | 749 | } |
| 200784123 | 2:5446154a4028 | 750 | } |
| 200784123 | 2:5446154a4028 | 751 | } |
| 200784123 | 2:5446154a4028 | 752 | lcd.refresh(); |
| 200784123 | 2:5446154a4028 | 753 | } |
| 200784123 | 2:5446154a4028 | 754 | |
| 200784123 | 2:5446154a4028 | 755 | |
| 200784123 | 2:5446154a4028 | 756 | ///hardwall boundary so the player can't go off the screen |
| 200784123 | 2:5446154a4028 | 757 | void border(){ |
| 200784123 | 2:5446154a4028 | 758 | if (i>=nx) |
| 200784123 | 2:5446154a4028 | 759 | i=nx-1; |
| 200784123 | 2:5446154a4028 | 760 | if (i<=0) |
| 200784123 | 2:5446154a4028 | 761 | i=0; |
| 200784123 | 2:5446154a4028 | 762 | if (j>=ny) |
| 200784123 | 2:5446154a4028 | 763 | j=ny-1; |
| 200784123 | 2:5446154a4028 | 764 | if (j<=0) |
| 200784123 | 2:5446154a4028 | 765 | j=0; |
| 200784123 | 2:5446154a4028 | 766 | } |
| 200784123 | 2:5446154a4028 | 767 | |
| 200784123 | 2:5446154a4028 | 768 | ///portal boundary, so the player can go from one side to another or top to bottom, used in the mini game only |
| 200784123 | 2:5446154a4028 | 769 | void endBorder(){ |
| 200784123 | 2:5446154a4028 | 770 | if (i>=nx) |
| 200784123 | 2:5446154a4028 | 771 | i=0; |
| 200784123 | 2:5446154a4028 | 772 | if (i<0) |
| 200784123 | 2:5446154a4028 | 773 | i=nx-1; |
| 200784123 | 2:5446154a4028 | 774 | if (j>=ny) |
| 200784123 | 2:5446154a4028 | 775 | j=0; |
| 200784123 | 2:5446154a4028 | 776 | if (j<0) |
| 200784123 | 2:5446154a4028 | 777 | j=ny-1; |
| 200784123 | 2:5446154a4028 | 778 | } |
| 200784123 | 3:b78d65300f24 | 779 | ///function called to check if the player is trying to move through a solid object |
| 200784123 | 2:5446154a4028 | 780 | void collide1(){ |
| 200784123 | 2:5446154a4028 | 781 | int block=0; |
| 200784123 | 2:5446154a4028 | 782 | if(joystick.direction==UP){ |
| 200784123 | 2:5446154a4028 | 783 | if(lcd.getPixel(i,j)&&lcd.getPixel(i,j-1)){ |
| 200784123 | 2:5446154a4028 | 784 | block++; |
| 200784123 | 2:5446154a4028 | 785 | } |
| 200784123 | 2:5446154a4028 | 786 | if(lcd.getPixel(i+1,j)||lcd.getPixel(i-1,j)){ |
| 200784123 | 2:5446154a4028 | 787 | block++; |
| 200784123 | 2:5446154a4028 | 788 | } |
| 200784123 | 2:5446154a4028 | 789 | } |
| 200784123 | 2:5446154a4028 | 790 | if(joystick.direction==DOWN){ |
| 200784123 | 2:5446154a4028 | 791 | if(lcd.getPixel(i,j)&&lcd.getPixel(i,j+1)){ |
| 200784123 | 2:5446154a4028 | 792 | block++; |
| 200784123 | 2:5446154a4028 | 793 | } |
| 200784123 | 2:5446154a4028 | 794 | if(lcd.getPixel(i+1,j)||lcd.getPixel(i-1,j)){ |
| 200784123 | 2:5446154a4028 | 795 | block++; |
| 200784123 | 2:5446154a4028 | 796 | } |
| 200784123 | 2:5446154a4028 | 797 | } |
| 200784123 | 2:5446154a4028 | 798 | if(joystick.direction==LEFT){ |
| 200784123 | 2:5446154a4028 | 799 | if(lcd.getPixel(i,j)&&lcd.getPixel(i-1,j)){ |
| 200784123 | 2:5446154a4028 | 800 | block++; |
| 200784123 | 2:5446154a4028 | 801 | } |
| 200784123 | 2:5446154a4028 | 802 | if(lcd.getPixel(i,j+1)||lcd.getPixel(i,j-1)){ |
| 200784123 | 2:5446154a4028 | 803 | block++; |
| 200784123 | 2:5446154a4028 | 804 | } |
| 200784123 | 2:5446154a4028 | 805 | } |
| 200784123 | 2:5446154a4028 | 806 | if(joystick.direction==RIGHT){ |
| 200784123 | 2:5446154a4028 | 807 | if(lcd.getPixel(i,j)&&lcd.getPixel(i+1,j)){ |
| 200784123 | 2:5446154a4028 | 808 | block++; |
| 200784123 | 2:5446154a4028 | 809 | } |
| 200784123 | 2:5446154a4028 | 810 | if(lcd.getPixel(i,j+1)||lcd.getPixel(i,j-1)){ |
| 200784123 | 2:5446154a4028 | 811 | block++; |
| 200784123 | 2:5446154a4028 | 812 | } |
| 200784123 | 2:5446154a4028 | 813 | } |
| 200784123 | 2:5446154a4028 | 814 | if(block==2){ |
| 200784123 | 2:5446154a4028 | 815 | if(!(i==0||i==1||i==2||i==3)){///outside the safe zone |
| 200784123 | 2:5446154a4028 | 816 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 817 | clear(); |
| 200784123 | 2:5446154a4028 | 818 | maze1(); |
| 200784123 | 2:5446154a4028 | 819 | life--; |
| 200784123 | 2:5446154a4028 | 820 | i=1; |
| 200784123 | 2:5446154a4028 | 821 | j=24; |
| 200784123 | 2:5446154a4028 | 822 | countFlag=0; |
| 200784123 | 2:5446154a4028 | 823 | }else{ |
| 200784123 | 2:5446154a4028 | 824 | lcd.clearPixel(i,j); |
| 200784123 | 2:5446154a4028 | 825 | clear(); |
| 200784123 | 2:5446154a4028 | 826 | maze1(); |
| 200784123 | 2:5446154a4028 | 827 | i=1; |
| 200784123 | 2:5446154a4028 | 828 | j=24; |
| 200784123 | 2:5446154a4028 | 829 | countFlag=0; |
| 200784123 | 2:5446154a4028 | 830 | } |
| 200784123 | 2:5446154a4028 | 831 | } |
| 200784123 | 2:5446154a4028 | 832 | } |
| 200784123 | 2:5446154a4028 | 833 | |
| 200784123 | 2:5446154a4028 | 834 | ///checks the joystick direction and alters the possition of the pixel accordingly while clearing off where the pixel just was |
| 200784123 | 2:5446154a4028 | 835 | ///used in the main game only |
| 200784123 | 2:5446154a4028 | 836 | void move(){ |
| 200784123 | 2:5446154a4028 | 837 | if (joystick.direction == DOWN) { |
| 200784123 | 2:5446154a4028 | 838 | j++; |
| 200784123 | 2:5446154a4028 | 839 | lcd.clearPixel(i,j-1); |
| 200784123 | 2:5446154a4028 | 840 | } |
| 200784123 | 2:5446154a4028 | 841 | if (joystick.direction == UP) { |
| 200784123 | 2:5446154a4028 | 842 | j--; |
| 200784123 | 2:5446154a4028 | 843 | lcd.clearPixel(i,j+1); |
| 200784123 | 2:5446154a4028 | 844 | } |
| 200784123 | 2:5446154a4028 | 845 | if (joystick.direction == LEFT) { |
| 200784123 | 2:5446154a4028 | 846 | i--; |
| 200784123 | 2:5446154a4028 | 847 | lcd.clearPixel(i+1,j); |
| 200784123 | 2:5446154a4028 | 848 | } |
| 200784123 | 2:5446154a4028 | 849 | if (joystick.direction == RIGHT) { |
| 200784123 | 2:5446154a4028 | 850 | i++; |
| 200784123 | 2:5446154a4028 | 851 | lcd.clearPixel(i-1,j); |
| 200784123 | 2:5446154a4028 | 852 | } |
| 200784123 | 2:5446154a4028 | 853 | } |
| 200784123 | 2:5446154a4028 | 854 | |
| 200784123 | 2:5446154a4028 | 855 | ///checks the direction of the joystick and updates the position of the pixel accordingly |
| 200784123 | 2:5446154a4028 | 856 | ///keeps the path drawn unless cleared by the button, see clear() |
| 200784123 | 2:5446154a4028 | 857 | void endMove(){ |
| 200784123 | 2:5446154a4028 | 858 | if (joystick.direction == DOWN) { |
| 200784123 | 2:5446154a4028 | 859 | j++; |
| 200784123 | 2:5446154a4028 | 860 | } |
| 200784123 | 2:5446154a4028 | 861 | if (joystick.direction == UP) { |
| 200784123 | 2:5446154a4028 | 862 | j--; |
| 200784123 | 2:5446154a4028 | 863 | } |
| 200784123 | 2:5446154a4028 | 864 | if (joystick.direction == LEFT) { |
| 200784123 | 2:5446154a4028 | 865 | i--; |
| 200784123 | 2:5446154a4028 | 866 | } |
| 200784123 | 2:5446154a4028 | 867 | if (joystick.direction == RIGHT) { |
| 200784123 | 2:5446154a4028 | 868 | i++; |
| 200784123 | 2:5446154a4028 | 869 | } |
| 200784123 | 2:5446154a4028 | 870 | } |
| 200784123 | 2:5446154a4028 | 871 | |
| 200784123 | 2:5446154a4028 | 872 | /* Joystick |
| 200784123 | 2:5446154a4028 | 873 | Example code of how to read a joystick |
| 200784123 | 2:5446154a4028 | 874 | |
| 200784123 | 2:5446154a4028 | 875 | https://www.sparkfun.com/products/9032 |
| 200784123 | 2:5446154a4028 | 876 | |
| 200784123 | 2:5446154a4028 | 877 | Craig A. Evans |
| 200784123 | 2:5446154a4028 | 878 | 7 March 2015 |
| 200784123 | 2:5446154a4028 | 879 | */ |
| 200784123 | 2:5446154a4028 | 880 | |
| 200784123 | 2:5446154a4028 | 881 | /// read default positions of the joystick to calibrate later readings |
| 200784123 | 2:5446154a4028 | 882 | void calibrateJoystick(){ |
| 200784123 | 2:5446154a4028 | 883 | button.mode(PullDown); |
| 200784123 | 2:5446154a4028 | 884 | ///// must not move during calibration |
| 200784123 | 2:5446154a4028 | 885 | joystick.x0 = xPot; /// initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
| 200784123 | 2:5446154a4028 | 886 | joystick.y0 = yPot; |
| 200784123 | 2:5446154a4028 | 887 | } |
| 200784123 | 3:b78d65300f24 | 888 | /// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
| 200784123 | 2:5446154a4028 | 889 | void updateJoystick(){ |
| 200784123 | 2:5446154a4028 | 890 | joystick.x = xPot - joystick.x0; |
| 200784123 | 2:5446154a4028 | 891 | joystick.y = yPot - joystick.y0; |
| 200784123 | 2:5446154a4028 | 892 | /// read button state |
| 200784123 | 2:5446154a4028 | 893 | joystick.button = button; |
| 200784123 | 3:b78d65300f24 | 894 | |
| 200784123 | 2:5446154a4028 | 895 | /// calculate direction depending on x,y values |
| 200784123 | 2:5446154a4028 | 896 | /// tolerance allows a little lee-way in case joystick not exactly in the stated direction |
| 200784123 | 2:5446154a4028 | 897 | |
| 200784123 | 2:5446154a4028 | 898 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| 200784123 | 2:5446154a4028 | 899 | joystick.direction = CENTRE; |
| 200784123 | 2:5446154a4028 | 900 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| 200784123 | 2:5446154a4028 | 901 | joystick.direction = DOWN; |
| 200784123 | 2:5446154a4028 | 902 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
| 200784123 | 2:5446154a4028 | 903 | joystick.direction = UP; |
| 200784123 | 2:5446154a4028 | 904 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| 200784123 | 2:5446154a4028 | 905 | joystick.direction = RIGHT; |
| 200784123 | 2:5446154a4028 | 906 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
| 200784123 | 2:5446154a4028 | 907 | joystick.direction = LEFT; |
| 200784123 | 2:5446154a4028 | 908 | } |
| 200784123 | 2:5446154a4028 | 909 | /// set flag for printing |
| 200784123 | 2:5446154a4028 | 910 | printFlag = 1; |
| eencae | 0:b85460bc73b9 | 911 | } |
