Memory Game
This project is a memory game incorporating a keypad, a speaker (and amp) playing audio off an SD card. This games tests your memory in rounds until you can't remember anymore.
include the mbed library with this snippet
#include <mbed.h> #include <mpr121.h> #include <vector> #include "uLCD_4DGL.h" #include "SDFileSystem.h" #include "wave_player.h" //#include "Speaker.h" AnalogOut DACout(p18); wave_player waver(&DACout); SDFileSystem sd(p5, p6, p7, p8, "sd"); Serial pc(USBTX,USBRX); PwmOut motr(p22); // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); // Setup the i2c bus on pins 9 and 10 I2C i2c(p9, p10); // Setup the Mpr121: // constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); uLCD_4DGL uLCD(p9,p10,p11); // Key hit/release interrupt routine vector<int> plyr; void fallInterrupt() { motr.period(0.0020); motr.write(1); wait(0.05); double value = mpr121.read(0x00); value += mpr121.read(0x01)<<8; value = log(value)/log(2.0); motr.period(0.0020); if (value < -12.0 || value > 12){ value = -1.0; } if (value >= 0){ plyr.push_back(value); } motr.write(0); } int main() { interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); srand(time(0)); bool live = true; vector< int > comp; int rnd = 1; int iRand = 0; while (live) { plyr.clear(); //pc.printf("player array: %i\n\r",plyr.size()); //wait(5); iRand = rand() % 10; pc.printf("Round %i\n\r",rnd); uLCD.cls(); uLCD.locate(5,0); uLCD.printf("Round %i\n\r",rnd); comp.push_back(iRand); wait(5); pc.printf("Try and remember this: \n\r"); uLCD.locate(0,5); uLCD.printf("Try and remember this:"); for (int disp = 0; disp < comp.size(); disp++){ pc.printf("%i",comp[disp]); uLCD.printf("%i",comp[disp]); } pc.printf(" \n\r"); wait(5); pc.printf("What was the sequence?\n\r"); uLCD.cls(); uLCD.locate(0,0); uLCD.printf("What was the sequence?\n\r"); wait(10); pc.printf("You entered: \n\r"); uLCD.cls(); uLCD.locate(0,0); uLCD.printf("You entered: \n\r"); uLCD.locate(0,5); for (int disp = 0; disp < plyr.size(); disp++){ pc.printf("%i",plyr[disp]); uLCD.printf("%i",plyr[disp]); } wait(5); pc.printf(" \n\r"); wait(5); //pc.printf("Size: %i\n\r",plyr.size()); //wait(5); if(plyr.size()==comp.size()) { for(int indx = 0; indx < comp.size(); indx++){ if (comp[indx]!= plyr[indx]){ uLCD.cls(); uLCD.locate(5,0); uLCD.printf("Game Ended!\n\n\r"); pc.printf("Game Ended!\n\r"); live = false; uLCD.printf("Your Score: %d \n" , rnd-1 ); pc.printf("Your Score: %d \n" , rnd-1 ); break; } } rnd++; } else { uLCD.cls(); uLCD.locate(5,0); uLCD.printf("Game Ended!\n\n\r"); uLCD.printf("Your Score: %d \n" , rnd-1); pc.printf("Game Ended!\n\r"); pc.printf("Your Score: %d \n" , rnd-1 ); live = false; {{/media/uploads/Osherif/ulcd.jpg}} } } FILE *wave_file = fopen("/sd/eyes.wav", "r"); waver.play(wave_file); //Plays *.wav audio file fclose(wave_file); }
Please log in to post comments.