Whack a Mole game! Features: - LCD graphics display - Touch pad input - Speaker effects through a class D audio amplifier\ - A high score page maintained by the SD card file system - Analog noise used to seed random numbers
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed
Fork of MPR121_Demo by
Diff: main.cpp
- Revision:
- 8:e6e1e5b3f2a9
- Parent:
- 7:5a8f9a018e1f
- Child:
- 9:867dde4be88a
--- a/main.cpp Mon Mar 14 08:12:15 2016 +0000 +++ b/main.cpp Mon Mar 14 16:23:47 2016 +0000 @@ -35,30 +35,47 @@ DigitalOut led4(LED4); AnalogIn Ain(p15); +//Setup class D audio output on pwm out pin SongPlayer mySpeaker(p21); + // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); -// Setup the Serial to the PC for debugging + +//Setup the Serial to the PC for debugging Serial pc(USBTX, USBRX); -SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card + +//setup SD card filesystem +SDFileSystem sd(p5, p6, p7, p8, "sd"); + // Setup the i2c bus on pins 28 and 27 I2C i2c(p28, p27); + +//setup the note to be played upon hitting a mole float note[1]= {1568.0}; float duration[1]= {0.1}; + +//initialize the possition integers int pos1; int pos2; int pos3; int pos4; int pos5; int pos6; + +//initialize global variable to keep track of the score int score = 0; + +//create variables for the keypad outputs that correspond to mole possitions int hole1 = 0x100; int hole2 = 0x10; int hole3 = 0x1; int hole4 = 0x200; int hole5 = 0x20; int hole6 = 0x2; + +//create a character buffer to read from the SD card char str [80]; + // Setup the Mpr121: // constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); @@ -66,14 +83,16 @@ void fallInterrupt() { int key_code=0; int i=0; + + //read values from keypad after interupt and bitshift left 8 times int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; - // LED demo mod by J. Hamblen + //Check to see if the a mole is hit after an interupt if (value==256&&pos1==1) { mySpeaker.PlaySong(note,duration); score++; - pos1 = 0; + pos1 = 0;//set the position to 0 after succesful hit to prevent multiple hits } if (value==16&&pos2==1) { @@ -105,32 +124,7 @@ score++; pos6 = 0; } - pc.printf("MPR value: %x \r\n", value); - /*if (value == 1) - { - led1 = !led1; - } - if (value == 2) - { - led2 = !led2; - } - if (value == 16) - { - led3 = !led3; - } - if (value == 32) - { - led4 = !led4; - } - - if (value == 256) - { - led3 = !led3; - } - if (value == 512) - { - led4 = !led4; - }*/ + //pc.printf("MPR value: %x \r\n", value); //debug code to the COM port that prints MPR value //i=0; // puts key number out to LEDs for demo /* for (i=0; i<12; i++) { @@ -218,10 +212,11 @@ interrupt.fall(&fallInterrupt); interrupt.mode(PullUp); int counter = 0; - while (counter < 5) { + srand(state); + while (counter < 30) { arand = Ain.read()*1000000; state = arand; - srand(state); + state = rand()%15; pc.printf("%i\n\r",state); switch(state) @@ -514,9 +509,10 @@ lcd.filled_circle(col2, row2 , rmole, mole); break; } - wait(2); + wait(0.5); counter++; } + lcd.cls(); wait(3); lcd.cls(); /* mkdir("/sd/mydir", 0777); @@ -562,17 +558,37 @@ pc.printf("Opened file for read\n\r"); fscanf (fp1, "%s", str); sscanf(str, "%d", &please); - pc.printf ("I have read: %d \n",please); fclose(fp1); - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); - if(fp == NULL) { + pc.printf (": %d \n",please); + if (score > please) + { + FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); + if(fp == NULL) { error("Could not open file for write\n"); } char buf[10]; - int a = 5; - sprintf(buf, "%d", a); + sprintf(buf, "%d", score); fprintf(fp, "%s",buf); fclose(fp); + lcd.printf("CONGRATULATIONS!\n\n"); + lcd.printf("You set the new\n\n"); + lcd.printf("High score!!!!!!\n\n"); + lcd.printf("New high score: %d\n\n",score); + lcd.printf("Press reset to\n\n"); + lcd.printf("Play Again!"); + } + else + { + lcd.printf("High score: %d\n\n",please); + lcd.printf("Your score: %d\n\n",score); + lcd.printf("You did not set\n\n"); + lcd.printf("A new high score\n\n"); + lcd.printf("Press reset to\n\n"); + lcd.printf("Try again)"); + } + + +/* FILE *fp2 = fopen("/sd/mydir/sdtest.txt", "r"); if(fp2 == NULL) { @@ -586,9 +602,9 @@ if (please == 5) { pc.printf("we got it\n"); - } + }*/ pc.printf("Goodbye World!\n"); - } + }//end