Codigo para juego de aros ultrasonicos utilizando la tarjeta freedom KL25Z

Dependencies:   PinDetect_for_KL25Z TextLCD mbed

Committer:
rodomnz
Date:
Sun May 11 17:12:22 2014 +0000
Revision:
2:edb1ef4d407d
Parent:
1:39340990fa3f
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rodomnz 1:39340990fa3f 1 /*'Frisensor' code for a game that generates a sequence of leds (4), and checks if the user
rodomnz 1:39340990fa3f 2 is able to remember the sequence and reproduce it with ultrasonic sensor
rodomnz 1:39340990fa3f 3 Designed for the subject "informatica industrial",
rodomnz 1:39340990fa3f 4 in Tecnologico de Monterrey Campus Guadalajara
rodomnz 1:39340990fa3f 5 as of May 11 2014
rodomnz 1:39340990fa3f 6 v 1.1*/
rodomnz 2:edb1ef4d407d 7 /* Authors: Jorge Muñoz, Ismael Davila, Miguel Ortiz, Carlos Reyes
rodomnz 1:39340990fa3f 8 */
rodomnz 1:39340990fa3f 9
rodomnz 0:5ebac3c273c0 10 #include "mbed.h"
rodomnz 0:5ebac3c273c0 11 #include "TextLCD.h"
rodomnz 0:5ebac3c273c0 12 #include "Timer.h"
rodomnz 0:5ebac3c273c0 13 #include "PinDetect.h"
rodomnz 1:39340990fa3f 14 /*copyright of pindetect library
rodomnz 1:39340990fa3f 15 Copyright (c) 2010 Andy Kirkham
rodomnz 1:39340990fa3f 16
rodomnz 1:39340990fa3f 17 Permission is hereby granted, free of charge, to any person obtaining a copy
rodomnz 1:39340990fa3f 18 of this software and associated documentation files (the "Software"), to deal
rodomnz 1:39340990fa3f 19 in the Software without restriction, including without limitation the rights
rodomnz 1:39340990fa3f 20 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rodomnz 1:39340990fa3f 21 copies of the Software, and to permit persons to whom the Software is
rodomnz 1:39340990fa3f 22 furnished to do so, subject to the following conditions:
rodomnz 1:39340990fa3f 23 */
rodomnz 1:39340990fa3f 24
rodomnz 0:5ebac3c273c0 25 //LCD
rodomnz 0:5ebac3c273c0 26 TextLCD lcd( D2, D3, D4, D5,D6, D7, TextLCD::LCD16x2);
rodomnz 0:5ebac3c273c0 27 //leds
rodomnz 0:5ebac3c273c0 28 BusOut leds(PTC3, PTC4, PTC5, PTC6);
rodomnz 0:5ebac3c273c0 29 // Sensores
rodomnz 0:5ebac3c273c0 30 AnalogIn sensor_1(A0);
rodomnz 0:5ebac3c273c0 31 AnalogIn sensor_2(A2);
rodomnz 0:5ebac3c273c0 32 AnalogIn sensor_3(A4);
rodomnz 0:5ebac3c273c0 33 AnalogIn sensor_4(A1);
rodomnz 0:5ebac3c273c0 34 //pot for velocity of leds
rodomnz 0:5ebac3c273c0 35 AnalogIn pot(A3);
rodomnz 1:39340990fa3f 36 //variables fot sensors
rodomnz 0:5ebac3c273c0 37 float s1,s2,s3,s4;
rodomnz 1:39340990fa3f 38 //modes----------
rodomnz 0:5ebac3c273c0 39 int modo_lectura=0;
rodomnz 1:39340990fa3f 40 int gamemode=0;
rodomnz 1:39340990fa3f 41 int start_mode=0;
rodomnz 1:39340990fa3f 42 //array for points
rodomnz 0:5ebac3c273c0 43 int points_array[10];
rodomnz 0:5ebac3c273c0 44 int points_pointer=0;
rodomnz 0:5ebac3c273c0 45
rodomnz 1:39340990fa3f 46 //array for levels
rodomnz 1:39340990fa3f 47 int level_array[20];//max 19 levels
rodomnz 0:5ebac3c273c0 48 int level_selection=1;
rodomnz 1:39340990fa3f 49 //timer
rodomnz 0:5ebac3c273c0 50 Timer timer;
rodomnz 1:39340990fa3f 51
rodomnz 1:39340990fa3f 52 //
rodomnz 0:5ebac3c273c0 53 int contador;
rodomnz 1:39340990fa3f 54 int i;//its used for some "fors"
rodomnz 0:5ebac3c273c0 55 int show_time=500;
rodomnz 0:5ebac3c273c0 56 int scores_time=0;
rodomnz 0:5ebac3c273c0 57
rodomnz 1:39340990fa3f 58 // Button with antibouncing effect------
rodomnz 0:5ebac3c273c0 59 PinDetect start_button(PTA13);
rodomnz 0:5ebac3c273c0 60 /*PinDetect levelup(PTD5);
rodomnz 0:5ebac3c273c0 61 PinDetect leveldown(PTD0);*/
rodomnz 0:5ebac3c273c0 62 PinDetect show_scores(PTD2);
rodomnz 0:5ebac3c273c0 63
rodomnz 1:39340990fa3f 64 //functions-----------------------------
rodomnz 1:39340990fa3f 65 void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue);//function that displays the sequence of leds
rodomnz 1:39340990fa3f 66 int tiempo_apagado, leds_value;
rodomnz 1:39340990fa3f 67 void secuence_generator(int *ptr_level);//function that generates the sequence of the level
rodomnz 0:5ebac3c273c0 68
rodomnz 1:39340990fa3f 69 //functions fot buttons----------------
rodomnz 0:5ebac3c273c0 70 void start_reset();
rodomnz 0:5ebac3c273c0 71 void levelupfunction();
rodomnz 0:5ebac3c273c0 72 void leveldownfunction();
rodomnz 0:5ebac3c273c0 73 void show_scores_function();
rodomnz 0:5ebac3c273c0 74 int main()
rodomnz 0:5ebac3c273c0 75 {
rodomnz 0:5ebac3c273c0 76
rodomnz 0:5ebac3c273c0 77 contador=0;
rodomnz 0:5ebac3c273c0 78 secuence_generator(&level_selection);
rodomnz 0:5ebac3c273c0 79 timer.start();
rodomnz 1:39340990fa3f 80 //because we are using internal PullUp resistors we use deasserted
rodomnz 0:5ebac3c273c0 81 start_button.attach_deasserted(&start_reset);
rodomnz 0:5ebac3c273c0 82 /*leveldown.attach_deasserted(&levelupfunction);
rodomnz 0:5ebac3c273c0 83 levelup.attach_deasserted(&leveldownfunction);*/
rodomnz 0:5ebac3c273c0 84 show_scores.attach_deasserted(&show_scores_function);
rodomnz 1:39340990fa3f 85 //because we are using internal PullUp resistors we need to add this
rodomnz 0:5ebac3c273c0 86 start_button.mode(PullUp);
rodomnz 0:5ebac3c273c0 87 /*leveldown.mode(PullUp);
rodomnz 0:5ebac3c273c0 88 levelup.mode(PullUp);*/
rodomnz 0:5ebac3c273c0 89 show_scores.mode(PullUp);
rodomnz 0:5ebac3c273c0 90 start_button.setSampleFrequency();
rodomnz 0:5ebac3c273c0 91 show_scores.setSampleFrequency();
rodomnz 0:5ebac3c273c0 92 while(1) {
rodomnz 1:39340990fa3f 93 while(start_mode==0) {//mode Press Start---------------------------
rodomnz 0:5ebac3c273c0 94 if((timer.read_ms()>show_time+500) && (timer.read_ms()>scores_time+4000)) {
rodomnz 0:5ebac3c273c0 95 lcd.cls();
rodomnz 0:5ebac3c273c0 96 contador=0;
rodomnz 0:5ebac3c273c0 97 timer.reset();
rodomnz 0:5ebac3c273c0 98 lcd.printf("press start \nnivel %i \n", level_selection);
rodomnz 0:5ebac3c273c0 99 secuence_generator(&level_selection);
rodomnz 0:5ebac3c273c0 100 }
rodomnz 0:5ebac3c273c0 101 }
rodomnz 0:5ebac3c273c0 102 while((gamemode==0)&&(start_mode==1)) {//mode show led sequence----------------------
rodomnz 0:5ebac3c273c0 103 while(timer.read_ms()>(pot.read()*1000)+200) {
rodomnz 0:5ebac3c273c0 104 leds_function(&level_array[contador],&leds_value);
rodomnz 0:5ebac3c273c0 105 leds=leds_value;
rodomnz 0:5ebac3c273c0 106 contador++;
rodomnz 0:5ebac3c273c0 107 timer.reset();
rodomnz 0:5ebac3c273c0 108 tiempo_apagado=((pot.read()*1000)+200)/2;
rodomnz 0:5ebac3c273c0 109 }
rodomnz 0:5ebac3c273c0 110 while(timer.read_ms()>tiempo_apagado) {
rodomnz 0:5ebac3c273c0 111 tiempo_apagado=tiempo_apagado+(pot.read()*1000)+200;
rodomnz 0:5ebac3c273c0 112 leds=0;
rodomnz 0:5ebac3c273c0 113 if(contador==(level_selection+1)) {
rodomnz 0:5ebac3c273c0 114 gamemode=1;
rodomnz 0:5ebac3c273c0 115 leds=0;
rodomnz 0:5ebac3c273c0 116 contador=0;
rodomnz 0:5ebac3c273c0 117 lcd.cls();
rodomnz 0:5ebac3c273c0 118 }
rodomnz 0:5ebac3c273c0 119 }
rodomnz 0:5ebac3c273c0 120 }
rodomnz 1:39340990fa3f 121 while((gamemode==1)&&(start_mode==1)) {//gaming mode-------------------------------
rodomnz 0:5ebac3c273c0 122 while(modo_lectura==0) {
rodomnz 0:5ebac3c273c0 123 s1=sensor_1.read();
rodomnz 0:5ebac3c273c0 124 s2=sensor_2.read();
rodomnz 0:5ebac3c273c0 125 s3=sensor_3.read();
rodomnz 0:5ebac3c273c0 126 s4=sensor_4.read();
rodomnz 1:39340990fa3f 127 if((s1>0.05)&&(s2>0.05)&&(s3>0.05)&&(s4>0.05)) {// verify that there is nothing above leds
rodomnz 0:5ebac3c273c0 128 modo_lectura=1;
rodomnz 0:5ebac3c273c0 129 lcd.printf("Empieza!!\n");
rodomnz 0:5ebac3c273c0 130 }
rodomnz 0:5ebac3c273c0 131 }
rodomnz 0:5ebac3c273c0 132 while(modo_lectura==1) {
rodomnz 0:5ebac3c273c0 133 s1=sensor_1.read();
rodomnz 0:5ebac3c273c0 134 s2=sensor_2.read();
rodomnz 0:5ebac3c273c0 135 s3=sensor_3.read();
rodomnz 0:5ebac3c273c0 136 s4=sensor_4.read();
rodomnz 0:5ebac3c273c0 137 if(contador!=0) {
rodomnz 0:5ebac3c273c0 138 lcd.cls();
rodomnz 0:5ebac3c273c0 139 }
rodomnz 1:39340990fa3f 140 if(s1<0.017 && level_array[contador]==1) {// sensor 1 code----------------
rodomnz 0:5ebac3c273c0 141 modo_lectura=0;
rodomnz 0:5ebac3c273c0 142 contador++;
rodomnz 0:5ebac3c273c0 143 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 144 } else if(s1<0.017 && level_array[contador]!=1) {
rodomnz 0:5ebac3c273c0 145 lcd.printf("Perdiste \n");
rodomnz 0:5ebac3c273c0 146 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 147 if(points_pointer==9) {
rodomnz 0:5ebac3c273c0 148 for (i=0; i<9; i++) {
rodomnz 0:5ebac3c273c0 149 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 150 }
rodomnz 1:39340990fa3f 151 } else {
rodomnz 1:39340990fa3f 152 points_pointer++;
rodomnz 1:39340990fa3f 153 }
rodomnz 0:5ebac3c273c0 154 modo_lectura=0;
rodomnz 0:5ebac3c273c0 155 gamemode=0;
rodomnz 0:5ebac3c273c0 156 contador=0;
rodomnz 0:5ebac3c273c0 157 start_mode=0;
rodomnz 0:5ebac3c273c0 158 level_selection=1;
rodomnz 0:5ebac3c273c0 159 }
rodomnz 1:39340990fa3f 160 if(s2<0.017 && level_array[contador]==2) {// sensor 2 code-----------------------
rodomnz 0:5ebac3c273c0 161 modo_lectura=0;
rodomnz 0:5ebac3c273c0 162 contador++;
rodomnz 0:5ebac3c273c0 163 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 164 } else if(s2<0.017 && level_array[contador]!=2) {
rodomnz 0:5ebac3c273c0 165 lcd.printf("perdiste \n");
rodomnz 0:5ebac3c273c0 166 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 167 if(points_pointer==9) {
rodomnz 0:5ebac3c273c0 168 for (i=0; i<9; i++) {
rodomnz 0:5ebac3c273c0 169 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 170 }
rodomnz 1:39340990fa3f 171 } else {
rodomnz 1:39340990fa3f 172 points_pointer++;
rodomnz 1:39340990fa3f 173 }
rodomnz 0:5ebac3c273c0 174 modo_lectura=0;
rodomnz 0:5ebac3c273c0 175 gamemode=0;
rodomnz 0:5ebac3c273c0 176 contador=0;
rodomnz 0:5ebac3c273c0 177 start_mode=0;
rodomnz 0:5ebac3c273c0 178 level_selection=1;
rodomnz 0:5ebac3c273c0 179 }
rodomnz 1:39340990fa3f 180 if(s3<0.017 && level_array[contador]==3) {// sensor 3 code------------------------
rodomnz 0:5ebac3c273c0 181 modo_lectura=0;
rodomnz 0:5ebac3c273c0 182 contador++;
rodomnz 0:5ebac3c273c0 183 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 184 } else if(s3<0.017 && level_array[contador]!=3) {
rodomnz 0:5ebac3c273c0 185 lcd.printf("Perdiste \n");
rodomnz 0:5ebac3c273c0 186 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 187 if(points_pointer==9) {
rodomnz 0:5ebac3c273c0 188 for (i=0; i<9; i++) {
rodomnz 0:5ebac3c273c0 189 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 190 }
rodomnz 1:39340990fa3f 191 } else {
rodomnz 1:39340990fa3f 192 points_pointer++;
rodomnz 1:39340990fa3f 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 1:39340990fa3f 200 if(s4<0.017 && level_array[contador]==4) {// sensor 4 code-------------------------
rodomnz 0:5ebac3c273c0 201 modo_lectura=0;
rodomnz 0:5ebac3c273c0 202 contador++;
rodomnz 0:5ebac3c273c0 203 lcd.printf("good \n");
rodomnz 0:5ebac3c273c0 204 } else if(s4<0.017 && level_array[contador]!=4) {
rodomnz 0:5ebac3c273c0 205 lcd.printf("Perdiste \n");
rodomnz 0:5ebac3c273c0 206 show_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 207 if(points_pointer==7) {
rodomnz 0:5ebac3c273c0 208 for (i=0; i<7; i++) {
rodomnz 0:5ebac3c273c0 209 points_array[i]=points_array[i+1];
rodomnz 0:5ebac3c273c0 210 }
rodomnz 1:39340990fa3f 211 } else {
rodomnz 1:39340990fa3f 212 points_pointer++;
rodomnz 1:39340990fa3f 213 }
rodomnz 0:5ebac3c273c0 214 modo_lectura=0;
rodomnz 0:5ebac3c273c0 215 gamemode=0;
rodomnz 0:5ebac3c273c0 216 contador=0;
rodomnz 0:5ebac3c273c0 217 start_mode=0;
rodomnz 0:5ebac3c273c0 218 level_selection=1;
rodomnz 0:5ebac3c273c0 219 }
rodomnz 0:5ebac3c273c0 220 }
rodomnz 0:5ebac3c273c0 221 if(contador>level_selection) {//check if theres winners-------------------
rodomnz 0:5ebac3c273c0 222 lcd.printf("ganaste \n");
rodomnz 0:5ebac3c273c0 223 gamemode=0;
rodomnz 0:5ebac3c273c0 224 points_array[points_pointer]=points_array[points_pointer] + level_selection*2;
rodomnz 0:5ebac3c273c0 225 lcd.printf("puntos %i",points_array[points_pointer] );
rodomnz 1:39340990fa3f 226 wait(3);//just to keep the word "ganaste" in the display for some time
rodomnz 0:5ebac3c273c0 227 level_selection++;
rodomnz 0:5ebac3c273c0 228 secuence_generator(&level_selection);
rodomnz 0:5ebac3c273c0 229 contador=0;
rodomnz 0:5ebac3c273c0 230 }
rodomnz 0:5ebac3c273c0 231 }
rodomnz 0:5ebac3c273c0 232 }//end_of_while(1)
rodomnz 0:5ebac3c273c0 233 }//end_of_int_main
rodomnz 1:39340990fa3f 234 //functions
rodomnz 1:39340990fa3f 235 //function to generate sequences-------------------------
rodomnz 0:5ebac3c273c0 236 void secuence_generator(int *ptr_level)
rodomnz 0:5ebac3c273c0 237 {
rodomnz 0:5ebac3c273c0 238 int j;
rodomnz 1:39340990fa3f 239
rodomnz 0:5ebac3c273c0 240 srand (time(NULL));
rodomnz 0:5ebac3c273c0 241 for (j=0; j<=(*ptr_level); j++) {
rodomnz 0:5ebac3c273c0 242 level_array[j]=rand()%4+1;
rodomnz 0:5ebac3c273c0 243 }
rodomnz 0:5ebac3c273c0 244 }
rodomnz 1:39340990fa3f 245 //function to show the number in leds-------------------
rodomnz 0:5ebac3c273c0 246 void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue)
rodomnz 0:5ebac3c273c0 247 {
rodomnz 0:5ebac3c273c0 248 switch(*ptr_randomvalue) {
rodomnz 0:5ebac3c273c0 249 case 0:
rodomnz 0:5ebac3c273c0 250 *ptr_busoutvalue=0;
rodomnz 0:5ebac3c273c0 251 break;
rodomnz 0:5ebac3c273c0 252 case 1:
rodomnz 0:5ebac3c273c0 253 *ptr_busoutvalue=1;
rodomnz 0:5ebac3c273c0 254 break;
rodomnz 0:5ebac3c273c0 255 case 2:
rodomnz 0:5ebac3c273c0 256 *ptr_busoutvalue=2;
rodomnz 0:5ebac3c273c0 257 break;
rodomnz 0:5ebac3c273c0 258 case 3:
rodomnz 0:5ebac3c273c0 259 *ptr_busoutvalue=4;
rodomnz 0:5ebac3c273c0 260 break;
rodomnz 0:5ebac3c273c0 261 case 4:
rodomnz 0:5ebac3c273c0 262 *ptr_busoutvalue=8;
rodomnz 0:5ebac3c273c0 263 break;
rodomnz 0:5ebac3c273c0 264 }
rodomnz 0:5ebac3c273c0 265 }
rodomnz 0:5ebac3c273c0 266 void levelupfunction()
rodomnz 0:5ebac3c273c0 267 {
rodomnz 0:5ebac3c273c0 268 level_selection++;
rodomnz 0:5ebac3c273c0 269 start_mode=0;
rodomnz 0:5ebac3c273c0 270 }
rodomnz 0:5ebac3c273c0 271 void leveldownfunction()
rodomnz 0:5ebac3c273c0 272 {
rodomnz 0:5ebac3c273c0 273 level_selection--;
rodomnz 0:5ebac3c273c0 274 start_mode=0;
rodomnz 0:5ebac3c273c0 275 }
rodomnz 0:5ebac3c273c0 276 void start_reset()
rodomnz 0:5ebac3c273c0 277 {
rodomnz 0:5ebac3c273c0 278 start_mode=!start_mode;
rodomnz 0:5ebac3c273c0 279 contador=0;
rodomnz 0:5ebac3c273c0 280 }
rodomnz 1:39340990fa3f 281 void show_scores_function()
rodomnz 1:39340990fa3f 282 {
rodomnz 0:5ebac3c273c0 283 lcd.cls();
rodomnz 1:39340990fa3f 284 for (i=0; i<8; i++) {
rodomnz 0:5ebac3c273c0 285 lcd.printf("%i:%i,", i+1,points_array[i]);
rodomnz 0:5ebac3c273c0 286 scores_time=timer.read_ms();
rodomnz 0:5ebac3c273c0 287 }
rodomnz 0:5ebac3c273c0 288 start_mode=0;
rodomnz 1:39340990fa3f 289 }