Jorge Rodolfo Muñoz / Mbed 2 deprecated Programa_juego_info

Dependencies:   PinDetect_for_KL25Z TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*'Frisensor' code for a game that generates a sequence of leds (4), and checks if the user
00002 is able to remember the sequence and reproduce it with ultrasonic sensor
00003 Designed for the subject "informatica industrial", 
00004 in Tecnologico de Monterrey Campus Guadalajara
00005 as of May 11 2014
00006 v 1.1*/
00007 /* Authors: Jorge Muñoz, Ismael Davila, Miguel Ortiz, Carlos Reyes
00008 */
00009 
00010 #include "mbed.h"
00011 #include "TextLCD.h"
00012 #include "Timer.h"
00013 #include "PinDetect.h"
00014 /*copyright of pindetect library
00015 Copyright (c) 2010 Andy Kirkham
00016  
00017     Permission is hereby granted, free of charge, to any person obtaining a copy
00018     of this software and associated documentation files (the "Software"), to deal
00019     in the Software without restriction, including without limitation the rights
00020     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00021     copies of the Software, and to permit persons to whom the Software is
00022     furnished to do so, subject to the following conditions:
00023 */
00024 
00025 //LCD
00026 TextLCD lcd( D2, D3, D4, D5,D6, D7, TextLCD::LCD16x2);
00027 //leds
00028 BusOut leds(PTC3, PTC4, PTC5, PTC6);
00029 // Sensores
00030 AnalogIn sensor_1(A0);
00031 AnalogIn sensor_2(A2);
00032 AnalogIn sensor_3(A4);
00033 AnalogIn sensor_4(A1);
00034 //pot for velocity of leds
00035 AnalogIn pot(A3);
00036 //variables fot sensors
00037 float s1,s2,s3,s4;
00038 //modes----------
00039 int modo_lectura=0;
00040 int gamemode=0;
00041 int start_mode=0;
00042 //array for points
00043 int points_array[10];
00044 int points_pointer=0;
00045 
00046 //array for levels
00047 int level_array[20];//max 19 levels
00048 int level_selection=1;
00049 //timer
00050 Timer timer;
00051 
00052 //
00053 int contador;
00054 int i;//its used for some "fors"
00055 int show_time=500;
00056 int scores_time=0;
00057 
00058 // Button with antibouncing effect------
00059 PinDetect start_button(PTA13);
00060 /*PinDetect levelup(PTD5);
00061 PinDetect leveldown(PTD0);*/
00062 PinDetect show_scores(PTD2);
00063 
00064 //functions-----------------------------
00065 void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue);//function that displays the sequence of leds
00066 int tiempo_apagado, leds_value;
00067 void secuence_generator(int *ptr_level);//function that generates the sequence of the level
00068 
00069 //functions fot buttons----------------
00070 void start_reset();
00071 void levelupfunction();
00072 void leveldownfunction();
00073 void show_scores_function();
00074 int main()
00075 {
00076 
00077     contador=0;
00078     secuence_generator(&level_selection);
00079     timer.start();
00080     //because we are using internal PullUp resistors we use deasserted
00081     start_button.attach_deasserted(&start_reset);
00082     /*leveldown.attach_deasserted(&levelupfunction);
00083     levelup.attach_deasserted(&leveldownfunction);*/
00084     show_scores.attach_deasserted(&show_scores_function);
00085     //because we are using internal PullUp resistors we need to add this
00086     start_button.mode(PullUp);
00087     /*leveldown.mode(PullUp);
00088     levelup.mode(PullUp);*/
00089     show_scores.mode(PullUp);
00090     start_button.setSampleFrequency();
00091     show_scores.setSampleFrequency();
00092     while(1) {
00093         while(start_mode==0) {//mode Press Start---------------------------
00094             if((timer.read_ms()>show_time+500) && (timer.read_ms()>scores_time+4000)) {
00095                 lcd.cls();
00096                 contador=0;
00097                 timer.reset();
00098                 lcd.printf("press start \nnivel %i \n", level_selection);
00099                 secuence_generator(&level_selection);
00100             }
00101         }
00102         while((gamemode==0)&&(start_mode==1)) {//mode show led sequence----------------------
00103             while(timer.read_ms()>(pot.read()*1000)+200) {
00104                 leds_function(&level_array[contador],&leds_value);
00105                 leds=leds_value;
00106                 contador++;
00107                 timer.reset();
00108                 tiempo_apagado=((pot.read()*1000)+200)/2;
00109             }
00110             while(timer.read_ms()>tiempo_apagado) {
00111                 tiempo_apagado=tiempo_apagado+(pot.read()*1000)+200;
00112                 leds=0;
00113                 if(contador==(level_selection+1)) {
00114                     gamemode=1;
00115                     leds=0;
00116                     contador=0;
00117                     lcd.cls();
00118                 }
00119             }
00120         }
00121         while((gamemode==1)&&(start_mode==1)) {//gaming mode-------------------------------
00122             while(modo_lectura==0) {
00123                 s1=sensor_1.read();
00124                 s2=sensor_2.read();
00125                 s3=sensor_3.read();
00126                 s4=sensor_4.read();
00127                 if((s1>0.05)&&(s2>0.05)&&(s3>0.05)&&(s4>0.05)) {// verify that there is nothing above leds
00128                     modo_lectura=1;
00129                     lcd.printf("Empieza!!\n");
00130                 }
00131             }
00132             while(modo_lectura==1) {
00133                 s1=sensor_1.read();
00134                 s2=sensor_2.read();
00135                 s3=sensor_3.read();
00136                 s4=sensor_4.read();
00137                 if(contador!=0) {
00138                     lcd.cls();
00139                 }
00140                 if(s1<0.017 && level_array[contador]==1) {// sensor 1 code----------------
00141                     modo_lectura=0;
00142                     contador++;
00143                     lcd.printf("good \n");
00144                 } else if(s1<0.017 && level_array[contador]!=1) {
00145                     lcd.printf("Perdiste \n");
00146                     show_time=timer.read_ms();
00147                     if(points_pointer==9) {
00148                         for (i=0; i<9; i++) {
00149                             points_array[i]=points_array[i+1];
00150                         }
00151                     } else {
00152                         points_pointer++;
00153                     }
00154                     modo_lectura=0;
00155                     gamemode=0;
00156                     contador=0;
00157                     start_mode=0;
00158                     level_selection=1;
00159                 }
00160                 if(s2<0.017 && level_array[contador]==2) {// sensor 2 code-----------------------
00161                     modo_lectura=0;
00162                     contador++;
00163                     lcd.printf("good \n");
00164                 } else if(s2<0.017 && level_array[contador]!=2) {
00165                     lcd.printf("perdiste \n");
00166                     show_time=timer.read_ms();
00167                     if(points_pointer==9) {
00168                         for (i=0; i<9; i++) {
00169                             points_array[i]=points_array[i+1];
00170                         }
00171                     } else {
00172                         points_pointer++;
00173                     }
00174                     modo_lectura=0;
00175                     gamemode=0;
00176                     contador=0;
00177                     start_mode=0;
00178                     level_selection=1;
00179                 }
00180                 if(s3<0.017 && level_array[contador]==3) {// sensor 3 code------------------------
00181                     modo_lectura=0;
00182                     contador++;
00183                     lcd.printf("good \n");
00184                 } else if(s3<0.017 && level_array[contador]!=3) {
00185                     lcd.printf("Perdiste \n");
00186                     show_time=timer.read_ms();
00187                     if(points_pointer==9) {
00188                         for (i=0; i<9; i++) {
00189                             points_array[i]=points_array[i+1];
00190                         }
00191                     } else {
00192                         points_pointer++;
00193                     }
00194                     start_mode=0;
00195                     modo_lectura=0;
00196                     gamemode=0;
00197                     contador=0;
00198                     level_selection=1;
00199                 }
00200                 if(s4<0.017 && level_array[contador]==4) {// sensor 4 code-------------------------
00201                     modo_lectura=0;
00202                     contador++;
00203                     lcd.printf("good \n");
00204                 } else if(s4<0.017 && level_array[contador]!=4) {
00205                     lcd.printf("Perdiste \n");
00206                     show_time=timer.read_ms();
00207                     if(points_pointer==7) {
00208                         for (i=0; i<7; i++) {
00209                             points_array[i]=points_array[i+1];
00210                         }
00211                     } else {
00212                         points_pointer++;
00213                     }
00214                     modo_lectura=0;
00215                     gamemode=0;
00216                     contador=0;
00217                     start_mode=0;
00218                     level_selection=1;
00219                 }
00220             }
00221             if(contador>level_selection) {//check if theres winners-------------------
00222                 lcd.printf("ganaste \n");
00223                 gamemode=0;
00224                 points_array[points_pointer]=points_array[points_pointer] + level_selection*2;
00225                 lcd.printf("puntos %i",points_array[points_pointer] );
00226                 wait(3);//just to keep the word "ganaste" in the display for some time
00227                 level_selection++;
00228                 secuence_generator(&level_selection);
00229                 contador=0;
00230             }
00231         }
00232     }//end_of_while(1)
00233 }//end_of_int_main
00234 //functions
00235 //function to generate sequences-------------------------
00236 void secuence_generator(int *ptr_level)
00237 {
00238     int j;
00239 
00240     srand (time(NULL));
00241     for (j=0; j<=(*ptr_level); j++) {
00242         level_array[j]=rand()%4+1;
00243     }
00244 }
00245 //function to show the number in leds-------------------
00246 void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue)
00247 {
00248     switch(*ptr_randomvalue) {
00249         case 0:
00250             *ptr_busoutvalue=0;
00251             break;
00252         case 1:
00253             *ptr_busoutvalue=1;
00254             break;
00255         case 2:
00256             *ptr_busoutvalue=2;
00257             break;
00258         case 3:
00259             *ptr_busoutvalue=4;
00260             break;
00261         case 4:
00262             *ptr_busoutvalue=8;
00263             break;
00264     }
00265 }
00266 void levelupfunction()
00267 {
00268     level_selection++;
00269     start_mode=0;
00270 }
00271 void leveldownfunction()
00272 {
00273     level_selection--;
00274     start_mode=0;
00275 }
00276 void start_reset()
00277 {
00278     start_mode=!start_mode;
00279     contador=0;
00280 }
00281 void show_scores_function()
00282 {
00283     lcd.cls();
00284     for (i=0; i<8; i++) {
00285         lcd.printf("%i:%i,", i+1,points_array[i]);
00286         scores_time=timer.read_ms();
00287     }
00288     start_mode=0;
00289 }