Codigo para juego de aros ultrasonicos utilizando la tarjeta freedom KL25Z
Dependencies: PinDetect_for_KL25Z TextLCD mbed
Diff: main.cpp
- Revision:
- 1:39340990fa3f
- Parent:
- 0:5ebac3c273c0
- Child:
- 2:edb1ef4d407d
--- a/main.cpp Sun May 11 04:32:15 2014 +0000 +++ b/main.cpp Sun May 11 17:10:49 2014 +0000 @@ -1,53 +1,72 @@ +/*'Frisensor' code for a game that generates a sequence of leds (4), and checks if the user +is able to remember the sequence and reproduce it with ultrasonic sensor +Designed for the subject "informatica industrial", +in Tecnologico de Monterrey Campus Guadalajara +as of May 11 2014 +v 1.1*/ +/* Autors: Jorge Muñoz, Ismael Davila, Miguel Ortiz, Carlos Reyes +*/ + #include "mbed.h" #include "TextLCD.h" #include "Timer.h" #include "PinDetect.h" -Serial pc(USBTX, USBRX); +/*copyright of pindetect library +Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: +*/ + //LCD TextLCD lcd( D2, D3, D4, D5,D6, D7, TextLCD::LCD16x2); - //leds BusOut leds(PTC3, PTC4, PTC5, PTC6); - // Sensores - AnalogIn sensor_1(A0); AnalogIn sensor_2(A2); AnalogIn sensor_3(A4); AnalogIn sensor_4(A1); - //pot for velocity of leds AnalogIn pot(A3); - +//variables fot sensors float s1,s2,s3,s4; +//modes---------- int modo_lectura=0; - +int gamemode=0; +int start_mode=0; +//array for points int points_array[10]; int points_pointer=0; -//level_array -int level_array[20]; +//array for levels +int level_array[20];//max 19 levels int level_selection=1; - +//timer Timer timer; -int gamemode=0; + +// int contador; -int start_mode=0; -int i; +int i;//its used for some "fors" int show_time=500; int scores_time=0; -// Button with antibouncing effect------------------------ +// Button with antibouncing effect------ PinDetect start_button(PTA13); /*PinDetect levelup(PTD5); PinDetect leveldown(PTD0);*/ PinDetect show_scores(PTD2); +//functions----------------------------- +void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue);//function that displays the sequence of leds +int tiempo_apagado, leds_value; +void secuence_generator(int *ptr_level);//function that generates the sequence of the level -//functions----------------------------- -void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue); -int tiempo_apagado, leds_value; -void secuence_generator(int *ptr_level); +//functions fot buttons---------------- void start_reset(); void levelupfunction(); void leveldownfunction(); @@ -58,11 +77,12 @@ contador=0; secuence_generator(&level_selection); timer.start(); - + //because we are using internal PullUp resistors we use deasserted start_button.attach_deasserted(&start_reset); /*leveldown.attach_deasserted(&levelupfunction); levelup.attach_deasserted(&leveldownfunction);*/ show_scores.attach_deasserted(&show_scores_function); + //because we are using internal PullUp resistors we need to add this start_button.mode(PullUp); /*leveldown.mode(PullUp); levelup.mode(PullUp);*/ @@ -70,20 +90,16 @@ start_button.setSampleFrequency(); show_scores.setSampleFrequency(); while(1) { - while(start_mode==0) {//mode Press Start........................... + while(start_mode==0) {//mode Press Start--------------------------- if((timer.read_ms()>show_time+500) && (timer.read_ms()>scores_time+4000)) { lcd.cls(); contador=0; - /*secuence_generator(&level_selection);*/ timer.reset(); lcd.printf("press start \nnivel %i \n", level_selection); secuence_generator(&level_selection); - //(pot.read()*1000)+200= (pot.read()*1000)+200; } } while((gamemode==0)&&(start_mode==1)) {//mode show led sequence---------------------- - - while(timer.read_ms()>(pot.read()*1000)+200) { leds_function(&level_array[contador],&leds_value); leds=leds_value; @@ -102,21 +118,13 @@ } } } - while((gamemode==1)&&(start_mode==1)) { - /*s1=sensor_1.read(); - s2=sensor_2.read(); - s3=sensor_3.read(); - s4=sensor_4.read();*/ - /*pc.printf("Sensor 1 %f \n", s1); - pc.printf("Sensor 2 %f \n", s2); - pc.printf("Sensor 3 %f \n", s3); - pc.printf("Sensor 4 %f \n\n", s4);*/ + while((gamemode==1)&&(start_mode==1)) {//gaming mode------------------------------- while(modo_lectura==0) { s1=sensor_1.read(); s2=sensor_2.read(); s3=sensor_3.read(); s4=sensor_4.read(); - if((s1>0.05)&&(s2>0.05)&&(s3>0.05)&&(s4>0.05)) {// verify that theres nothing above leds + if((s1>0.05)&&(s2>0.05)&&(s3>0.05)&&(s4>0.05)) {// verify that there is nothing above leds modo_lectura=1; lcd.printf("Empieza!!\n"); } @@ -129,8 +137,7 @@ if(contador!=0) { lcd.cls(); } - if(s1<0.017 && level_array[contador]==1) {// sensor 1 code - + if(s1<0.017 && level_array[contador]==1) {// sensor 1 code---------------- modo_lectura=0; contador++; lcd.printf("good \n"); @@ -141,18 +148,16 @@ for (i=0; i<9; i++) { points_array[i]=points_array[i+1]; } - } - else{ - points_pointer++; - } - + } else { + points_pointer++; + } modo_lectura=0; gamemode=0; contador=0; start_mode=0; level_selection=1; } - if(s2<0.017 && level_array[contador]==2) {// sensor 2 code + if(s2<0.017 && level_array[contador]==2) {// sensor 2 code----------------------- modo_lectura=0; contador++; lcd.printf("good \n"); @@ -163,19 +168,16 @@ for (i=0; i<9; i++) { points_array[i]=points_array[i+1]; } - } - else{ - points_pointer++; - } - + } else { + points_pointer++; + } modo_lectura=0; gamemode=0; contador=0; start_mode=0; level_selection=1; } - if(s3<0.017 && level_array[contador]==3) {// sensor 3 code - + if(s3<0.017 && level_array[contador]==3) {// sensor 3 code------------------------ modo_lectura=0; contador++; lcd.printf("good \n"); @@ -186,20 +188,16 @@ for (i=0; i<9; i++) { points_array[i]=points_array[i+1]; } - } - else{ - points_pointer++; - } - + } else { + points_pointer++; + } start_mode=0; modo_lectura=0; gamemode=0; contador=0; level_selection=1; - } - if(s4<0.017 && level_array[contador]==4) {// sensor 4 code - + if(s4<0.017 && level_array[contador]==4) {// sensor 4 code------------------------- modo_lectura=0; contador++; lcd.printf("good \n"); @@ -210,48 +208,41 @@ for (i=0; i<7; i++) { points_array[i]=points_array[i+1]; } - } - else{ - points_pointer++; - } - + } else { + points_pointer++; + } modo_lectura=0; gamemode=0; contador=0; start_mode=0; level_selection=1; - } } if(contador>level_selection) {//check if theres winners------------------- lcd.printf("ganaste \n"); - gamemode=0; points_array[points_pointer]=points_array[points_pointer] + level_selection*2; lcd.printf("puntos %i",points_array[points_pointer] ); - wait(3); + wait(3);//just to keep the word "ganaste" in the display for some time level_selection++; secuence_generator(&level_selection); contador=0; - } } }//end_of_while(1) }//end_of_int_main - - -//funciones +//functions +//function to generate sequences------------------------- void secuence_generator(int *ptr_level) { int j; - + srand (time(NULL)); for (j=0; j<=(*ptr_level); j++) { level_array[j]=rand()%4+1; - pc.printf(" %i \n",level_array[j]); } } - +//function to show the number in leds------------------- void leds_function(int *ptr_randomvalue, int *ptr_busoutvalue) { switch(*ptr_randomvalue) { @@ -272,35 +263,27 @@ break; } } - void levelupfunction() { - level_selection++; start_mode=0; - } void leveldownfunction() { - level_selection--; start_mode=0; - } void start_reset() { - start_mode=!start_mode; contador=0; - } -void show_scores_function(){ +void show_scores_function() +{ lcd.cls(); - for (i=0;i<8;i++){ + for (i=0; i<8; i++) { lcd.printf("%i:%i,", i+1,points_array[i]); scores_time=timer.read_ms(); - - } start_mode=0; - } \ No newline at end of file +} \ No newline at end of file