Colton Mathis / Mbed 2 deprecated Simon

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*********************************/
00002 // ECE 4180 Lab 4
00003 // Colton Mathis, Taylor Hawley
00004 /*********************************/
00005 
00006 #include "mbed.h"
00007 #include "uLCD_4DGL.h"
00008 #include "PinDetect.h"
00009 #include "Speaker.h"
00010 
00011 // pushbutton interrupts with debounce
00012 PinDetect pb1(p25, PullUp);
00013 PinDetect pb2(p26, PullUp);
00014 PinDetect pb3(p23, PullUp);
00015 PinDetect pb4(p24, PullUp);
00016 
00017 // four LED outputs
00018 DigitalOut led1(p7);
00019 DigitalOut led2(p9);
00020 DigitalOut led3(p8);
00021 DigitalOut led4(p6);
00022 
00023 // speaker object
00024 Speaker mySpeaker(p21);
00025 
00026 // LCD object
00027 uLCD_4DGL uLCD(p28, p27, p29);
00028 
00029 // analog input for seeding rand() function
00030 AnalogIn adcReading(p18);
00031 AnalogIn pot(p19);
00032 
00033 // variable declarations
00034 int levels[16];
00035 int levelsIndex;
00036 int level;
00037 int buttonPressed;
00038 int buttonPushes;
00039 int failed;
00040 int start = 1;
00041 int randomSeed;
00042 float volume;
00043 
00044 // function prototypes
00045 void enableInterrupts();
00046 void disableInterrupts();
00047 void startGame();
00048 void setLevels();
00049 void playSequence();
00050 void pushOne();
00051 void pushTwo();
00052 void pushThree();
00053 void pushFour();
00054 void failedGame();
00055 void endGame();
00056 
00057 int main() {
00058     
00059     // read in value from analog input as unsigned short instead of float
00060     // and seed the rand() function with the value
00061     randomSeed = adcReading.read_u16();
00062     srand(randomSeed);
00063     uLCD.baudrate(3000000);
00064     
00065     while(1) {
00066         
00067         volume = pot.read();
00068         
00069         // only initialize these variables at the start of each game
00070         if(start == 1){
00071             failed = 0;
00072             level = 0;
00073             buttonPressed = 0;
00074             buttonPushes = 0;
00075             levelsIndex = 0;
00076             startGame();
00077         }
00078         start = 0;
00079         
00080         // generate and play the sequence of LEDs (and sounds) that the player copies
00081         setLevels();
00082         playSequence();
00083         
00084         // enable the four pushbutton interrupts
00085         enableInterrupts();
00086         
00087         // wait in this loop until the user pushes buttons the same number of times as the current level
00088         // or until the user has pushed an incorrect button
00089         while(buttonPushes != level && failed == 0){
00090             volume = pot.read();
00091             wait(0.1);
00092         }
00093         
00094         // if the user has failed, disable the pushbutton interrupts, notify the user that they failed,
00095         // and then set start=1 so that the game restarts from level 1
00096         if(failed == 1){
00097             disableInterrupts();
00098             failedGame(); 
00099             start = 1; 
00100         }
00101         
00102         // if the user has completed all of the levels without failing, disable the pushbutton interrupts,
00103         // notify the user that they won, and set start=1 so that the game restarts from level 1
00104         if(level == 10 && failed == 0){
00105             disableInterrupts();
00106             endGame();
00107             start = 1;
00108         }
00109         
00110         // if the user has not failed or completed all of the levels, disable the interrupts and continue
00111         disableInterrupts();
00112         
00113         // set buttonpushes=0 to record the button presses for the next level
00114         buttonPushes = 0;
00115         
00116         // wait a short amount of time before starting the next game
00117         if(start==1){
00118             wait(2);
00119         }
00120         
00121     }
00122     
00123 }
00124 
00125 
00126 void enableInterrupts(){
00127     
00128     // set the four pushbutton interrupts to trigger on a falling edge
00129     pb1.attach_deasserted(&pushOne);
00130     pb2.attach_deasserted(&pushTwo);
00131     pb3.attach_deasserted(&pushThree);
00132     pb4.attach_deasserted(&pushFour);
00133     
00134     // start the four interrupts (default sample frequency is 20ms)
00135     pb1.setSampleFrequency();
00136     pb2.setSampleFrequency();
00137     pb3.setSampleFrequency();
00138     pb4.setSampleFrequency();
00139     
00140 }
00141 
00142 
00143 void disableInterrupts(){
00144     
00145     // disable four interrupts by setting their service routine to NULL pointer
00146     pb1.attach_deasserted(NULL);
00147     pb2.attach_deasserted(NULL);
00148     pb3.attach_deasserted(NULL);
00149     pb4.attach_deasserted(NULL);
00150 
00151 }
00152 
00153 // this function is called at the beginning of the game
00154 void startGame(){
00155     
00156     uLCD.cls();
00157     uLCD.display_control(PORTRAIT);
00158     uLCD.color(BLUE);
00159     uLCD.text_width(2);
00160     uLCD.text_height(2);
00161     uLCD.locate(2, 3);
00162     uLCD.printf("SIMON");
00163     
00164     // start up LED/uLCD sequence
00165     led1 = 1;
00166     uLCD.filled_rectangle(0, 0, 63, 40, 0x00CC00);
00167     wait(0.5);
00168     led1 = 0;
00169     uLCD.filled_rectangle(0, 0, 63, 40, 0x000000);
00170     
00171     led2 = 1;
00172     uLCD.filled_rectangle(64, 0, 127, 40, 0xFF0000);
00173     wait(0.5);
00174     led2 = 0;
00175     uLCD.filled_rectangle(64, 0, 127, 40, 0x000000);
00176     
00177     led3 = 1;
00178     uLCD.filled_rectangle(64, 87, 127, 127, 0x0000FF);
00179     wait(0.5);
00180     led3 = 0;
00181     uLCD.filled_rectangle(64, 87, 127, 127, 0x000000);
00182     
00183     led4 = 1;
00184     uLCD.filled_rectangle(0, 87, 63, 127, 0xCCCC00);
00185     wait(0.5);
00186     led4 = 0;
00187     uLCD.filled_rectangle(0, 87, 63, 127, 0x000000);
00188     
00189     wait(2);
00190     
00191     uLCD.cls();
00192 
00193 }
00194 
00195 
00196 void setLevels(){
00197     
00198     // "levels" array is a sequence of numbers 1-4 that determines which LEDs will light up
00199     // during a given level --- grows by 1 value each level
00200     level++;
00201     levels[level-1] = rand()%4 + 1;
00202     
00203     // display the current level on the uLCD
00204     uLCD.color(BLUE);
00205     uLCD.text_width(2);
00206     uLCD.text_height(2);
00207     uLCD.locate(2, 2);
00208     uLCD.printf("Level");
00209     uLCD.locate(4, 4);
00210     uLCD.printf("%d", level);
00211     wait(1);
00212     
00213 }
00214 
00215 
00216 void playSequence(){
00217 
00218     // play the sequence of LEDs defined in the "levels" array along with the corresponding notes
00219     for(int i = 0; i < level; i++){
00220         switch(levels[i]){
00221             case 1:
00222                 led1 = 1;
00223                 mySpeaker.PlayNote(659.26, 0.50, volume);
00224                 led1 = 0;
00225                 wait(0.025);
00226                 break;
00227             case 2:
00228                 led2 = 1;
00229                 mySpeaker.PlayNote(554.00, 0.50, volume);
00230                 led2 = 0;
00231                 wait(0.025);
00232                 break;
00233             case 3:
00234                 led3 = 1;
00235                 mySpeaker.PlayNote(440.00, 0.50, volume);
00236                 led3 = 0;
00237                 wait(0.025);
00238                 break;
00239             case 4:
00240                 led4 = 1;
00241                 mySpeaker.PlayNote(329.63, 0.50, volume);
00242                 led4 = 0;
00243                 wait(0.025);
00244                 break;
00245             default:
00246                 while(1){
00247                     led1 = !led1;
00248                     wait(0.5);
00249                 }
00250         }   
00251     }   
00252    
00253 }
00254 
00255 // ISR for pushbutton 1 interrupt
00256 void pushOne(){
00257     
00258     buttonPushes++;
00259     
00260     led1 = 1;
00261     mySpeaker.PlayNote(659.26, 0.25, volume);
00262     led1 = 0;
00263     
00264     if(levels[buttonPushes-1] != 1){
00265         failed = 1;   
00266     }
00267     
00268 }
00269 
00270 // ISR for pushbutton 2 interrupt
00271 void pushTwo(){
00272     
00273     buttonPushes++;
00274     
00275     led2 = 1;
00276     mySpeaker.PlayNote(554.00, 0.25, volume);
00277     led2 = 0;
00278     
00279     if(levels[buttonPushes-1] != 2){
00280         failed = 1;   
00281     }
00282     
00283 }
00284 
00285 // ISR for pushbutton 3 interrupt
00286 void pushThree(){
00287     
00288     buttonPushes++;
00289     
00290     led3 = 1;
00291     mySpeaker.PlayNote(440.00, 0.25, volume);
00292     led3 = 0;
00293     
00294     if(levels[buttonPushes-1] != 3){
00295         failed = 1;   
00296     }
00297     
00298 }
00299 
00300 // ISR for pushbutton 4 interrupt
00301 void pushFour(){
00302     
00303     buttonPushes++;
00304     
00305     led4 = 1;
00306     mySpeaker.PlayNote(329.63, 0.25, volume);
00307     led4 = 0;
00308     
00309     if(levels[buttonPushes-1] != 4){
00310         failed = 1;   
00311     }
00312     
00313 }
00314 
00315 // this function is called when the user fails the game
00316 void failedGame(){
00317     
00318     uLCD.cls();
00319     uLCD.color(RED);
00320     uLCD.text_width(2);
00321     uLCD.text_height(2);
00322     uLCD.locate(2, 2);
00323     uLCD.printf("Game");
00324     uLCD.locate(2, 4);
00325     uLCD.printf("Over");
00326     
00327     
00328     mySpeaker.PlayNote(147.00, 0.60, volume);
00329     mySpeaker.PlayNote(139.00, 0.60, volume);
00330     mySpeaker.PlayNote(131.00, 0.60, volume);
00331     mySpeaker.PlayNote(123.00, 1.60, volume);
00332     
00333 }
00334 
00335 // this function is called if the user completes all levels
00336 void endGame(){
00337     
00338     uLCD.cls();
00339     uLCD.color(GREEN);
00340     uLCD.text_width(2);
00341     uLCD.text_height(2);
00342     uLCD.locate(2, 2);
00343     uLCD.printf("YOU");
00344     uLCD.locate(2, 4);
00345     uLCD.printf("WIN!");
00346     
00347     int time = 0.01;
00348     
00349     mySpeaker.PlayNote(392.00, 0.33, volume);
00350     wait(time);
00351     mySpeaker.PlayNote(349.23, 0.17, volume);
00352     wait(time);
00353     
00354     mySpeaker.PlayNote(311.13, 0.33, volume);
00355     wait(time);
00356     mySpeaker.PlayNote(311.13, 0.17, volume);
00357     wait(time);
00358     mySpeaker.PlayNote(311.13, 0.33, volume);
00359     wait(time);
00360     mySpeaker.PlayNote(349.23, 0.17, volume);
00361     wait(time);
00362     
00363     mySpeaker.PlayNote(392.00, 0.33, volume);
00364     wait(time);
00365     mySpeaker.PlayNote(392.00, 0.17, volume);
00366     wait(time);
00367     mySpeaker.PlayNote(392.00, 0.165, volume);
00368     wait(time);
00369     mySpeaker.PlayNote(349.23, 0.165, volume);
00370     wait(time);
00371     mySpeaker.PlayNote(311.13, 0.17, volume);
00372     wait(time);
00373     
00374     mySpeaker.PlayNote(349.23, 0.165, volume);
00375     wait(time);
00376     mySpeaker.PlayNote(392.00, 0.165, volume);
00377     wait(time);
00378     mySpeaker.PlayNote(349.23, 0.17, volume);
00379     wait(time);
00380     mySpeaker.PlayNote(311.13, 0.33, volume);
00381     wait(time);
00382     mySpeaker.PlayNote(293.66, 0.17, volume);
00383     wait(time);
00384     
00385     mySpeaker.PlayNote(311.13, 0.5, volume);
00386     
00387 }