14A Game BettyMachine

Dependencies:   eeprom mbed

Committer:
jettanan
Date:
Wed Dec 09 06:31:03 2015 +0000
Revision:
0:8f12bfa12b59
14A Betty Machine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jettanan 0:8f12bfa12b59 1 #include "mbed.h"
jettanan 0:8f12bfa12b59 2 #include "eeprom.h"
jettanan 0:8f12bfa12b59 3
jettanan 0:8f12bfa12b59 4 Serial device(D8,D2);
jettanan 0:8f12bfa12b59 5 Serial pc(SERIAL_TX,SERIAL_RX);
jettanan 0:8f12bfa12b59 6 EEPROM memory(D14,D15,0);
jettanan 0:8f12bfa12b59 7 DigitalIn resetbt(D3);
jettanan 0:8f12bfa12b59 8 DigitalOut led(LED2);
jettanan 0:8f12bfa12b59 9 DigitalOut ln1(A0);
jettanan 0:8f12bfa12b59 10 DigitalOut ln2(A1);
jettanan 0:8f12bfa12b59 11 PwmOut ln3(PC_8);
jettanan 0:8f12bfa12b59 12 PwmOut ln4(PC_6);
jettanan 0:8f12bfa12b59 13 PwmOut ln11(PB_14);
jettanan 0:8f12bfa12b59 14 PwmOut ln22(PB_15);
jettanan 0:8f12bfa12b59 15 DigitalIn limitA(D4);
jettanan 0:8f12bfa12b59 16 DigitalIn limitB(D5);
jettanan 0:8f12bfa12b59 17 DigitalIn limitC(D6);
jettanan 0:8f12bfa12b59 18 DigitalIn limitD(D7);
jettanan 0:8f12bfa12b59 19 DigitalIn limit1(D9);
jettanan 0:8f12bfa12b59 20 DigitalIn limit2(D10);
jettanan 0:8f12bfa12b59 21 DigitalIn limit3(D11);
jettanan 0:8f12bfa12b59 22 DigitalIn limit4(D12);
jettanan 0:8f12bfa12b59 23 DigitalIn switchh(PB_13);
jettanan 0:8f12bfa12b59 24 Timer t;
jettanan 0:8f12bfa12b59 25 Timer t2;
jettanan 0:8f12bfa12b59 26 Timer t3;
jettanan 0:8f12bfa12b59 27 int credits ;
jettanan 0:8f12bfa12b59 28 uint16_t score ;
jettanan 0:8f12bfa12b59 29 int t_score ;
jettanan 0:8f12bfa12b59 30 void MotorA(int a)//สายพาน
jettanan 0:8f12bfa12b59 31 {
jettanan 0:8f12bfa12b59 32 if(a == 1)
jettanan 0:8f12bfa12b59 33 {
jettanan 0:8f12bfa12b59 34 ln1 = 1;
jettanan 0:8f12bfa12b59 35 ln2 = 0;
jettanan 0:8f12bfa12b59 36 }
jettanan 0:8f12bfa12b59 37 if(a == -1)
jettanan 0:8f12bfa12b59 38 {
jettanan 0:8f12bfa12b59 39 ln1 = 0;
jettanan 0:8f12bfa12b59 40 ln2 = 1;
jettanan 0:8f12bfa12b59 41 }
jettanan 0:8f12bfa12b59 42 if(a == 0)
jettanan 0:8f12bfa12b59 43 {
jettanan 0:8f12bfa12b59 44 ln1 = 0;
jettanan 0:8f12bfa12b59 45 ln2 = 0;
jettanan 0:8f12bfa12b59 46 }
jettanan 0:8f12bfa12b59 47 }
jettanan 0:8f12bfa12b59 48 void MotorB(int a)//Gripper
jettanan 0:8f12bfa12b59 49 {
jettanan 0:8f12bfa12b59 50 if(a == 1)
jettanan 0:8f12bfa12b59 51 {
jettanan 0:8f12bfa12b59 52 ln3 = 1;
jettanan 0:8f12bfa12b59 53 ln4 = 0;
jettanan 0:8f12bfa12b59 54 }
jettanan 0:8f12bfa12b59 55 if(a == -1)
jettanan 0:8f12bfa12b59 56 {
jettanan 0:8f12bfa12b59 57 ln3 = 0;
jettanan 0:8f12bfa12b59 58 ln4 = 1;
jettanan 0:8f12bfa12b59 59 }
jettanan 0:8f12bfa12b59 60 if(a == 0)
jettanan 0:8f12bfa12b59 61 {
jettanan 0:8f12bfa12b59 62 ln3 = 0;
jettanan 0:8f12bfa12b59 63 ln4 = 0;
jettanan 0:8f12bfa12b59 64 }
jettanan 0:8f12bfa12b59 65 }
jettanan 0:8f12bfa12b59 66 void MotorC(int a)//Door
jettanan 0:8f12bfa12b59 67 {
jettanan 0:8f12bfa12b59 68 if(a == 1)
jettanan 0:8f12bfa12b59 69 {
jettanan 0:8f12bfa12b59 70 ln11 = 1;
jettanan 0:8f12bfa12b59 71 ln22 = 0;
jettanan 0:8f12bfa12b59 72 }
jettanan 0:8f12bfa12b59 73 if(a == -1)
jettanan 0:8f12bfa12b59 74 {
jettanan 0:8f12bfa12b59 75 ln11 = 0;
jettanan 0:8f12bfa12b59 76 ln22 = 1;
jettanan 0:8f12bfa12b59 77 }
jettanan 0:8f12bfa12b59 78 if(a == 0)
jettanan 0:8f12bfa12b59 79 {
jettanan 0:8f12bfa12b59 80 ln11 = 0;
jettanan 0:8f12bfa12b59 81 ln22 = 0;
jettanan 0:8f12bfa12b59 82 }
jettanan 0:8f12bfa12b59 83 }
jettanan 0:8f12bfa12b59 84 uint16_t numb[4];//0=high byte color , 1 = low byte color , 2 = high byte clear , 3 = low byte clear
jettanan 0:8f12bfa12b59 85 uint16_t command[7];
jettanan 0:8f12bfa12b59 86 uint16_t rnumb[4];
jettanan 0:8f12bfa12b59 87 uint16_t re_command[7];
jettanan 0:8f12bfa12b59 88 uint16_t pattern[24][4]={{1,2,3,0},{1,2,0,3},{1,3,2,0},{1,3,0,2},{1,0,2,3},{1,0,3,2}
jettanan 0:8f12bfa12b59 89 ,{2,1,3,0},{2,1,0,3},{2,3,1,0},{2,3,0,1},{2,0,1,3},{2,0,3,1}
jettanan 0:8f12bfa12b59 90 ,{3,1,2,0},{3,1,0,2},{3,2,1,0},{3,2,0,1},{3,0,1,2},{3,0,2,1}
jettanan 0:8f12bfa12b59 91 ,{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1}};
jettanan 0:8f12bfa12b59 92 int color_num , clear_num ;
jettanan 0:8f12bfa12b59 93 int colorb,clearb ;
jettanan 0:8f12bfa12b59 94 int num1 , num2 ;
jettanan 0:8f12bfa12b59 95 char ch ;
jettanan 0:8f12bfa12b59 96 int main() {
jettanan 0:8f12bfa12b59 97 pc.baud(115200);
jettanan 0:8f12bfa12b59 98 t_score = 0;
jettanan 0:8f12bfa12b59 99 pc.printf("\nin program");
jettanan 0:8f12bfa12b59 100 while(1)
jettanan 0:8f12bfa12b59 101 {
jettanan 0:8f12bfa12b59 102 if(resetbt == 1)
jettanan 0:8f12bfa12b59 103 {
jettanan 0:8f12bfa12b59 104 uint16_t m[4]={0};
jettanan 0:8f12bfa12b59 105 memory.write(20,m,8);
jettanan 0:8f12bfa12b59 106 pc.printf("clear complete");
jettanan 0:8f12bfa12b59 107 led = 1;
jettanan 0:8f12bfa12b59 108 wait(1);
jettanan 0:8f12bfa12b59 109 led = 0;
jettanan 0:8f12bfa12b59 110 }
jettanan 0:8f12bfa12b59 111 if(re_command[1] == 0x44)
jettanan 0:8f12bfa12b59 112 {
jettanan 0:8f12bfa12b59 113 uint16_t m[4]={0};
jettanan 0:8f12bfa12b59 114 memory.write(20,m,8);
jettanan 0:8f12bfa12b59 115 pc.printf("clear complete");
jettanan 0:8f12bfa12b59 116 }
jettanan 0:8f12bfa12b59 117 pc.printf("\nwait for serial");
jettanan 0:8f12bfa12b59 118
jettanan 0:8f12bfa12b59 119 for(int i=0;i<7;i++)
jettanan 0:8f12bfa12b59 120 {
jettanan 0:8f12bfa12b59 121 re_command[i] = device.getc();
jettanan 0:8f12bfa12b59 122 pc.printf("%x ",re_command[i]);
jettanan 0:8f12bfa12b59 123 }
jettanan 0:8f12bfa12b59 124 if(re_command[0] != 0x42)
jettanan 0:8f12bfa12b59 125 {
jettanan 0:8f12bfa12b59 126 pc.printf("Wrong Pattern");
jettanan 0:8f12bfa12b59 127 continue ;
jettanan 0:8f12bfa12b59 128 }
jettanan 0:8f12bfa12b59 129 pc.printf("\n%s\n",re_command);
jettanan 0:8f12bfa12b59 130 if(re_command[6] != 0x4D)
jettanan 0:8f12bfa12b59 131 {
jettanan 0:8f12bfa12b59 132 pc.printf("Wrong Pattern");
jettanan 0:8f12bfa12b59 133 continue ;
jettanan 0:8f12bfa12b59 134 }
jettanan 0:8f12bfa12b59 135 if(re_command[1] == 0x52)//Record All Bottle
jettanan 0:8f12bfa12b59 136 {
jettanan 0:8f12bfa12b59 137 num1 = re_command[3];
jettanan 0:8f12bfa12b59 138 num2 = re_command[2];
jettanan 0:8f12bfa12b59 139 color_num = (num2*256) + num1 ;
jettanan 0:8f12bfa12b59 140 num1 = re_command[5];
jettanan 0:8f12bfa12b59 141 num2 = re_command[4];
jettanan 0:8f12bfa12b59 142 clear_num = (num2*256) + num1 ;
jettanan 0:8f12bfa12b59 143 pc.printf("color bottle = %d \nclear bottle = %d \n",color_num,clear_num);
jettanan 0:8f12bfa12b59 144 memory.read(20,numb,8);
jettanan 0:8f12bfa12b59 145 colorb = ((numb[0]*256)+numb[1])+color_num;
jettanan 0:8f12bfa12b59 146 clearb = ((numb[2]*256)+numb[3])+clear_num;
jettanan 0:8f12bfa12b59 147 int highb , lowb ;
jettanan 0:8f12bfa12b59 148 highb = colorb/256 ;
jettanan 0:8f12bfa12b59 149 lowb = colorb - (highb*256);
jettanan 0:8f12bfa12b59 150 numb[1] = lowb ;
jettanan 0:8f12bfa12b59 151 numb[0] = highb ;
jettanan 0:8f12bfa12b59 152 highb = clearb/256 ;
jettanan 0:8f12bfa12b59 153 lowb = clearb - (highb*256);
jettanan 0:8f12bfa12b59 154 numb[3] = lowb ;
jettanan 0:8f12bfa12b59 155 numb[2] = highb ;
jettanan 0:8f12bfa12b59 156 memory.write (20,numb,8);
jettanan 0:8f12bfa12b59 157 memory.read(20,rnumb,8);
jettanan 0:8f12bfa12b59 158 colorb = (rnumb[0]*256) + rnumb[1] ;
jettanan 0:8f12bfa12b59 159 clearb = (rnumb[2]*256) + rnumb[3] ;
jettanan 0:8f12bfa12b59 160 pc.printf("Record Complete\nall color bottle = %d \nall clear bottle = %d \n",colorb,clearb);
jettanan 0:8f12bfa12b59 161 }
jettanan 0:8f12bfa12b59 162 else if(re_command[1] == 0x43)
jettanan 0:8f12bfa12b59 163 {
jettanan 0:8f12bfa12b59 164 memory.read(20,numb,8);
jettanan 0:8f12bfa12b59 165 colorb = ((numb[0]*256)+numb[1]);
jettanan 0:8f12bfa12b59 166 clearb = ((numb[2]*256)+numb[3]);
jettanan 0:8f12bfa12b59 167 pc.printf("color bottle : %d\n",colorb);
jettanan 0:8f12bfa12b59 168 pc.printf("clear bottle : %d\n",clearb);
jettanan 0:8f12bfa12b59 169 }
jettanan 0:8f12bfa12b59 170 else if(re_command[1] == 0x50)
jettanan 0:8f12bfa12b59 171 {
jettanan 0:8f12bfa12b59 172 pc.printf("Game Mode");
jettanan 0:8f12bfa12b59 173 credits = re_command[5];
jettanan 0:8f12bfa12b59 174 pc.printf("Credits : %d\n",credits);
jettanan 0:8f12bfa12b59 175 srand(time(NULL));
jettanan 0:8f12bfa12b59 176 int patternNum ;
jettanan 0:8f12bfa12b59 177
jettanan 0:8f12bfa12b59 178 for(int i=1;i<=credits;i++)
jettanan 0:8f12bfa12b59 179 {
jettanan 0:8f12bfa12b59 180 patternNum = rand()%16 ;
jettanan 0:8f12bfa12b59 181 command[0] = 0x42 ;
jettanan 0:8f12bfa12b59 182 command[1] = 0x50 ;
jettanan 0:8f12bfa12b59 183 command[2] = pattern[patternNum][0];
jettanan 0:8f12bfa12b59 184 command[3] = pattern[patternNum][1];
jettanan 0:8f12bfa12b59 185 command[4] = pattern[patternNum][2];
jettanan 0:8f12bfa12b59 186 command[5] = pattern[patternNum][3];
jettanan 0:8f12bfa12b59 187 command[6] = 0x4D ;
jettanan 0:8f12bfa12b59 188 for(int j=0;j<7;j++)
jettanan 0:8f12bfa12b59 189 {
jettanan 0:8f12bfa12b59 190 wait(0.2);
jettanan 0:8f12bfa12b59 191 device.putc(command[j]);
jettanan 0:8f12bfa12b59 192 pc.printf("\n0x%x",command[j]);
jettanan 0:8f12bfa12b59 193 }
jettanan 0:8f12bfa12b59 194 ln11 = 0 ;
jettanan 0:8f12bfa12b59 195 ln22 = 0.4 ;
jettanan 0:8f12bfa12b59 196 wait(1);
jettanan 0:8f12bfa12b59 197 ln11 = 0;
jettanan 0:8f12bfa12b59 198 ln22 = 0;
jettanan 0:8f12bfa12b59 199 wait(0.6);
jettanan 0:8f12bfa12b59 200 ln11 = 0;
jettanan 0:8f12bfa12b59 201 ln22 = 0;
jettanan 0:8f12bfa12b59 202 wait(0.4);
jettanan 0:8f12bfa12b59 203 ln11 = 0.4 ;
jettanan 0:8f12bfa12b59 204 ln22 = 0 ;
jettanan 0:8f12bfa12b59 205 wait(1);
jettanan 0:8f12bfa12b59 206 ln11 = 0;
jettanan 0:8f12bfa12b59 207 ln22 = 0;
jettanan 0:8f12bfa12b59 208 ln3 = 0.1;
jettanan 0:8f12bfa12b59 209 ln4 = 0;
jettanan 0:8f12bfa12b59 210 wait(0.1);
jettanan 0:8f12bfa12b59 211 ln3 = 0 ;
jettanan 0:8f12bfa12b59 212 ln4 = 0 ;
jettanan 0:8f12bfa12b59 213 while(limitA ==0)
jettanan 0:8f12bfa12b59 214 {
jettanan 0:8f12bfa12b59 215
jettanan 0:8f12bfa12b59 216 }
jettanan 0:8f12bfa12b59 217 pc.printf("\nPut the ball complete\n");
jettanan 0:8f12bfa12b59 218 device.putc(0x50);
jettanan 0:8f12bfa12b59 219 ln4 = 0.3 ;
jettanan 0:8f12bfa12b59 220 ln3 = 0 ;
jettanan 0:8f12bfa12b59 221 wait(0.5);
jettanan 0:8f12bfa12b59 222 MotorA(1);
jettanan 0:8f12bfa12b59 223 t.start();
jettanan 0:8f12bfa12b59 224 t.reset();
jettanan 0:8f12bfa12b59 225 int a;
jettanan 0:8f12bfa12b59 226 pc.printf("MotorA(a)");
jettanan 0:8f12bfa12b59 227 while(t.read_ms()<10000)
jettanan 0:8f12bfa12b59 228 {
jettanan 0:8f12bfa12b59 229 if(limitB==1)
jettanan 0:8f12bfa12b59 230 {
jettanan 0:8f12bfa12b59 231 a=1;
jettanan 0:8f12bfa12b59 232 }
jettanan 0:8f12bfa12b59 233 if(limitC==1)
jettanan 0:8f12bfa12b59 234 {
jettanan 0:8f12bfa12b59 235 a=1;
jettanan 0:8f12bfa12b59 236 }
jettanan 0:8f12bfa12b59 237 if(limitD==1)
jettanan 0:8f12bfa12b59 238 {
jettanan 0:8f12bfa12b59 239 a=-1;
jettanan 0:8f12bfa12b59 240 }
jettanan 0:8f12bfa12b59 241 if(switchh==1)
jettanan 0:8f12bfa12b59 242 {
jettanan 0:8f12bfa12b59 243 break;
jettanan 0:8f12bfa12b59 244 }
jettanan 0:8f12bfa12b59 245 MotorA(a);
jettanan 0:8f12bfa12b59 246
jettanan 0:8f12bfa12b59 247 }
jettanan 0:8f12bfa12b59 248 pc.printf("\nBall has fall\n");
jettanan 0:8f12bfa12b59 249
jettanan 0:8f12bfa12b59 250 MotorA(0);
jettanan 0:8f12bfa12b59 251 ln3 = 0;
jettanan 0:8f12bfa12b59 252 ln4 = 0.3;
jettanan 0:8f12bfa12b59 253 wait(0.1);
jettanan 0:8f12bfa12b59 254 MotorB(0);
jettanan 0:8f12bfa12b59 255 MotorB(0);
jettanan 0:8f12bfa12b59 256 device.putc(0x53);
jettanan 0:8f12bfa12b59 257 wait(0.1);
jettanan 0:8f12bfa12b59 258 /* uint16_t pattern[24][4]={{1,2,3,4},{1,2,4,3},{1,3,2,4},{1,3,4,2},{1,4,2,3},{1,4,3,2}
jettanan 0:8f12bfa12b59 259 ,{2,1,3,4},{2,1,4,3},{2,3,1,4},{2,3,4,1},{2,4,1,3},{2,4,3,1}
jettanan 0:8f12bfa12b59 260 ,{3,1,2,4},{3,1,4,2},{3,2,1,4},{3,2,4,1},{3,4,1,2},{3,4,2,1}
jettanan 0:8f12bfa12b59 261 ,{4,1,2,3},{4,1,3,2},{4,2,1,3},{4,2,3,1},{4,3,1,2},{4,3,2,1}};*/
jettanan 0:8f12bfa12b59 262 int checkp ;
jettanan 0:8f12bfa12b59 263 int checkst;
jettanan 0:8f12bfa12b59 264 while(1)
jettanan 0:8f12bfa12b59 265 {
jettanan 0:8f12bfa12b59 266
jettanan 0:8f12bfa12b59 267 if(limit1 ==1)
jettanan 0:8f12bfa12b59 268 {
jettanan 0:8f12bfa12b59 269 checkst = 1;
jettanan 0:8f12bfa12b59 270 break;
jettanan 0:8f12bfa12b59 271 }
jettanan 0:8f12bfa12b59 272 else if(limit2 ==1)
jettanan 0:8f12bfa12b59 273 {
jettanan 0:8f12bfa12b59 274 checkst = 2;
jettanan 0:8f12bfa12b59 275 break;
jettanan 0:8f12bfa12b59 276 }
jettanan 0:8f12bfa12b59 277 else if(limit3 ==1)
jettanan 0:8f12bfa12b59 278 {
jettanan 0:8f12bfa12b59 279 checkst = 3;
jettanan 0:8f12bfa12b59 280 break;
jettanan 0:8f12bfa12b59 281 }
jettanan 0:8f12bfa12b59 282 else if(limit4 ==1)
jettanan 0:8f12bfa12b59 283 {
jettanan 0:8f12bfa12b59 284 checkst = 4;
jettanan 0:8f12bfa12b59 285 break;
jettanan 0:8f12bfa12b59 286 }
jettanan 0:8f12bfa12b59 287
jettanan 0:8f12bfa12b59 288 }
jettanan 0:8f12bfa12b59 289 device.putc(checkst);
jettanan 0:8f12bfa12b59 290 wait(0.1);
jettanan 0:8f12bfa12b59 291 while(1)
jettanan 0:8f12bfa12b59 292 {
jettanan 0:8f12bfa12b59 293 if(limitB ==0)
jettanan 0:8f12bfa12b59 294 {
jettanan 0:8f12bfa12b59 295 MotorA(-1);
jettanan 0:8f12bfa12b59 296 }
jettanan 0:8f12bfa12b59 297 else
jettanan 0:8f12bfa12b59 298 {
jettanan 0:8f12bfa12b59 299 MotorA(0);
jettanan 0:8f12bfa12b59 300 break;
jettanan 0:8f12bfa12b59 301 }
jettanan 0:8f12bfa12b59 302 }
jettanan 0:8f12bfa12b59 303
jettanan 0:8f12bfa12b59 304 pc.printf("complete one tern checkp = %x\n",checkst);
jettanan 0:8f12bfa12b59 305 wait(2);
jettanan 0:8f12bfa12b59 306 device.putc(0x52);
jettanan 0:8f12bfa12b59 307 }
jettanan 0:8f12bfa12b59 308 }
jettanan 0:8f12bfa12b59 309
jettanan 0:8f12bfa12b59 310
jettanan 0:8f12bfa12b59 311
jettanan 0:8f12bfa12b59 312
jettanan 0:8f12bfa12b59 313 }
jettanan 0:8f12bfa12b59 314 }
jettanan 0:8f12bfa12b59 315 //Test Slave
jettanan 0:8f12bfa12b59 316 /*Serial device(D8,D2);
jettanan 0:8f12bfa12b59 317 Serial pc(SERIAL_TX,SERIAL_RX);
jettanan 0:8f12bfa12b59 318 uint16_t y=0;
jettanan 0:8f12bfa12b59 319
jettanan 0:8f12bfa12b59 320 int main()
jettanan 0:8f12bfa12b59 321 {
jettanan 0:8f12bfa12b59 322 while(1)
jettanan 0:8f12bfa12b59 323 {
jettanan 0:8f12bfa12b59 324 y = device.getc();
jettanan 0:8f12bfa12b59 325 pc.printf("%d\n",y);
jettanan 0:8f12bfa12b59 326 }
jettanan 0:8f12bfa12b59 327 }*/