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.
main.cpp@9:c8ea9b4af873, 2015-05-10 (annotated)
- Committer:
- el14rd
- Date:
- Sun May 10 00:22:50 2015 +0000
- Revision:
- 9:c8ea9b4af873
- Parent:
- 8:b01f638d23b4
- Child:
- 10:67e13dfaa10d
complete version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:026fa541af7a | 1 | #include "mbed.h" |
el14rd | 1:f682aeb462f1 | 2 | #include "N5110.h" |
eencae | 0:026fa541af7a | 3 | #define DIRECTION_TOLERANCE 0.05 |
eencae | 0:026fa541af7a | 4 | |
el14rd | 1:f682aeb462f1 | 5 | |
el14rd | 1:f682aeb462f1 | 6 | //pins in |
el14rd | 1:f682aeb462f1 | 7 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
el14rd | 8:b01f638d23b4 | 8 | InterruptIn button(p17); |
el14rd | 8:b01f638d23b4 | 9 | DigitalIn buttonA(p19); |
el14rd | 9:c8ea9b4af873 | 10 | AnalogIn pot(p20); |
el14rd | 9:c8ea9b4af873 | 11 | AnalogIn xPot(p15); // connections for joystick |
el14rd | 1:f682aeb462f1 | 12 | AnalogIn yPot(p16); |
el14rd | 4:41acda9d68c7 | 13 | PwmOut buzzer(p21); |
el14rd | 4:41acda9d68c7 | 14 | PwmOut blacklight(p26); |
el14rd | 4:41acda9d68c7 | 15 | PwmOut led1(p24); |
el14rd | 4:41acda9d68c7 | 16 | PwmOut led2(p23); |
el14rd | 8:b01f638d23b4 | 17 | BusOut leds(LED4,LED3,LED2,LED1); |
el14rd | 4:41acda9d68c7 | 18 | |
el14rd | 9:c8ea9b4af873 | 19 | float ain; |
el14rd | 1:f682aeb462f1 | 20 | int i; |
el14rd | 1:f682aeb462f1 | 21 | int j; |
el14rd | 2:d7b17623ba26 | 22 | int X; |
el14rd | 2:d7b17623ba26 | 23 | int Y; |
el14rd | 5:d8a06e7c54fb | 24 | char buffer[14]; |
el14rd | 1:f682aeb462f1 | 25 | |
el14rd | 1:f682aeb462f1 | 26 | int snakeX[100]; |
el14rd | 1:f682aeb462f1 | 27 | int snakeY[100]; |
eencae | 0:026fa541af7a | 28 | |
eencae | 0:026fa541af7a | 29 | // timer to regularly read the joystick |
eencae | 0:026fa541af7a | 30 | Ticker pollJoystick; |
eencae | 0:026fa541af7a | 31 | // Serial for debug |
eencae | 0:026fa541af7a | 32 | Serial serial(USBTX,USBRX); |
eencae | 0:026fa541af7a | 33 | |
eencae | 0:026fa541af7a | 34 | // create enumerated type (0,1,2,3 etc. for direction) |
eencae | 0:026fa541af7a | 35 | // could be extended for diagonals etc. |
eencae | 0:026fa541af7a | 36 | enum DirectionName { |
eencae | 0:026fa541af7a | 37 | UP, |
eencae | 0:026fa541af7a | 38 | DOWN, |
eencae | 0:026fa541af7a | 39 | LEFT, |
eencae | 0:026fa541af7a | 40 | RIGHT, |
eencae | 0:026fa541af7a | 41 | CENTRE, |
eencae | 0:026fa541af7a | 42 | UNKNOWN |
eencae | 0:026fa541af7a | 43 | }; |
eencae | 0:026fa541af7a | 44 | |
eencae | 0:026fa541af7a | 45 | // struct for Joystick |
eencae | 0:026fa541af7a | 46 | typedef struct JoyStick Joystick; |
eencae | 0:026fa541af7a | 47 | struct JoyStick { |
eencae | 0:026fa541af7a | 48 | float x; // current x value |
eencae | 0:026fa541af7a | 49 | float x0; // 'centred' x value |
eencae | 0:026fa541af7a | 50 | float y; // current y value |
eencae | 0:026fa541af7a | 51 | float y0; // 'centred' y value |
eencae | 0:026fa541af7a | 52 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
eencae | 0:026fa541af7a | 53 | DirectionName direction; // current direction |
eencae | 0:026fa541af7a | 54 | }; |
eencae | 0:026fa541af7a | 55 | // create struct variable |
eencae | 0:026fa541af7a | 56 | Joystick joystick; |
eencae | 0:026fa541af7a | 57 | |
el14rd | 8:b01f638d23b4 | 58 | //int buttonA = 0; |
el14rd | 8:b01f638d23b4 | 59 | int pause = 0; |
el14rd | 8:b01f638d23b4 | 60 | int buttonAFlag=0; |
el14rd | 7:69ae0585d11a | 61 | int buttonFlag = 0; |
eencae | 0:026fa541af7a | 62 | int printFlag = 0; |
el14rd | 3:04ff8cd27dd1 | 63 | int nutFlag =1; |
el14rd | 5:d8a06e7c54fb | 64 | int lifeFlag =0; |
el14rd | 8:b01f638d23b4 | 65 | float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; |
el14rd | 8:b01f638d23b4 | 66 | float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; |
eencae | 0:026fa541af7a | 67 | // function prototypes |
el14rd | 1:f682aeb462f1 | 68 | //void setPixel(int x, int y); |
el14rd | 1:f682aeb462f1 | 69 | //void clearPixel(int x, int y); |
el14rd | 1:f682aeb462f1 | 70 | |
eencae | 0:026fa541af7a | 71 | |
el14rd | 1:f682aeb462f1 | 72 | typedef struct Nut Nut; |
el14rd | 1:f682aeb462f1 | 73 | struct Nut { |
el14rd | 1:f682aeb462f1 | 74 | int X; |
el14rd | 1:f682aeb462f1 | 75 | int Y; |
el14rd | 1:f682aeb462f1 | 76 | }; |
el14rd | 1:f682aeb462f1 | 77 | Nut nut; |
eencae | 0:026fa541af7a | 78 | |
el14rd | 1:f682aeb462f1 | 79 | typedef struct Snake Snake; |
el14rd | 1:f682aeb462f1 | 80 | struct Snake { |
el14rd | 1:f682aeb462f1 | 81 | int snakeX[100]; |
el14rd | 1:f682aeb462f1 | 82 | int snakeY[100]; |
el14rd | 1:f682aeb462f1 | 83 | int node; |
el14rd | 1:f682aeb462f1 | 84 | DirectionName direction; |
el14rd | 1:f682aeb462f1 | 85 | }; |
el14rd | 1:f682aeb462f1 | 86 | Snake snake; |
eencae | 0:026fa541af7a | 87 | |
el14rd | 1:f682aeb462f1 | 88 | DirectionName snakeDirection; |
el14rd | 1:f682aeb462f1 | 89 | |
el14rd | 2:d7b17623ba26 | 90 | void initNut() |
el14rd | 2:d7b17623ba26 | 91 | { |
el14rd | 3:04ff8cd27dd1 | 92 | nut.X=rand()%84; |
el14rd | 3:04ff8cd27dd1 | 93 | nut.Y=rand()%48; |
el14rd | 3:04ff8cd27dd1 | 94 | //nutFlag=1; |
el14rd | 8:b01f638d23b4 | 95 | } |
el14rd | 8:b01f638d23b4 | 96 | |
el14rd | 2:d7b17623ba26 | 97 | void drawNut() |
el14rd | 2:d7b17623ba26 | 98 | { |
el14rd | 3:04ff8cd27dd1 | 99 | //int X = rand()%84; |
el14rd | 3:04ff8cd27dd1 | 100 | //int Y = rand()%48; |
el14rd | 8:b01f638d23b4 | 101 | if(nutFlag==1) { |
el14rd | 8:b01f638d23b4 | 102 | lcd.drawCircle(nut.X,nut.Y,1,1); |
el14rd | 3:04ff8cd27dd1 | 103 | } |
el14rd | 2:d7b17623ba26 | 104 | } |
eencae | 0:026fa541af7a | 105 | |
el14rd | 1:f682aeb462f1 | 106 | void moveSnake() |
el14rd | 1:f682aeb462f1 | 107 | { |
el14rd | 8:b01f638d23b4 | 108 | |
el14rd | 8:b01f638d23b4 | 109 | if(joystick.direction == CENTRE) { |
el14rd | 4:41acda9d68c7 | 110 | //snake.snakeX[0]+=1; |
el14rd | 4:41acda9d68c7 | 111 | joystick.direction = CENTRE; |
el14rd | 3:04ff8cd27dd1 | 112 | } |
el14rd | 4:41acda9d68c7 | 113 | //else if(joystick.button == button){ |
el14rd | 8:b01f638d23b4 | 114 | // snake.snakeX[i]=snake.snakeX[i]; |
el14rd | 8:b01f638d23b4 | 115 | // snake.snakeY[i]=snake.snakeY[i]; |
el14rd | 8:b01f638d23b4 | 116 | // } |
el14rd | 8:b01f638d23b4 | 117 | else if(joystick.direction == UNKNOWN) { |
el14rd | 8:b01f638d23b4 | 118 | joystick.direction = UNKNOWN; |
el14rd | 8:b01f638d23b4 | 119 | } else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { |
el14rd | 2:d7b17623ba26 | 120 | |
el14rd | 4:41acda9d68c7 | 121 | if((joystick.direction == DOWN)&&(snake.direction!=LEFT)) { |
el14rd | 2:d7b17623ba26 | 122 | snake.direction= RIGHT; |
el14rd | 4:41acda9d68c7 | 123 | //snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 124 | |
el14rd | 4:41acda9d68c7 | 125 | } else if((joystick.direction == UP)&&(snake.direction!=RIGHT)) { |
el14rd | 2:d7b17623ba26 | 126 | snake.direction=LEFT; |
el14rd | 4:41acda9d68c7 | 127 | //snake.snakeX[0]-=1; |
el14rd | 4:41acda9d68c7 | 128 | } else if((joystick.direction == LEFT)&&(snake.direction!=DOWN)) { |
el14rd | 5:d8a06e7c54fb | 129 | snake.direction=UP; |
el14rd | 4:41acda9d68c7 | 130 | //snake.snakeY[0]-=1; |
el14rd | 4:41acda9d68c7 | 131 | } else if((joystick.direction == RIGHT)&&(snake.direction!=UP)) { |
el14rd | 5:d8a06e7c54fb | 132 | snake.direction=DOWN; |
el14rd | 4:41acda9d68c7 | 133 | //snake.snakeY[0]+=1; |
eencae | 0:026fa541af7a | 134 | } |
eencae | 0:026fa541af7a | 135 | |
eencae | 0:026fa541af7a | 136 | } |
el14rd | 8:b01f638d23b4 | 137 | |
el14rd | 2:d7b17623ba26 | 138 | } |
el14rd | 2:d7b17623ba26 | 139 | |
el14rd | 2:d7b17623ba26 | 140 | void initSnake() |
el14rd | 2:d7b17623ba26 | 141 | { |
el14rd | 8:b01f638d23b4 | 142 | |
el14rd | 5:d8a06e7c54fb | 143 | snake.snakeX[0] = 16; //coordinate of head |
el14rd | 2:d7b17623ba26 | 144 | snake.snakeY[0] = 10; |
el14rd | 5:d8a06e7c54fb | 145 | snake.snakeX[1] = 14; |
el14rd | 2:d7b17623ba26 | 146 | snake.snakeY[1] = 10; |
el14rd | 4:41acda9d68c7 | 147 | snake.snakeX[2] = 12; |
el14rd | 2:d7b17623ba26 | 148 | snake.snakeY[2] = 10; |
el14rd | 5:d8a06e7c54fb | 149 | snake.snakeX[3] = 10; |
el14rd | 2:d7b17623ba26 | 150 | snake.snakeY[3] = 10; |
el14rd | 5:d8a06e7c54fb | 151 | snake.node = 6; //node of snake |
el14rd | 4:41acda9d68c7 | 152 | snake.direction = RIGHT; |
el14rd | 8:b01f638d23b4 | 153 | |
el14rd | 5:d8a06e7c54fb | 154 | } |
el14rd | 8:b01f638d23b4 | 155 | |
el14rd | 8:b01f638d23b4 | 156 | |
el14rd | 1:f682aeb462f1 | 157 | void drawSnake() |
el14rd | 8:b01f638d23b4 | 158 | { |
el14rd | 8:b01f638d23b4 | 159 | |
el14rd | 8:b01f638d23b4 | 160 | for(i=0; i<snake.node-1; i++) { |
el14rd | 5:d8a06e7c54fb | 161 | lcd.drawCircle(snake.snakeX[i],snake.snakeY[i],1,1); |
el14rd | 5:d8a06e7c54fb | 162 | //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]); |
el14rd | 8:b01f638d23b4 | 163 | |
el14rd | 8:b01f638d23b4 | 164 | } |
el14rd | 8:b01f638d23b4 | 165 | |
el14rd | 1:f682aeb462f1 | 166 | } |
el14rd | 1:f682aeb462f1 | 167 | |
el14rd | 1:f682aeb462f1 | 168 | void startGmae() |
el14rd | 1:f682aeb462f1 | 169 | { |
el14rd | 8:b01f638d23b4 | 170 | //nutFlag = 1; |
el14rd | 5:d8a06e7c54fb | 171 | //lifeFlag = 0; |
el14rd | 2:d7b17623ba26 | 172 | //snake.direction=LEFT; |
el14rd | 8:b01f638d23b4 | 173 | |
el14rd | 6:290173d4a22d | 174 | if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)) { // if snake got nut ||(snake.snakeX[0]==nut.X-1)&&(snake.snakeY[0]==nut.Y-1)||(snake.snakeY[0]==nut.Y+1)&&(snake.snakeY[0]==nut.Y+1 |
el14rd | 8:b01f638d23b4 | 175 | snake.node+=1; |
el14rd | 8:b01f638d23b4 | 176 | nutFlag=0; // new nut appear |
el14rd | 8:b01f638d23b4 | 177 | buzzer.period(1/frequency[1]); // set PWM period |
el14rd | 8:b01f638d23b4 | 178 | buzzer=0.1; // set duty cycle |
el14rd | 3:04ff8cd27dd1 | 179 | } |
el14rd | 8:b01f638d23b4 | 180 | |
el14rd | 8:b01f638d23b4 | 181 | if((snake.snakeX[0]!=nut.X)||(snake.snakeY[0]!=nut.Y)) { |
el14rd | 8:b01f638d23b4 | 182 | buzzer=0; |
el14rd | 8:b01f638d23b4 | 183 | } |
el14rd | 8:b01f638d23b4 | 184 | |
el14rd | 3:04ff8cd27dd1 | 185 | if(nutFlag==0) { //set nut randomly |
el14rd | 1:f682aeb462f1 | 186 | nut.X = rand()%83; |
el14rd | 1:f682aeb462f1 | 187 | nut.Y = rand()%47; |
el14rd | 3:04ff8cd27dd1 | 188 | nutFlag = 1; |
el14rd | 1:f682aeb462f1 | 189 | } |
el14rd | 2:d7b17623ba26 | 190 | |
el14rd | 8:b01f638d23b4 | 191 | |
el14rd | 8:b01f638d23b4 | 192 | for(i=3; i<snake.node; i++) { //if snake collide itself snake die |
el14rd | 8:b01f638d23b4 | 193 | if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) { |
el14rd | 8:b01f638d23b4 | 194 | lifeFlag=1; |
el14rd | 7:69ae0585d11a | 195 | } |
el14rd | 8:b01f638d23b4 | 196 | } |
el14rd | 8:b01f638d23b4 | 197 | |
el14rd | 8:b01f638d23b4 | 198 | |
el14rd | 8:b01f638d23b4 | 199 | for(i=snake.node-1; i>0; i--) { //move snake |
el14rd | 8:b01f638d23b4 | 200 | if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47) { |
el14rd | 8:b01f638d23b4 | 201 | snake.snakeX[i]=snake.snakeX[i-1]; |
el14rd | 8:b01f638d23b4 | 202 | snake.snakeY[i]=snake.snakeY[i-1]; |
el14rd | 8:b01f638d23b4 | 203 | } else { |
el14rd | 8:b01f638d23b4 | 204 | snake.snakeX[i]=snake.snakeX[i]; |
el14rd | 8:b01f638d23b4 | 205 | snake.snakeY[i]=snake.snakeY[i]; |
el14rd | 8:b01f638d23b4 | 206 | } |
el14rd | 8:b01f638d23b4 | 207 | } |
el14rd | 8:b01f638d23b4 | 208 | |
el14rd | 1:f682aeb462f1 | 209 | |
el14rd | 1:f682aeb462f1 | 210 | if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) { |
el14rd | 5:d8a06e7c54fb | 211 | lifeFlag=1; // snake collide the corrider snake die |
el14rd | 1:f682aeb462f1 | 212 | } |
el14rd | 8:b01f638d23b4 | 213 | |
el14rd | 1:f682aeb462f1 | 214 | } |
el14rd | 1:f682aeb462f1 | 215 | |
el14rd | 8:b01f638d23b4 | 216 | void printScore() |
el14rd | 8:b01f638d23b4 | 217 | { |
el14rd | 6:290173d4a22d | 218 | lcd.printString("Your Score is",0,0); |
el14rd | 6:290173d4a22d | 219 | int score=snake.node-6; |
el14rd | 6:290173d4a22d | 220 | int length = sprintf(buffer," SCORE=%2d ",score); |
el14rd | 8:b01f638d23b4 | 221 | lcd.printString(buffer,0,2 ); |
el14rd | 6:290173d4a22d | 222 | lcd.printString("Press to start",0,4); |
el14rd | 8:b01f638d23b4 | 223 | } |
el14rd | 7:69ae0585d11a | 224 | |
el14rd | 8:b01f638d23b4 | 225 | void buttonPressed() |
el14rd | 8:b01f638d23b4 | 226 | { |
el14rd | 8:b01f638d23b4 | 227 | buttonFlag =1; |
el14rd | 7:69ae0585d11a | 228 | } |
el14rd | 7:69ae0585d11a | 229 | |
el14rd | 8:b01f638d23b4 | 230 | void snakeModel() |
el14rd | 8:b01f638d23b4 | 231 | { |
el14rd | 8:b01f638d23b4 | 232 | for(int i=0; i<25; i+=5) { |
el14rd | 8:b01f638d23b4 | 233 | lcd.clear(); |
el14rd | 8:b01f638d23b4 | 234 | lcd.printString("Press to start",1,1); |
el14rd | 8:b01f638d23b4 | 235 | |
el14rd | 8:b01f638d23b4 | 236 | lcd.drawRect(44-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 237 | lcd.drawRect(46-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 238 | lcd.drawRect(48-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 239 | |
el14rd | 8:b01f638d23b4 | 240 | lcd.drawRect(42-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 241 | lcd.drawRect(44-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 242 | lcd.drawRect(46-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 243 | lcd.drawRect(48-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 244 | |
el14rd | 8:b01f638d23b4 | 245 | lcd.drawRect(40-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 246 | lcd.drawRect(42-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 247 | lcd.drawRect(44-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 248 | lcd.drawRect(46-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 249 | lcd.drawRect(48-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 250 | lcd.drawRect(50-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 251 | |
el14rd | 8:b01f638d23b4 | 252 | lcd.drawRect(40-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 253 | lcd.drawRect(42-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 254 | lcd.drawRect(44-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 255 | lcd.drawRect(46-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 256 | lcd.drawRect(48-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 257 | lcd.drawRect(50-i,27,1,1,1);//head |
el14rd | 8:b01f638d23b4 | 258 | |
el14rd | 8:b01f638d23b4 | 259 | lcd.drawRect(38-i,29,1,1,0); //tougue |
el14rd | 8:b01f638d23b4 | 260 | lcd.drawRect(40-i,31,1,1,0); |
el14rd | 8:b01f638d23b4 | 261 | |
el14rd | 8:b01f638d23b4 | 262 | lcd.drawRect(46-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 263 | lcd.drawRect(48-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 264 | lcd.drawRect(50-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 265 | |
el14rd | 8:b01f638d23b4 | 266 | lcd.drawRect(46-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 267 | lcd.drawRect(48-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 268 | lcd.drawRect(50-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 269 | |
el14rd | 8:b01f638d23b4 | 270 | lcd.drawRect(44-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 271 | lcd.drawRect(46-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 272 | lcd.drawRect(48-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 273 | |
el14rd | 8:b01f638d23b4 | 274 | lcd.drawRect(44-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 275 | lcd.drawRect(46-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 276 | lcd.drawRect(48-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 277 | |
el14rd | 8:b01f638d23b4 | 278 | lcd.drawRect(42-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 279 | lcd.drawRect(44-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 280 | lcd.drawRect(46-i,37,1,1,1); |
el14rd | 6:290173d4a22d | 281 | |
el14rd | 8:b01f638d23b4 | 282 | lcd.drawRect(42-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 283 | lcd.drawRect(44-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 284 | lcd.drawRect(46-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 285 | |
el14rd | 8:b01f638d23b4 | 286 | lcd.drawRect(44-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 287 | lcd.drawRect(46-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 288 | lcd.drawRect(48-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 289 | lcd.drawRect(50-i,41,1,1,1);//neck |
el14rd | 8:b01f638d23b4 | 290 | |
el14rd | 8:b01f638d23b4 | 291 | lcd.drawRect(80-i,41,1,1,1);//tail |
el14rd | 8:b01f638d23b4 | 292 | lcd.drawRect(82-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 293 | lcd.drawRect(84-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 294 | |
el14rd | 8:b01f638d23b4 | 295 | lcd.drawRect(82-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 296 | lcd.drawRect(84-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 297 | |
el14rd | 8:b01f638d23b4 | 298 | lcd.drawRect(82-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 299 | |
el14rd | 8:b01f638d23b4 | 300 | lcd.drawRect(80-i,35,1,1,1);//tail |
el14rd | 8:b01f638d23b4 | 301 | |
el14rd | 8:b01f638d23b4 | 302 | lcd.drawRect(46-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 303 | lcd.drawRect(48-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 304 | lcd.drawRect(50-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 305 | lcd.drawRect(52-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 306 | lcd.drawRect(54-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 307 | lcd.drawRect(56-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 308 | lcd.drawRect(58-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 309 | lcd.drawRect(60-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 310 | lcd.drawRect(62-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 311 | lcd.drawRect(64-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 312 | lcd.drawRect(66-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 313 | lcd.drawRect(68-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 314 | lcd.drawRect(70-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 315 | lcd.drawRect(72-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 316 | lcd.drawRect(74-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 317 | lcd.drawRect(76-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 318 | lcd.drawRect(78-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 319 | lcd.drawRect(80-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 320 | lcd.drawRect(82-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 321 | |
el14rd | 8:b01f638d23b4 | 322 | lcd.drawRect(48-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 323 | lcd.drawRect(50-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 324 | lcd.drawRect(52-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 325 | lcd.drawRect(54-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 326 | lcd.drawRect(56-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 327 | lcd.drawRect(58-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 328 | lcd.drawRect(60-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 329 | lcd.drawRect(62-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 330 | lcd.drawRect(64-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 331 | lcd.drawRect(66-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 332 | lcd.drawRect(68-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 333 | lcd.drawRect(70-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 334 | lcd.drawRect(72-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 335 | lcd.drawRect(74-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 336 | lcd.drawRect(76-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 337 | lcd.drawRect(78-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 338 | lcd.drawRect(80-i,45,1,1,1); |
el14rd | 7:69ae0585d11a | 339 | wait(0.5); |
el14rd | 8:b01f638d23b4 | 340 | //lcd.refresh(); |
el14rd | 7:69ae0585d11a | 341 | } |
el14rd | 8:b01f638d23b4 | 342 | |
el14rd | 8:b01f638d23b4 | 343 | } |
el14rd | 1:f682aeb462f1 | 344 | //switch(snake.direction) |
el14rd | 1:f682aeb462f1 | 345 | //{ |
el14rd | 1:f682aeb462f1 | 346 | // case RIGHT:snake.X[0]+=1; |
el14rd | 1:f682aeb462f1 | 347 | // break; |
el14rd | 1:f682aeb462f1 | 348 | // case LEFT:snake.X[0]-=1; |
el14rd | 1:f682aeb462f1 | 349 | // break; |
el14rd | 1:f682aeb462f1 | 350 | // case UP:snake.Y[0]-=1; |
el14rd | 1:f682aeb462f1 | 351 | // break; |
el14rd | 1:f682aeb462f1 | 352 | // case DOWN:snake.Y[0]+=1; |
el14rd | 1:f682aeb462f1 | 353 | // break; |
el14rd | 1:f682aeb462f1 | 354 | // } |
el14rd | 1:f682aeb462f1 | 355 | |
el14rd | 1:f682aeb462f1 | 356 | //end of while(joystick.direction = UNKNOWN) |
el14rd | 1:f682aeb462f1 | 357 | |
el14rd | 1:f682aeb462f1 | 358 | |
eencae | 0:026fa541af7a | 359 | // read default positions of the joystick to calibrate later readings |
eencae | 0:026fa541af7a | 360 | void calibrateJoystick() |
eencae | 0:026fa541af7a | 361 | { |
eencae | 0:026fa541af7a | 362 | button.mode(PullDown); |
eencae | 0:026fa541af7a | 363 | // must not move during calibration |
eencae | 0:026fa541af7a | 364 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
eencae | 0:026fa541af7a | 365 | joystick.y0 = yPot; |
eencae | 0:026fa541af7a | 366 | } |
el14rd | 1:f682aeb462f1 | 367 | |
el14rd | 2:d7b17623ba26 | 368 | |
eencae | 0:026fa541af7a | 369 | void updateJoystick() |
eencae | 0:026fa541af7a | 370 | { |
el14rd | 9:c8ea9b4af873 | 371 | //led1=1; |
eencae | 0:026fa541af7a | 372 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
eencae | 0:026fa541af7a | 373 | joystick.x = xPot - joystick.x0; |
eencae | 0:026fa541af7a | 374 | joystick.y = yPot - joystick.y0; |
eencae | 0:026fa541af7a | 375 | // read button state |
eencae | 0:026fa541af7a | 376 | joystick.button = button; |
eencae | 0:026fa541af7a | 377 | |
eencae | 0:026fa541af7a | 378 | // calculate direction depending on x,y values |
eencae | 0:026fa541af7a | 379 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
eencae | 0:026fa541af7a | 380 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 381 | joystick.direction = CENTRE; |
eencae | 0:026fa541af7a | 382 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 383 | joystick.direction = UP; |
eencae | 0:026fa541af7a | 384 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 385 | joystick.direction = DOWN; |
el14rd | 4:41acda9d68c7 | 386 | //LIGHT LED2 |
eencae | 0:026fa541af7a | 387 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 388 | joystick.direction = RIGHT; |
el14rd | 4:41acda9d68c7 | 389 | //LIGHT LED3 |
eencae | 0:026fa541af7a | 390 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 391 | joystick.direction = LEFT; |
eencae | 0:026fa541af7a | 392 | } else { |
eencae | 0:026fa541af7a | 393 | joystick.direction = UNKNOWN; |
eencae | 0:026fa541af7a | 394 | } |
eencae | 0:026fa541af7a | 395 | // set flag for printing |
eencae | 0:026fa541af7a | 396 | printFlag = 1; |
el14rd | 1:f682aeb462f1 | 397 | } |
el14rd | 1:f682aeb462f1 | 398 | |
el14rd | 1:f682aeb462f1 | 399 | void updateSnake() |
el14rd | 1:f682aeb462f1 | 400 | { |
el14rd | 1:f682aeb462f1 | 401 | // get direction of joystick and update snake |
el14rd | 4:41acda9d68c7 | 402 | if (snake.direction == LEFT) { |
el14rd | 7:69ae0585d11a | 403 | snake.snakeX[0]-=1; |
el14rd | 4:41acda9d68c7 | 404 | //LED1 |
el14rd | 4:41acda9d68c7 | 405 | if(snake.snakeX[0]<1) { |
el14rd | 4:41acda9d68c7 | 406 | snake.snakeX[0]=1; |
el14rd | 8:b01f638d23b4 | 407 | |
el14rd | 1:f682aeb462f1 | 408 | }//will stop at the top edge |
el14rd | 1:f682aeb462f1 | 409 | |
el14rd | 1:f682aeb462f1 | 410 | } |
el14rd | 4:41acda9d68c7 | 411 | if (snake.direction == RIGHT) { |
el14rd | 7:69ae0585d11a | 412 | snake.snakeX[0]+=1; |
el14rd | 4:41acda9d68c7 | 413 | //LED2 |
el14rd | 4:41acda9d68c7 | 414 | if(snake.snakeX[0]>83) { |
el14rd | 4:41acda9d68c7 | 415 | snake.snakeX[0]=83; |
el14rd | 1:f682aeb462f1 | 416 | }//will stop at the bottom edge |
el14rd | 1:f682aeb462f1 | 417 | } |
el14rd | 4:41acda9d68c7 | 418 | if (snake.direction == UP) { |
el14rd | 7:69ae0585d11a | 419 | snake.snakeY[0]-=1; |
el14rd | 4:41acda9d68c7 | 420 | if(snake.snakeY[0]>47) { |
el14rd | 4:41acda9d68c7 | 421 | snake.snakeY[0]=47; |
el14rd | 1:f682aeb462f1 | 422 | }//will stop at the left edge |
el14rd | 1:f682aeb462f1 | 423 | |
el14rd | 1:f682aeb462f1 | 424 | } |
el14rd | 4:41acda9d68c7 | 425 | if (snake.direction == DOWN) { |
el14rd | 7:69ae0585d11a | 426 | snake.snakeY[0]+=1; |
el14rd | 4:41acda9d68c7 | 427 | if(snake.snakeY[0]<1) { |
el14rd | 4:41acda9d68c7 | 428 | snake.snakeY[0]=1; |
el14rd | 1:f682aeb462f1 | 429 | }//will stop at the right edge |
el14rd | 1:f682aeb462f1 | 430 | |
el14rd | 1:f682aeb462f1 | 431 | } |
el14rd | 8:b01f638d23b4 | 432 | if(snake.direction == CENTRE) { |
el14rd | 4:41acda9d68c7 | 433 | snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 434 | } |
el14rd | 8:b01f638d23b4 | 435 | if(snake.direction == UNKNOWN) { |
el14rd | 5:d8a06e7c54fb | 436 | snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 437 | } |
el14rd | 1:f682aeb462f1 | 438 | } |
el14rd | 1:f682aeb462f1 | 439 | |
el14rd | 1:f682aeb462f1 | 440 | |
el14rd | 1:f682aeb462f1 | 441 | //main code |
el14rd | 1:f682aeb462f1 | 442 | int main() |
el14rd | 1:f682aeb462f1 | 443 | { |
el14rd | 9:c8ea9b4af873 | 444 | //led1=1; |
el14rd | 1:f682aeb462f1 | 445 | lcd.init(); |
el14rd | 1:f682aeb462f1 | 446 | lcd.setBrightness(0.5); // put LED backlight on 50% |
el14rd | 1:f682aeb462f1 | 447 | calibrateJoystick(); // get centred values of joystick |
el14rd | 1:f682aeb462f1 | 448 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el14rd | 1:f682aeb462f1 | 449 | lcd.refresh(); |
el14rd | 2:d7b17623ba26 | 450 | initNut(); |
el14rd | 2:d7b17623ba26 | 451 | initSnake(); |
el14rd | 8:b01f638d23b4 | 452 | button.rise(&buttonPressed); |
el14rd | 8:b01f638d23b4 | 453 | buttonA.mode(PullDown); |
el14rd | 1:f682aeb462f1 | 454 | //infinite while loop |
el14rd | 1:f682aeb462f1 | 455 | while(1) { |
el14rd | 9:c8ea9b4af873 | 456 | led2=1; |
el14rd | 8:b01f638d23b4 | 457 | if(buttonFlag==0) { //joystick button unpressed |
el14rd | 8:b01f638d23b4 | 458 | lcd.printString("Press to start",1,1); |
el14rd | 8:b01f638d23b4 | 459 | snakeModel(); |
el14rd | 8:b01f638d23b4 | 460 | } else { |
el14rd | 8:b01f638d23b4 | 461 | |
el14rd | 8:b01f638d23b4 | 462 | leds = 0; |
el14rd | 8:b01f638d23b4 | 463 | |
el14rd | 8:b01f638d23b4 | 464 | if(buttonA) { // if button pressed, toggle pause |
el14rd | 8:b01f638d23b4 | 465 | wait_ms(50); |
el14rd | 8:b01f638d23b4 | 466 | pause = !pause; |
el14rd | 8:b01f638d23b4 | 467 | leds = 15; |
el14rd | 9:c8ea9b4af873 | 468 | led2=0; |
el14rd | 9:c8ea9b4af873 | 469 | led1=1; |
el14rd | 8:b01f638d23b4 | 470 | } |
el14rd | 8:b01f638d23b4 | 471 | |
el14rd | 8:b01f638d23b4 | 472 | while(pause) { |
el14rd | 8:b01f638d23b4 | 473 | leds = 3; |
el14rd | 8:b01f638d23b4 | 474 | if(buttonA) { // if button pressed, toggle pause |
el14rd | 8:b01f638d23b4 | 475 | wait_ms(50); |
el14rd | 8:b01f638d23b4 | 476 | pause = 0; |
el14rd | 8:b01f638d23b4 | 477 | leds = 1; |
el14rd | 9:c8ea9b4af873 | 478 | led2=1; |
el14rd | 9:c8ea9b4af873 | 479 | led1=0; |
el14rd | 8:b01f638d23b4 | 480 | |
el14rd | 8:b01f638d23b4 | 481 | } |
el14rd | 8:b01f638d23b4 | 482 | } |
el14rd | 8:b01f638d23b4 | 483 | } |
el14rd | 8:b01f638d23b4 | 484 | |
el14rd | 8:b01f638d23b4 | 485 | |
el14rd | 1:f682aeb462f1 | 486 | lcd.clear(); |
el14rd | 5:d8a06e7c54fb | 487 | lifeFlag=0; |
el14rd | 1:f682aeb462f1 | 488 | updateSnake(); |
el14rd | 1:f682aeb462f1 | 489 | drawSnake(); |
el14rd | 2:d7b17623ba26 | 490 | drawNut(); |
el14rd | 1:f682aeb462f1 | 491 | moveSnake(); |
el14rd | 1:f682aeb462f1 | 492 | startGmae(); |
el14rd | 8:b01f638d23b4 | 493 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 494 | wait(0.08); |
el14rd | 8:b01f638d23b4 | 495 | //pauseGame(); |
el14rd | 8:b01f638d23b4 | 496 | //if (buttonFlag==1){ |
el14rd | 8:b01f638d23b4 | 497 | //while(1){ |
el14rd | 8:b01f638d23b4 | 498 | // buttonFlag =0; |
el14rd | 8:b01f638d23b4 | 499 | //if(buttonFlag ==1) |
el14rd | 8:b01f638d23b4 | 500 | // break; |
el14rd | 8:b01f638d23b4 | 501 | //} |
el14rd | 8:b01f638d23b4 | 502 | // } |
el14rd | 8:b01f638d23b4 | 503 | if(lifeFlag==1) { |
el14rd | 8:b01f638d23b4 | 504 | lcd.clear(); |
el14rd | 8:b01f638d23b4 | 505 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 506 | printScore(); |
el14rd | 8:b01f638d23b4 | 507 | for(int i=0; i<=11; i++) { |
el14rd | 8:b01f638d23b4 | 508 | buzzer.period(1/(frequency[i])); // set PWM period |
el14rd | 8:b01f638d23b4 | 509 | buzzer=0.1; // set duty cycle |
el14rd | 8:b01f638d23b4 | 510 | wait(0.5*beat[i]); |
el14rd | 8:b01f638d23b4 | 511 | } |
el14rd | 9:c8ea9b4af873 | 512 | buzzer=0; |
el14rd | 8:b01f638d23b4 | 513 | } |
el14rd | 9:c8ea9b4af873 | 514 | led1=1; |
el14rd | 9:c8ea9b4af873 | 515 | ain = pot.read(); |
el14rd | 9:c8ea9b4af873 | 516 | lcd.setBrightness(ain); |
el14rd | 9:c8ea9b4af873 | 517 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 518 | |
el14rd | 8:b01f638d23b4 | 519 | } |
el14rd | 8:b01f638d23b4 | 520 | |
el14rd | 9:c8ea9b4af873 | 521 | |
el14rd | 1:f682aeb462f1 | 522 | } |
el14rd | 1:f682aeb462f1 | 523 |