hahaha

Dependencies:   mbed

Committer:
arthicha
Date:
Tue Dec 06 06:11:54 2016 +0000
Revision:
1:d8ce226c8c2e
;UUpdate;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arthicha 1:d8ce226c8c2e 1 #include "mbed.h"
arthicha 1:d8ce226c8c2e 2 #include "bmu.h"
arthicha 1:d8ce226c8c2e 3
arthicha 1:d8ce226c8c2e 4
arthicha 1:d8ce226c8c2e 5 InterruptIn lim(D3);
arthicha 1:d8ce226c8c2e 6 InterruptIn rotate(D4);
arthicha 1:d8ce226c8c2e 7 InterruptIn choose(D5);
arthicha 1:d8ce226c8c2e 8 Ticker flipper;
arthicha 1:d8ce226c8c2e 9 Ticker Update;
arthicha 1:d8ce226c8c2e 10
arthicha 1:d8ce226c8c2e 11 bmuimu imu;
arthicha 1:d8ce226c8c2e 12
arthicha 1:d8ce226c8c2e 13 Serial pc(USBTX,USBRX);
arthicha 1:d8ce226c8c2e 14 Serial mas(D8,D2);
arthicha 1:d8ce226c8c2e 15 Serial duino(PA_11, PA_12);
arthicha 1:d8ce226c8c2e 16
arthicha 1:d8ce226c8c2e 17 Timer t;
arthicha 1:d8ce226c8c2e 18 Timer timer;
arthicha 1:d8ce226c8c2e 19
arthicha 1:d8ce226c8c2e 20 //*********************************************************************************************************************************
arthicha 1:d8ce226c8c2e 21 //*********************************************************************************************************************************
arthicha 1:d8ce226c8c2e 22 //*********************************************************************************************************************************
arthicha 1:d8ce226c8c2e 23 // variable
arthicha 1:d8ce226c8c2e 24 //*********************************************************************************************************************************
arthicha 1:d8ce226c8c2e 25 //*********************************************************************************************************************************
arthicha 1:d8ce226c8c2e 26 //*********************************************************************************************************************************
arthicha 1:d8ce226c8c2e 27 bool anach = false;
arthicha 1:d8ce226c8c2e 28 bool buttonState[3] = {0,0,0}; // button limit switchm rotate and choose state respectively.
arthicha 1:d8ce226c8c2e 29 unsigned int county = 10; // county down.
arthicha 1:d8ce226c8c2e 30 char data[50];
arthicha 1:d8ce226c8c2e 31 char elem;
arthicha 1:d8ce226c8c2e 32 int yyaaww;
arthicha 1:d8ce226c8c2e 33 bool errorie = false; // store error in ambigous imu data.
arthicha 1:d8ce226c8c2e 34 int helperRoll = 0, helperPitch=0,helperYaw=0;
arthicha 1:d8ce226c8c2e 35 unsigned int indx = 0;
arthicha 1:d8ce226c8c2e 36 unsigned int state = 0;
arthicha 1:d8ce226c8c2e 37 int dete[50];
arthicha 1:d8ce226c8c2e 38 int lis[50] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
arthicha 1:d8ce226c8c2e 39
arthicha 1:d8ce226c8c2e 40 // state 0 = initial / start.
arthicha 1:d8ce226c8c2e 41 // 1 = waiting for limit switch.
arthicha 1:d8ce226c8c2e 42
arthicha 1:d8ce226c8c2e 43 unsigned int turn = 0; // turn.
arthicha 1:d8ce226c8c2e 44
arthicha 1:d8ce226c8c2e 45 unsigned int x = 0, y = 0, z = 0,lx=0,ly=0,lz=0;
arthicha 1:d8ce226c8c2e 46 int winStatus = 0;
arthicha 1:d8ce226c8c2e 47
arthicha 1:d8ce226c8c2e 48
arthicha 1:d8ce226c8c2e 49
arthicha 1:d8ce226c8c2e 50 int A , B , C , player , button;
arthicha 1:d8ce226c8c2e 51 int countDown = 9;
arthicha 1:d8ce226c8c2e 52 int WMbox[6];
arthicha 1:d8ce226c8c2e 53 int map[5][5][5],rmap[5][5][5]; //store map.
arthicha 1:d8ce226c8c2e 54 int Check1[5][5][5] , Check2[5][5][5]; //convert to x or o map.
arthicha 1:d8ce226c8c2e 55
arthicha 1:d8ce226c8c2e 56 void Rise(int but)
arthicha 1:d8ce226c8c2e 57 {
arthicha 1:d8ce226c8c2e 58 buttonState[but-3] = 1;
arthicha 1:d8ce226c8c2e 59 }
arthicha 1:d8ce226c8c2e 60
arthicha 1:d8ce226c8c2e 61 void Fall(int but)
arthicha 1:d8ce226c8c2e 62 {
arthicha 1:d8ce226c8c2e 63 buttonState[but-3] = 0;
arthicha 1:d8ce226c8c2e 64 }
arthicha 1:d8ce226c8c2e 65
arthicha 1:d8ce226c8c2e 66 void Sent(char arrayi[],float it,int siz)
arthicha 1:d8ce226c8c2e 67 {
arthicha 1:d8ce226c8c2e 68 for (int i = 0;i<siz;i++)
arthicha 1:d8ce226c8c2e 69 {
arthicha 1:d8ce226c8c2e 70 mas.putc(arrayi[i]);
arthicha 1:d8ce226c8c2e 71 wait(it);
arthicha 1:d8ce226c8c2e 72 }
arthicha 1:d8ce226c8c2e 73 }
arthicha 1:d8ce226c8c2e 74
arthicha 1:d8ce226c8c2e 75 void LInitial(){ //ลงค่าเสร็จ
arthicha 1:d8ce226c8c2e 76 if (timer.read() > 1) {
arthicha 1:d8ce226c8c2e 77
arthicha 1:d8ce226c8c2e 78
arthicha 1:d8ce226c8c2e 79 duino.putc(lis[20]);
arthicha 1:d8ce226c8c2e 80
arthicha 1:d8ce226c8c2e 81
arthicha 1:d8ce226c8c2e 82 timer.reset();
arthicha 1:d8ce226c8c2e 83 }
arthicha 1:d8ce226c8c2e 84 if (duino.readable()){
arthicha 1:d8ce226c8c2e 85 }
arthicha 1:d8ce226c8c2e 86 }
arthicha 1:d8ce226c8c2e 87
arthicha 1:d8ce226c8c2e 88 void getStr()
arthicha 1:d8ce226c8c2e 89 {
arthicha 1:d8ce226c8c2e 90 while (mas.readable())
arthicha 1:d8ce226c8c2e 91 {
arthicha 1:d8ce226c8c2e 92
arthicha 1:d8ce226c8c2e 93 elem = mas.getc();
arthicha 1:d8ce226c8c2e 94 pc.printf("char is %d\n\r",elem);
arthicha 1:d8ce226c8c2e 95 if ((elem == '\n'))
arthicha 1:d8ce226c8c2e 96 {
arthicha 1:d8ce226c8c2e 97 pc.printf("string is %s\n\r",data);
arthicha 1:d8ce226c8c2e 98 for(int i=0;i<indx;i++)
arthicha 1:d8ce226c8c2e 99 {
arthicha 1:d8ce226c8c2e 100 if (indx > 2)
arthicha 1:d8ce226c8c2e 101 {
arthicha 1:d8ce226c8c2e 102 dete[i] = data[i];
arthicha 1:d8ce226c8c2e 103 }
arthicha 1:d8ce226c8c2e 104 }
arthicha 1:d8ce226c8c2e 105 for(int i=0;i<30;i++)
arthicha 1:d8ce226c8c2e 106 {
arthicha 1:d8ce226c8c2e 107 data[i] = '\0';
arthicha 1:d8ce226c8c2e 108 }
arthicha 1:d8ce226c8c2e 109 indx = 0;
arthicha 1:d8ce226c8c2e 110 break;
arthicha 1:d8ce226c8c2e 111 }else
arthicha 1:d8ce226c8c2e 112 {
arthicha 1:d8ce226c8c2e 113 data[indx] = elem;
arthicha 1:d8ce226c8c2e 114 indx += 1;
arthicha 1:d8ce226c8c2e 115 }
arthicha 1:d8ce226c8c2e 116
arthicha 1:d8ce226c8c2e 117 }
arthicha 1:d8ce226c8c2e 118 }
arthicha 1:d8ce226c8c2e 119 bool chooseAnother()
arthicha 1:d8ce226c8c2e 120 {
arthicha 1:d8ce226c8c2e 121 Sent("cho\n",0.1,4);
arthicha 1:d8ce226c8c2e 122 while(1)
arthicha 1:d8ce226c8c2e 123 {
arthicha 1:d8ce226c8c2e 124 if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='0'))
arthicha 1:d8ce226c8c2e 125 {
arthicha 1:d8ce226c8c2e 126 return true;
arthicha 1:d8ce226c8c2e 127 }else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='x'))
arthicha 1:d8ce226c8c2e 128 {
arthicha 1:d8ce226c8c2e 129 return false;
arthicha 1:d8ce226c8c2e 130 }
arthicha 1:d8ce226c8c2e 131 }
arthicha 1:d8ce226c8c2e 132 }
arthicha 1:d8ce226c8c2e 133
arthicha 1:d8ce226c8c2e 134 void Rise1()
arthicha 1:d8ce226c8c2e 135 {
arthicha 1:d8ce226c8c2e 136 buttonState[0] = 1;
arthicha 1:d8ce226c8c2e 137 }
arthicha 1:d8ce226c8c2e 138
arthicha 1:d8ce226c8c2e 139 void Fall1()
arthicha 1:d8ce226c8c2e 140 {
arthicha 1:d8ce226c8c2e 141 buttonState[0] = 0;
arthicha 1:d8ce226c8c2e 142 }
arthicha 1:d8ce226c8c2e 143 void Rise2()
arthicha 1:d8ce226c8c2e 144 {
arthicha 1:d8ce226c8c2e 145 buttonState[1] = 1;
arthicha 1:d8ce226c8c2e 146 }
arthicha 1:d8ce226c8c2e 147
arthicha 1:d8ce226c8c2e 148 void Fall2()
arthicha 1:d8ce226c8c2e 149 {
arthicha 1:d8ce226c8c2e 150 buttonState[1] = 0;
arthicha 1:d8ce226c8c2e 151 }
arthicha 1:d8ce226c8c2e 152 void LLose(){ //แพ้
arthicha 1:d8ce226c8c2e 153
arthicha 1:d8ce226c8c2e 154 if ( t.read() > 1) {
arthicha 1:d8ce226c8c2e 155 //pc.printf("Writing!\n\r");
arthicha 1:d8ce226c8c2e 156 duino.putc( lis[3]);
arthicha 1:d8ce226c8c2e 157 duino.putc( lis[4]);
arthicha 1:d8ce226c8c2e 158 duino.putc( lis[5]);
arthicha 1:d8ce226c8c2e 159 duino.putc( lis[6]);
arthicha 1:d8ce226c8c2e 160 t.reset();
arthicha 1:d8ce226c8c2e 161 }
arthicha 1:d8ce226c8c2e 162 if ( duino.readable())
arthicha 1:d8ce226c8c2e 163 {
arthicha 1:d8ce226c8c2e 164 //pc.printf("recieving %c",duino.getc());
arthicha 1:d8ce226c8c2e 165 }
arthicha 1:d8ce226c8c2e 166
arthicha 1:d8ce226c8c2e 167 }
arthicha 1:d8ce226c8c2e 168 void Lose()
arthicha 1:d8ce226c8c2e 169 {
arthicha 1:d8ce226c8c2e 170 LLose();
arthicha 1:d8ce226c8c2e 171 Sent("los\n",0.1,4);
arthicha 1:d8ce226c8c2e 172 }
arthicha 1:d8ce226c8c2e 173
arthicha 1:d8ce226c8c2e 174 void LWin(){ //ชนะ
arthicha 1:d8ce226c8c2e 175 if ( timer.read() > 1) {
arthicha 1:d8ce226c8c2e 176 //pc.printf("Writing!\n\r");
arthicha 1:d8ce226c8c2e 177 duino.putc( lis[0]);
arthicha 1:d8ce226c8c2e 178 duino.putc( lis[1]);
arthicha 1:d8ce226c8c2e 179 duino.putc( lis[2]);
arthicha 1:d8ce226c8c2e 180 timer.reset();
arthicha 1:d8ce226c8c2e 181 }
arthicha 1:d8ce226c8c2e 182 }
arthicha 1:d8ce226c8c2e 183 void Win()
arthicha 1:d8ce226c8c2e 184 {
arthicha 1:d8ce226c8c2e 185 LWin();
arthicha 1:d8ce226c8c2e 186 Sent("lwin\n",0.1,4);
arthicha 1:d8ce226c8c2e 187 }
arthicha 1:d8ce226c8c2e 188 void Rise3()
arthicha 1:d8ce226c8c2e 189 {
arthicha 1:d8ce226c8c2e 190 buttonState[2] = 1;
arthicha 1:d8ce226c8c2e 191 }
arthicha 1:d8ce226c8c2e 192
arthicha 1:d8ce226c8c2e 193 void Fall3()
arthicha 1:d8ce226c8c2e 194 {
arthicha 1:d8ce226c8c2e 195 buttonState[2] = 0;
arthicha 1:d8ce226c8c2e 196 }
arthicha 1:d8ce226c8c2e 197 bool anotherRotate()
arthicha 1:d8ce226c8c2e 198 {
arthicha 1:d8ce226c8c2e 199 Sent("rot\n",0.1,4);
arthicha 1:d8ce226c8c2e 200 while(1)
arthicha 1:d8ce226c8c2e 201 {
arthicha 1:d8ce226c8c2e 202 if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='t'))
arthicha 1:d8ce226c8c2e 203 {
arthicha 1:d8ce226c8c2e 204 return true;
arthicha 1:d8ce226c8c2e 205 }else if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='x'))
arthicha 1:d8ce226c8c2e 206 {
arthicha 1:d8ce226c8c2e 207 return false;
arthicha 1:d8ce226c8c2e 208 }
arthicha 1:d8ce226c8c2e 209 }
arthicha 1:d8ce226c8c2e 210 }
arthicha 1:d8ce226c8c2e 211 void LCountDown(int ixxii){ //นับเวลา
arthicha 1:d8ce226c8c2e 212 if ( timer.read() > 1) {
arthicha 1:d8ce226c8c2e 213 duino.putc(ixxii);
arthicha 1:d8ce226c8c2e 214 timer.reset();
arthicha 1:d8ce226c8c2e 215 }
arthicha 1:d8ce226c8c2e 216 }
arthicha 1:d8ce226c8c2e 217 void LMapComplete(){ //ลงค่าเสร็จ
arthicha 1:d8ce226c8c2e 218 if ( timer.read() > 1) {
arthicha 1:d8ce226c8c2e 219 //pc.printf("Writing!\n\r");
arthicha 1:d8ce226c8c2e 220
arthicha 1:d8ce226c8c2e 221
arthicha 1:d8ce226c8c2e 222 duino.putc( lis[20]);
arthicha 1:d8ce226c8c2e 223
arthicha 1:d8ce226c8c2e 224
arthicha 1:d8ce226c8c2e 225 timer.reset();}}
arthicha 1:d8ce226c8c2e 226
arthicha 1:d8ce226c8c2e 227 int GetCoordinateX()
arthicha 1:d8ce226c8c2e 228 {
arthicha 1:d8ce226c8c2e 229 if ( WMbox[0] != -1)
arthicha 1:d8ce226c8c2e 230 {
arthicha 1:d8ce226c8c2e 231 return WMbox[0];
arthicha 1:d8ce226c8c2e 232 }else
arthicha 1:d8ce226c8c2e 233 {
arthicha 1:d8ce226c8c2e 234 return WMbox[3];
arthicha 1:d8ce226c8c2e 235 }
arthicha 1:d8ce226c8c2e 236 }
arthicha 1:d8ce226c8c2e 237
arthicha 1:d8ce226c8c2e 238 int GetCoordinateY()
arthicha 1:d8ce226c8c2e 239 {
arthicha 1:d8ce226c8c2e 240 if ( WMbox[1] != -1)
arthicha 1:d8ce226c8c2e 241 {
arthicha 1:d8ce226c8c2e 242 return WMbox[1];
arthicha 1:d8ce226c8c2e 243 }else
arthicha 1:d8ce226c8c2e 244 {
arthicha 1:d8ce226c8c2e 245 return WMbox[4];
arthicha 1:d8ce226c8c2e 246 }
arthicha 1:d8ce226c8c2e 247 }
arthicha 1:d8ce226c8c2e 248
arthicha 1:d8ce226c8c2e 249 int GetCoordinateZ()
arthicha 1:d8ce226c8c2e 250 {
arthicha 1:d8ce226c8c2e 251 if ( WMbox[2] != -1)
arthicha 1:d8ce226c8c2e 252 {
arthicha 1:d8ce226c8c2e 253 return WMbox[2];
arthicha 1:d8ce226c8c2e 254 }else
arthicha 1:d8ce226c8c2e 255 {
arthicha 1:d8ce226c8c2e 256 return WMbox[5];
arthicha 1:d8ce226c8c2e 257 }
arthicha 1:d8ce226c8c2e 258 }
arthicha 1:d8ce226c8c2e 259
arthicha 1:d8ce226c8c2e 260 void Choose(int box)
arthicha 1:d8ce226c8c2e 261 {
arthicha 1:d8ce226c8c2e 262 if (box)
arthicha 1:d8ce226c8c2e 263 {
arthicha 1:d8ce226c8c2e 264 WMbox[0] = -1;
arthicha 1:d8ce226c8c2e 265 WMbox[1] = -1;
arthicha 1:d8ce226c8c2e 266 WMbox[2] = -1;
arthicha 1:d8ce226c8c2e 267 }else
arthicha 1:d8ce226c8c2e 268 {
arthicha 1:d8ce226c8c2e 269 WMbox[3] = -1;
arthicha 1:d8ce226c8c2e 270 WMbox[4] = -1;
arthicha 1:d8ce226c8c2e 271 WMbox[5] = -1;
arthicha 1:d8ce226c8c2e 272 }
arthicha 1:d8ce226c8c2e 273 }
arthicha 1:d8ce226c8c2e 274
arthicha 1:d8ce226c8c2e 275
arthicha 1:d8ce226c8c2e 276
arthicha 1:d8ce226c8c2e 277
arthicha 1:d8ce226c8c2e 278
arthicha 1:d8ce226c8c2e 279
arthicha 1:d8ce226c8c2e 280
arthicha 1:d8ce226c8c2e 281 int main()
arthicha 1:d8ce226c8c2e 282 {
arthicha 1:d8ce226c8c2e 283 lim.rise(&Rise1);
arthicha 1:d8ce226c8c2e 284 lim.fall(&Fall1);
arthicha 1:d8ce226c8c2e 285 rotate.rise(&Rise2);
arthicha 1:d8ce226c8c2e 286 rotate.fall(&Fall2);
arthicha 1:d8ce226c8c2e 287 choose.rise(&Rise3);
arthicha 1:d8ce226c8c2e 288 choose.fall(&Fall3);
arthicha 1:d8ce226c8c2e 289 flipper.attach(&getStr,1.0); //timer interrupt for recieving bluetooth.
arthicha 1:d8ce226c8c2e 290 LInitial(); // set initial pattern in led matrix.
arthicha 1:d8ce226c8c2e 291 wait(5); // wait 5 second for putting the box down to set initial position of imu.
arthicha 1:d8ce226c8c2e 292
arthicha 1:d8ce226c8c2e 293 while(1){
arthicha 1:d8ce226c8c2e 294
arthicha 1:d8ce226c8c2e 295 if ((dete[0]=='l')&&(dete[1]=='o')&&(dete[2]=='s')){
arthicha 1:d8ce226c8c2e 296 Lose();
arthicha 1:d8ce226c8c2e 297
arthicha 1:d8ce226c8c2e 298 break;
arthicha 1:d8ce226c8c2e 299 }
arthicha 1:d8ce226c8c2e 300 else if ((dete[0]=='l')&&(dete[1]=='w')&&(dete[2]=='i')&&(dete[3]=='n')){
arthicha 1:d8ce226c8c2e 301 Win();
arthicha 1:d8ce226c8c2e 302
arthicha 1:d8ce226c8c2e 303 break;
arthicha 1:d8ce226c8c2e 304 }
arthicha 1:d8ce226c8c2e 305 else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='o')){
arthicha 1:d8ce226c8c2e 306 chooseAnother();
arthicha 1:d8ce226c8c2e 307 }
arthicha 1:d8ce226c8c2e 308 else if ((dete[0]=='r')&&(dete[1]=='o')&&(dete[2]=='t')){
arthicha 1:d8ce226c8c2e 309 anotherRotate();
arthicha 1:d8ce226c8c2e 310 }
arthicha 1:d8ce226c8c2e 311 else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='r')){
arthicha 1:d8ce226c8c2e 312 GetCoordinateX();
arthicha 1:d8ce226c8c2e 313 }
arthicha 1:d8ce226c8c2e 314 else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='p')){
arthicha 1:d8ce226c8c2e 315 GetCoordinateY();
arthicha 1:d8ce226c8c2e 316 }
arthicha 1:d8ce226c8c2e 317 else if ((dete[0]=='h')&&(dete[1]=='e')&&(dete[2]=='l')&&(dete[3]=='y')){
arthicha 1:d8ce226c8c2e 318 GetCoordinateZ();
arthicha 1:d8ce226c8c2e 319 }
arthicha 1:d8ce226c8c2e 320 else if ((dete[0]=='o')&&(dete[1]=='k')){
arthicha 1:d8ce226c8c2e 321 LMapComplete();
arthicha 1:d8ce226c8c2e 322 }
arthicha 1:d8ce226c8c2e 323 else if ((dete[0]=='m')&&(dete[1]=='a')&&(dete[2]=='p')&&(dete[3]=='c')){
arthicha 1:d8ce226c8c2e 324 LMapComplete();
arthicha 1:d8ce226c8c2e 325 }
arthicha 1:d8ce226c8c2e 326 else if ((dete[0]=='T')&&(dete[1]=='+')&&(dete[2]=='+')){
arthicha 1:d8ce226c8c2e 327 turn += 1;
arthicha 1:d8ce226c8c2e 328 }
arthicha 1:d8ce226c8c2e 329 else if ((dete[0]=='c')&&(dete[1]=='h')&&(dete[2]=='1')){
arthicha 1:d8ce226c8c2e 330 Choose(1);
arthicha 1:d8ce226c8c2e 331 }
arthicha 1:d8ce226c8c2e 332 else if ((dete[0]=='T')&&(dete[1]=='u')&&(dete[2]=='r')){
arthicha 1:d8ce226c8c2e 333 LCountDown(turn);
arthicha 1:d8ce226c8c2e 334
arthicha 1:d8ce226c8c2e 335 }
arthicha 1:d8ce226c8c2e 336 }
arthicha 1:d8ce226c8c2e 337 }