BopIt on the mbed

Dependencies:   SDFileSystem mbed wave_player

main.cpp

Committer:
dgobin
Date:
2013-10-17
Revision:
0:d30842e497f7

File content as of revision 0:d30842e497f7:

#include "mbed.h"
#include "bobit.h"
Serial pc(USBTX, USBRX); 


int main() {

  int gamestate;
  int ch=0; 
  float sl, tw;
  int tehPlay; 
  int count = 0; 
  //set gamestate to starting state 
  gamestate = 1;
  //declare files
  FILE *sbyte;  
  FILE *begin;
  //open starting sound file 
  begin = fopen("/sd/wavfiles/begin.wav","r"); 
  wplays.play(begin); 
  fclose(begin); 
  wait(1); 
    while(1) {
    switch(gamestate) {
     //"preparation" state
      case 1:  
      //set all LEDs to 0
      led1 = 0; 
      led2 = 0; 
      led3 = 0; 
      led4 = 0; 
      //get play value 
      float b = slide.read()*1000 + count*15 + twist.read()*100 + 926; 
      srand(b);  
      tehPlay = (rand())%4;
      //0 is bop it --hit a button
           if(tehPlay == 0) { 
               sbyte = fopen("/sd/wavfiles/bop.wav","r"); 
               wplays.play(sbyte); 
               fclose(sbyte);
               wait(.25);  
      //1 is slide it --slide switch
           }else if (tehPlay == 1){ 
               sbyte = fopen("/sd/wavfiles/slide.wav","r"); 
               wplays.play(sbyte); 
               fclose(sbyte); 
               wait(.25); 
      //2 is swipe it --put hand over distance sensor
           }else if (tehPlay == 2){ 
               sbyte = fopen("/sd/wavfiles/swipe.wav", "r"); 
               wplays.play(sbyte); 
               fclose(sbyte); 
               wait(.25); 
      //3 is twist it --twist dial sensor 
           }else if(tehPlay==3){
               sbyte = fopen("/sd/wavfiles/twist.wav","r");
               wplays.play(sbyte); 
               fclose(sbyte);
               wait(.25);  
            }//close if 
 
          //go to "play" state
          gamestate = 2; 
          
          break;
          
      //play state     
      case 2:   
      sl = slide.read(); 
      tw = twist.read();  
      led1 = 0; 
      led2 = 0; 
      led3 = 0; 
      led4 = 0;  
             //depending on number of rounds so far, give the user more or less time to change a sensor
             if( count < 3){
                for(int k = 0; k < 10; k++){ 
                 ch = chSig(tehPlay, sl, tw); 
                   if(ch == 1|| ch==2 ){  
                      count++;  
                      break; 
                    }
                    if( k < 3){ 
                   led1 = 1; 
                   }else if(k>=3 && k<6){ 
                   led2 = 1;
                   }else if(k>= 6 && k<8 ){ 
                   led3 = 1;
                   }else if(k>=8){  
                     led4 = 1; 
                  } 
                   wait(1);
                 }//close for              
              } else if( count >= 3 && count < 5){ 
                 
                for(int k =0; k < 10; k++){ 
 
                   ch = chSig(tehPlay, sl, tw); 
                   if(ch == 1|| ch==2 ){ 
                      count++;  
                      break; 
                    }
                   if( k < 3){ 
                   led1 = 1; 
                   }else if(k>=3 && k<6){ 
                   led2 = 1;
                   }else if(k>= 6 && k<8 ){ 
                   led3 = 1;
                   }else if(k>=8){  
                     led4 = 1; 
                  }  
                   wait(.2); 
                   }//close for
               }else if(count >= 5 && count < 8){ 
               
                 for(int k =0; k < 10; k++){ 
                 ch = chSig(tehPlay, sl, tw); 
                   if(ch == 1 || ch==2 ){ 
                      count++;  
                      break; 
                    }
                    if( k < 3){ 
                   led1 = 1; 
                   }else if(k>=3 && k<6){ 
                   led2 = 1;
                   }else if(k>= 6 && k<8 ){ 
                   led3 = 1;
                   }else if(k>=8){  
                     led4 = 1; 
                  }
                    wait(.13); 
                   }//close for 
                }else if(count >= 8){ 
                  count++; 
                  for( int k = 0; k<10; k++){ 
                  ch = chSig(tehPlay, sl, tw); 
                   if(ch == 1|| ch==2 ){ 
                      break; 
                    }
                      if( k < 3){ 
                   led1 = 1; 
                   }else if(k>=3 && k<6){ 
                   led2 = 1;
                   }else if(k>= 6 && k<8 ){ 
                   led3 = 1;
                   }else if(k>=8){  
                     led4 = 1; 
                  }
                     wait(.085); 
                    }//close for 
                  }//close if 
        
        //if they completed the commands then go back to state 1   
        if(ch == 1){ 
          ch = 0;  
          wait(.5);
          gamestate = 1; 
          break;  
         //if the loop broke because time is up or they were wrong 
        }else if(ch == 2 || ch==0){ 
          gamestate = 3; 
          break;
         }//close if
         
        //possibly unnecessary extra break to end case 2  
        break; 
        
     //game over state: print game over to terminal until player resets mbed
     case 3:
       while(1){ 
          pc.printf("Game Over! Score = %d", count); 
          led1 = 0; 
          led2 = 0; 
          led3 = 0; 
          led4 = 1;
          sbyte = fopen("/sd/wavfiles/gOver.wav","r");  
          wplays.play(sbyte); 
          fclose(sbyte); 
          wait(2); 
        }//close GO while
        break; 
    }//close switch
        
         
    
    }//close main while
}//close main