BopIt on the mbed

Dependencies:   SDFileSystem mbed wave_player

Files at this revision

API Documentation at this revision

Comitter:
dgobin
Date:
Thu Oct 17 19:58:54 2013 +0000
Commit message:
A game of bopIt on the mbed

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
bobit.h Show annotated file Show diff for this revision Revisions of this file
chSig.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d30842e497f7 SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Thu Oct 17 19:58:54 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r 000000000000 -r d30842e497f7 bobit.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bobit.h	Thu Oct 17 19:58:54 2013 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+
+
+//lights for guiding player
+DigitalOut led1(LED1);
+DigitalOut led2(LED2); 
+DigitalOut led3(LED3); 
+DigitalOut led4(LED4); 
+
+//noises for guiding player 
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+AnalogOut speaks(p18);
+wave_player wplays(&speaks);
+
+//slider 
+AnalogIn slide(p15); 
+//dial 
+AnalogIn twist(p16); 
+//Motion
+AnalogIn swipe(p19);
+//boop 
+DigitalIn boop(p21); 
+ //function 
+ int chSig(int val, float sli, float twi); 
\ No newline at end of file
diff -r 000000000000 -r d30842e497f7 chSig.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/chSig.cpp	Thu Oct 17 19:58:54 2013 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h" 
+//slider 
+AnalogIn slid(p15); 
+//dial 
+AnalogIn twis(p16); 
+//Motion
+AnalogIn swip(p19);
+//boop 
+DigitalIn boo(p21); 
+
+Serial px(USBTX, USBRX); 
+
+int chSig( int value, float sl, float tw) {
+     int check; 
+    if(value == 0 && boo.read()!=0){ 
+        check = 1; 
+     }else if (value ==1 && ( slid.read() >= (sl + 0.05) || slid.read() <= (sl-0.05) ) ){ 
+        check = 1; 
+     }else if(value==2 && swip.read() > 0.5){ 
+        check = 1; 
+     }else if(value ==3 && ( (tw-.05)>= twis.read() ||  twis.read()>=(tw+.05) ) ){
+        check = 1; 
+     }else if( (value!=0 && boo.read() != 0)){ 
+        check=2; 
+     } else if( value!=1 && ( slid.read() >= (sl + 0.05) || slid.read() <= (sl-0.05) ) ) {
+         check = 2; 
+     }else if( value !=3 && ( (tw-.05)>= twis.read() ||  twis.read()>=(tw+.05) ) ) { 
+         check = 2; 
+     }else if( value!=2 && swip.read()>0.5 ) { 
+         check = 2; 
+     }else{ 
+        check = 0; 
+     }
+     return check; 
+  
+  }
+     
\ No newline at end of file
diff -r 000000000000 -r d30842e497f7 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 17 19:58:54 2013 +0000
@@ -0,0 +1,187 @@
+#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 
diff -r 000000000000 -r d30842e497f7 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Oct 17 19:58:54 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f
\ No newline at end of file
diff -r 000000000000 -r d30842e497f7 wave_player.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Thu Oct 17 19:58:54 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad