Codigo para juego de aros ultrasonicos utilizando la tarjeta freedom KL25Z

Dependencies:   PinDetect_for_KL25Z TextLCD mbed

Committer:
rodomnz
Date:
Sun May 11 04:32:15 2014 +0000
Revision:
0:5ebac3c273c0
Child:
1:39340990fa3f
Codigo Para Juego de Aros Ultrasonicos Utilizando la Tarjeta Freedom KL25Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rodomnz 0:5ebac3c273c0 1 #include "mbed.h"
rodomnz 0:5ebac3c273c0 2 #include "TextLCD.h"
rodomnz 0:5ebac3c273c0 3 #include "Timer.h"
rodomnz 0:5ebac3c273c0 4 #include "PinDetect.h"
rodomnz 0:5ebac3c273c0 5 Serial pc(USBTX, USBRX);
rodomnz 0:5ebac3c273c0 6 //LCD
rodomnz 0:5ebac3c273c0 7 TextLCD lcd( D2, D3, D4, D5,D6, D7, TextLCD::LCD16x2);
rodomnz 0:5ebac3c273c0 8
rodomnz 0:5ebac3c273c0 9 //leds
rodomnz 0:5ebac3c273c0 10 BusOut leds(PTC3, PTC4, PTC5, PTC6);
rodomnz 0:5ebac3c273c0 11
rodomnz 0:5ebac3c273c0 12 // Sensores
rodomnz 0:5ebac3c273c0 13
rodomnz 0:5ebac3c273c0 14 AnalogIn sensor_1(A0);
rodomnz 0:5ebac3c273c0 15 AnalogIn sensor_2(A2);
rodomnz 0:5ebac3c273c0 16 AnalogIn sensor_3(A4);
rodomnz 0:5ebac3c273c0 17 AnalogIn sensor_4(A1);
rodomnz 0:5ebac3c273c0 18
rodomnz 0:5ebac3c273c0 19 //pot for velocity of leds
rodomnz 0:5ebac3c273c0 20 AnalogIn pot(A3);
rodomnz 0:5ebac3c273c0 21
rodomnz 0:5ebac3c273c0 22 float s1,s2,s3,s4;
rodomnz 0:5ebac3c273c0 23 int modo_lectura=0;
rodomnz 0:5ebac3c273c0 24
rodomnz 0:5ebac3c273c0 25 int points_array[10];
rodomnz 0:5ebac3c273c0 26 int points_pointer=0;
rodomnz 0:5ebac3c273c0 27
rodomnz 0:5ebac3c273c0 28 //level_array
rodomnz 0:5ebac3c273c0 29 int level_array[20];
rodomnz 0:5ebac3c273c0 30 int level_selection=1;
rodomnz 0:5ebac3c273c0 31
rodomnz 0:5ebac3c273c0 32 Timer timer;
rodomnz 0:5ebac3c273c0 33 int gamemode=0;
rodomnz 0:5ebac3c273c0 34 int contador;
rodomnz 0:5ebac3c273c0 35 int start_mode=0;
rodomnz 0:5ebac3c273c0 36 int i;
rodomnz 0:5ebac3c273c0 37 int show_time=500;
rodomnz 0:5ebac3c273c0 38 int scores_time=0;
rodomnz 0:5ebac3c273c0 39
rodomnz 0:5ebac3c273c0 40 // Button with antibouncing effect------------------------
rodomnz 0:5ebac3c273c0 41 PinDetect start_button(PTA13);
rodomnz 0:5ebac3c273c0 42 /*PinDetect levelup(PTD5);
rodomnz 0:5ebac3c273c0 43 PinDetect leveldown(PTD0);*/
rodomnz 0:5ebac3c273c0 44 PinDetect show_scores(PTD2);
rodomnz 0:5ebac3c273c0 45
rodomnz 0:5ebac3c273c0 46
rodomnz 0:5ebac3c273c0 47 //functions-----------------------------
rodomnz 0:5ebac3c273c0 48 void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue);
rodomnz 0:5ebac3c273c0 49 int tiempo_apagado, leds_value;
rodomnz 0:5ebac3c273c0 50 void secuence_generator(int *ptr_level);
rodomnz 0:5ebac3c273c0 51 void start_reset();
rodomnz 0:5ebac3c273c0 52 void levelupfunction();
rodomnz 0:5ebac3c273c0 53 void leveldownfunction();
rodomnz 0:5ebac3c273c0 54 void show_scores_function();
rodomnz 0:5ebac3c273c0 55 int main()
rodomnz 0:5ebac3c273c0 56 {
rodomnz 0:5ebac3c273c0 57
rodomnz 0:5ebac3c273c0 58 contador=0;
rodomnz 0:5ebac3c273c0 59 secuence_generator(&level_selection);
rodomnz 0:5ebac3c273c0 60 timer.start();
rodomnz 0:5ebac3c273c0 61
rodomnz 0:5ebac3c273c0 62 start_button.attach_deasserted(&start_reset);
rodomnz 0:5ebac3c273c0 63 /*leveldown.attach_deasserted(&levelupfunction);
rodomnz 0:5ebac3c273c0 64 levelup.attach_deasserted(&leveldownfunction);*/
rodomnz 0:5ebac3c273c0 65 show_scores.attach_deasserted(&show_scores_function);
rodomnz 0:5ebac3c273c0 66 start_button.mode(PullUp);
rodomnz 0:5ebac3c273c0 67 /*leveldown.mode(PullUp);
rodomnz 0:5ebac3c273c0 68 levelup.mode(PullUp);*/
rodomnz 0:5ebac3c273c0 69 show_scores.mode(PullUp);
rodomnz 0:5ebac3c273c0 70 start_button.setSampleFrequency();
rodomnz 0:5ebac3c273c0 71 show_scores.setSampleFrequency();
rodomnz 0:5ebac3c273c0 72 while(1) {
rodomnz 0:5ebac3c273c0 73 while(start_mode==0) {//mode Press Start...........................
rodomnz 0:5ebac3c273c0 74 if((timer.read_ms()>show_time+500) && (timer.read_ms()>scores_time+4000)) {
rodomnz 0:5ebac3c273c0 75 lcd.cls();
rodomnz 0:5ebac3c273c0 76 contador=0;
rodomnz 0:5ebac3c273c0 77 /*secuence_generator(&level_selection);*/
rodomnz 0:5ebac3c273c0 78 timer.reset();
rodomnz 0:5ebac3c273c0 79 lcd.printf("press start \nnivel %i \n", level_selection);
rodomnz 0:5ebac3c273c0 80 secuence_generator(&level_selection);
rodomnz 0:5ebac3c273c0 81 //(pot.read()*1000)+200= (pot.read()*1000)+200;
rodomnz 0:5ebac3c273c0 82 }
rodomnz 0:5ebac3c273c0 83 }
rodomnz 0:5ebac3c273c0 84 while((gamemode==0)&&(start_mode==1)) {//mode show led sequence----------------------
rodomnz 0:5ebac3c273c0 85
rodomnz 0:5ebac3c273c0 86
rodomnz 0:5ebac3c273c0 87 while(timer.read_ms()>(pot.read()*1000)+200) {
rodomnz 0:5ebac3c273c0 88 leds_function(&level_array[contador],&leds_value);
rodomnz 0:5ebac3c273c0 89 leds=leds_value;
rodomnz 0:5ebac3c273c0 90 contador++;
rodomnz 0:5ebac3c273c0 91 timer.reset();
rodomnz 0:5ebac3c273c0 92 tiempo_apagado=((pot.read()*1000)+200)/2;
rodomnz 0:5ebac3c273c0 93 }
rodomnz 0:5ebac3c273c0 94 while(timer.read_ms()>tiempo_apagado) {
rodomnz 0:5ebac3c273c0 95 tiempo_apagado=tiempo_apagado+(pot.read()*1000)+200;
rodomnz 0:5ebac3c273c0 96 leds=0;
rodomnz 0:5ebac3c273c0 97 if(contador==(level_selection+1)) {
rodomnz 0:5ebac3c273c0 98 gamemode=1;
rodomnz 0:5ebac3c273c0 99 leds=0;
rodomnz 0:5ebac3c273c0 100 contador=0;
rodomnz 0:5ebac3c273c0 101 lcd.cls();
rodomnz 0:5ebac3c273c0 102 }
rodomnz 0:5ebac3c273c0 103 }
rodomnz 0:5ebac3c273c0 104 }
rodomnz 0:5ebac3c273c0 105 while((gamemode==1)&&(start_mode==1)) {
rodomnz 0:5ebac3c273c0 106 /*s1=sensor_1.read();
rodomnz 0:5ebac3c273c0 107 s2=sensor_2.read();
rodomnz 0:5ebac3c273c0 108 s3=sensor_3.read();
rodomnz 0:5ebac3c273c0 109 s4=sensor_4.read();*/
rodomnz 0:5ebac3c273c0 110 /*pc.printf("Sensor 1 %f \n", s1);
rodomnz 0:5ebac3c273c0 111 pc.printf("Sensor 2 %f \n", s2);
rodomnz 0:5ebac3c273c0 112 pc.printf("Sensor 3 %f \n", s3);
rodomnz 0:5ebac3c273c0 113 pc.printf("Sensor 4 %f \n\n", s4);*/
rodomnz 0:5ebac3c273c0 114 while(modo_lectura==0) {
rodomnz 0:5ebac3c273c0 115 s1=sensor_1.read();
rodomnz 0:5ebac3c273c0 116 s2=sensor_2.read();
rodomnz 0:5ebac3c273c0 117 s3=sensor_3.read();
rodomnz 0:5ebac3c273c0 118 s4=sensor_4.read();
rodomnz 0:5ebac3c273c0 119 if((s1>0.05)&&(s2>0.05)&&(s3>0.05)&&(s4>0.05)) {// verify that theres nothing above leds
rodomnz 0:5ebac3c273c0 120 modo_lectura=1;
rodomnz 0:5ebac3c273c0 121 lcd.printf("Empieza!!\n");
rodomnz 0:5ebac3c273c0 122 }
rodomnz 0:5ebac3c273c0 123 }
rodomnz 0:5ebac3c273c0 124 while(modo_lectura==1) {
rodomnz 0:5ebac3c273c0 125 s1=sensor_1.read();
rodomnz 0:5ebac3c273c0 126 s2=sensor_2.read();
rodomnz 0:5ebac3c273c0 127 s3=sensor_3.read();
rodomnz 0:5ebac3c273c0 128 s4=sensor_4.read();
rodomnz 0:5ebac3c273c0 129 if(contador!=0) {
rodomnz 0:5ebac3c273c0 130 lcd.cls();
rodomnz 0:5ebac3c273c0 131 }
rodomnz 0:5ebac3c273c0 132 if(s1<0.017 && level_array[contador]==1) {// sensor 1 code
rodomnz 0:5ebac3c273c0 133
rodomnz 0:5ebac3c273c0 134 modo_lectura=0;
rodomnz 0:5ebac3c273c0 135 contador++;
rodomnz 0:5ebac3c273c0 136 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 137 } else if(s1<0.017 && level_array[contador]!=1) {
rodomnz 0:5ebac3c273c0 138 lcd.printf("Perdiste \n");
rodomnz 0:5ebac3c273c0 139 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 140 if(points_pointer==9) {
rodomnz 0:5ebac3c273c0 141 for (i=0; i<9; i++) {
rodomnz 0:5ebac3c273c0 142 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 143 }
rodomnz 0:5ebac3c273c0 144 }
rodomnz 0:5ebac3c273c0 145 else{
rodomnz 0:5ebac3c273c0 146 points_pointer++;
rodomnz 0:5ebac3c273c0 147 }
rodomnz 0:5ebac3c273c0 148
rodomnz 0:5ebac3c273c0 149 modo_lectura=0;
rodomnz 0:5ebac3c273c0 150 gamemode=0;
rodomnz 0:5ebac3c273c0 151 contador=0;
rodomnz 0:5ebac3c273c0 152 start_mode=0;
rodomnz 0:5ebac3c273c0 153 level_selection=1;
rodomnz 0:5ebac3c273c0 154 }
rodomnz 0:5ebac3c273c0 155 if(s2<0.017 && level_array[contador]==2) {// sensor 2 code
rodomnz 0:5ebac3c273c0 156 modo_lectura=0;
rodomnz 0:5ebac3c273c0 157 contador++;
rodomnz 0:5ebac3c273c0 158 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 159 } else if(s2<0.017 && level_array[contador]!=2) {
rodomnz 0:5ebac3c273c0 160 lcd.printf("perdiste \n");
rodomnz 0:5ebac3c273c0 161 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 162 if(points_pointer==9) {
rodomnz 0:5ebac3c273c0 163 for (i=0; i<9; i++) {
rodomnz 0:5ebac3c273c0 164 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 165 }
rodomnz 0:5ebac3c273c0 166 }
rodomnz 0:5ebac3c273c0 167 else{
rodomnz 0:5ebac3c273c0 168 points_pointer++;
rodomnz 0:5ebac3c273c0 169 }
rodomnz 0:5ebac3c273c0 170
rodomnz 0:5ebac3c273c0 171 modo_lectura=0;
rodomnz 0:5ebac3c273c0 172 gamemode=0;
rodomnz 0:5ebac3c273c0 173 contador=0;
rodomnz 0:5ebac3c273c0 174 start_mode=0;
rodomnz 0:5ebac3c273c0 175 level_selection=1;
rodomnz 0:5ebac3c273c0 176 }
rodomnz 0:5ebac3c273c0 177 if(s3<0.017 && level_array[contador]==3) {// sensor 3 code
rodomnz 0:5ebac3c273c0 178
rodomnz 0:5ebac3c273c0 179 modo_lectura=0;
rodomnz 0:5ebac3c273c0 180 contador++;
rodomnz 0:5ebac3c273c0 181 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 182 } else if(s3<0.017 && level_array[contador]!=3) {
rodomnz 0:5ebac3c273c0 183 lcd.printf("Perdiste \n");
rodomnz 0:5ebac3c273c0 184 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 185 if(points_pointer==9) {
rodomnz 0:5ebac3c273c0 186 for (i=0; i<9; i++) {
rodomnz 0:5ebac3c273c0 187 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 188 }
rodomnz 0:5ebac3c273c0 189 }
rodomnz 0:5ebac3c273c0 190 else{
rodomnz 0:5ebac3c273c0 191 points_pointer++;
rodomnz 0:5ebac3c273c0 192 }
rodomnz 0:5ebac3c273c0 193
rodomnz 0:5ebac3c273c0 194 start_mode=0;
rodomnz 0:5ebac3c273c0 195 modo_lectura=0;
rodomnz 0:5ebac3c273c0 196 gamemode=0;
rodomnz 0:5ebac3c273c0 197 contador=0;
rodomnz 0:5ebac3c273c0 198 level_selection=1;
rodomnz 0:5ebac3c273c0 199
rodomnz 0:5ebac3c273c0 200 }
rodomnz 0:5ebac3c273c0 201 if(s4<0.017 && level_array[contador]==4) {// sensor 4 code
rodomnz 0:5ebac3c273c0 202
rodomnz 0:5ebac3c273c0 203 modo_lectura=0;
rodomnz 0:5ebac3c273c0 204 contador++;
rodomnz 0:5ebac3c273c0 205 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 206 } else if(s4<0.017 && level_array[contador]!=4) {
rodomnz 0:5ebac3c273c0 207 lcd.printf("Perdiste \n");
rodomnz 0:5ebac3c273c0 208 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 209 if(points_pointer==7) {
rodomnz 0:5ebac3c273c0 210 for (i=0; i<7; i++) {
rodomnz 0:5ebac3c273c0 211 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 212 }
rodomnz 0:5ebac3c273c0 213 }
rodomnz 0:5ebac3c273c0 214 else{
rodomnz 0:5ebac3c273c0 215 points_pointer++;
rodomnz 0:5ebac3c273c0 216 }
rodomnz 0:5ebac3c273c0 217
rodomnz 0:5ebac3c273c0 218 modo_lectura=0;
rodomnz 0:5ebac3c273c0 219 gamemode=0;
rodomnz 0:5ebac3c273c0 220 contador=0;
rodomnz 0:5ebac3c273c0 221 start_mode=0;
rodomnz 0:5ebac3c273c0 222 level_selection=1;
rodomnz 0:5ebac3c273c0 223
rodomnz 0:5ebac3c273c0 224 }
rodomnz 0:5ebac3c273c0 225 }
rodomnz 0:5ebac3c273c0 226 if(contador>level_selection) {//check if theres winners-------------------
rodomnz 0:5ebac3c273c0 227 lcd.printf("ganaste \n");
rodomnz 0:5ebac3c273c0 228
rodomnz 0:5ebac3c273c0 229 gamemode=0;
rodomnz 0:5ebac3c273c0 230 points_array[points_pointer]=points_array[points_pointer] + level_selection*2;
rodomnz 0:5ebac3c273c0 231 lcd.printf("puntos %i",points_array[points_pointer] );
rodomnz 0:5ebac3c273c0 232 wait(3);
rodomnz 0:5ebac3c273c0 233 level_selection++;
rodomnz 0:5ebac3c273c0 234 secuence_generator(&level_selection);
rodomnz 0:5ebac3c273c0 235 contador=0;
rodomnz 0:5ebac3c273c0 236
rodomnz 0:5ebac3c273c0 237 }
rodomnz 0:5ebac3c273c0 238 }
rodomnz 0:5ebac3c273c0 239 }//end_of_while(1)
rodomnz 0:5ebac3c273c0 240 }//end_of_int_main
rodomnz 0:5ebac3c273c0 241
rodomnz 0:5ebac3c273c0 242
rodomnz 0:5ebac3c273c0 243 //funciones
rodomnz 0:5ebac3c273c0 244 void secuence_generator(int *ptr_level)
rodomnz 0:5ebac3c273c0 245 {
rodomnz 0:5ebac3c273c0 246 int j;
rodomnz 0:5ebac3c273c0 247
rodomnz 0:5ebac3c273c0 248 srand (time(NULL));
rodomnz 0:5ebac3c273c0 249 for (j=0; j<=(*ptr_level); j++) {
rodomnz 0:5ebac3c273c0 250 level_array[j]=rand()%4+1;
rodomnz 0:5ebac3c273c0 251 pc.printf(" %i \n",level_array[j]);
rodomnz 0:5ebac3c273c0 252 }
rodomnz 0:5ebac3c273c0 253 }
rodomnz 0:5ebac3c273c0 254
rodomnz 0:5ebac3c273c0 255 void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue)
rodomnz 0:5ebac3c273c0 256 {
rodomnz 0:5ebac3c273c0 257 switch(*ptr_randomvalue) {
rodomnz 0:5ebac3c273c0 258 case 0:
rodomnz 0:5ebac3c273c0 259 *ptr_busoutvalue=0;
rodomnz 0:5ebac3c273c0 260 break;
rodomnz 0:5ebac3c273c0 261 case 1:
rodomnz 0:5ebac3c273c0 262 *ptr_busoutvalue=1;
rodomnz 0:5ebac3c273c0 263 break;
rodomnz 0:5ebac3c273c0 264 case 2:
rodomnz 0:5ebac3c273c0 265 *ptr_busoutvalue=2;
rodomnz 0:5ebac3c273c0 266 break;
rodomnz 0:5ebac3c273c0 267 case 3:
rodomnz 0:5ebac3c273c0 268 *ptr_busoutvalue=4;
rodomnz 0:5ebac3c273c0 269 break;
rodomnz 0:5ebac3c273c0 270 case 4:
rodomnz 0:5ebac3c273c0 271 *ptr_busoutvalue=8;
rodomnz 0:5ebac3c273c0 272 break;
rodomnz 0:5ebac3c273c0 273 }
rodomnz 0:5ebac3c273c0 274 }
rodomnz 0:5ebac3c273c0 275
rodomnz 0:5ebac3c273c0 276 void levelupfunction()
rodomnz 0:5ebac3c273c0 277 {
rodomnz 0:5ebac3c273c0 278
rodomnz 0:5ebac3c273c0 279 level_selection++;
rodomnz 0:5ebac3c273c0 280 start_mode=0;
rodomnz 0:5ebac3c273c0 281
rodomnz 0:5ebac3c273c0 282 }
rodomnz 0:5ebac3c273c0 283 void leveldownfunction()
rodomnz 0:5ebac3c273c0 284 {
rodomnz 0:5ebac3c273c0 285
rodomnz 0:5ebac3c273c0 286 level_selection--;
rodomnz 0:5ebac3c273c0 287 start_mode=0;
rodomnz 0:5ebac3c273c0 288
rodomnz 0:5ebac3c273c0 289 }
rodomnz 0:5ebac3c273c0 290 void start_reset()
rodomnz 0:5ebac3c273c0 291 {
rodomnz 0:5ebac3c273c0 292
rodomnz 0:5ebac3c273c0 293 start_mode=!start_mode;
rodomnz 0:5ebac3c273c0 294 contador=0;
rodomnz 0:5ebac3c273c0 295
rodomnz 0:5ebac3c273c0 296 }
rodomnz 0:5ebac3c273c0 297 void show_scores_function(){
rodomnz 0:5ebac3c273c0 298 lcd.cls();
rodomnz 0:5ebac3c273c0 299 for (i=0;i<8;i++){
rodomnz 0:5ebac3c273c0 300 lcd.printf("%i:%i,", i+1,points_array[i]);
rodomnz 0:5ebac3c273c0 301 scores_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 302
rodomnz 0:5ebac3c273c0 303
rodomnz 0:5ebac3c273c0 304 }
rodomnz 0:5ebac3c273c0 305 start_mode=0;
rodomnz 0:5ebac3c273c0 306 }