Final Trivia Game Code
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of TriviaGame by
Diff: main.cpp
- Revision:
- 1:1d5055ca2793
- Parent:
- 0:e09703934ff4
- Child:
- 3:9842b3f1d809
--- a/main.cpp Wed Mar 16 01:49:13 2011 +0000 +++ b/main.cpp Fri Nov 04 15:43:36 2016 +0000 @@ -1,125 +1,691 @@ -/* -Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ +#include "mbed.h" +#include "SDFileSystem.h" +#include "uLCD_4DGL.h" +#include "wave_player.h" +#include "mpr121.h" +#include <vector> -#include <mbed.h> -#include <string> -#include <list> - -#include <mpr121.h> - -DigitalOut led1(LED1); -DigitalOut led2(LED2); -DigitalOut led3(LED3); -DigitalOut led4(LED4); - -// Create the interrupt receiver object on pin 26 +AnalogOut DACout(p18); // used to play sound on speaker +//wave player plays a *.wav file to D/A and a PWM +wave_player waver(&DACout); InterruptIn interrupt(p26); - -// Setup the Serial to the PC for debugging -Serial pc(USBTX, USBRX); - -// Setup the i2c bus on pins 28 and 27 I2C i2c(p9, p10); - -// Setup the Mpr121: -// constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); -void fallInterrupt() { - int key_code=0; - int i=0; - int value=mpr121.read(0x00); - value +=mpr121.read(0x01)<<8; - // LED demo mod by J. Hamblen - //pc.printf("MPR value: %x \r\n", value); - i=0; - // puts key number out to LEDs for demo - for (i=0; i<12; i++) { - if (((value>>i)&0x01)==1) key_code=i+1; - } - led4=key_code & 0x01; - led3=(key_code>>1) & 0x01; - led2=(key_code>>2) & 0x01; - led1=(key_code>>3) & 0x01; -} -int main() { - - pc.printf("\nHello from the mbed & mpr121\n\r"); - - unsigned char dataArray[2]; - int key; - int count = 0; - - pc.printf("Test 1: read a value: \r\n"); - dataArray[0] = mpr121.read(AFE_CFG); - pc.printf("Read value=%x\r\n\n",dataArray[0]); - - pc.printf("Test 2: read a value: \r\n"); - dataArray[0] = mpr121.read(0x5d); - pc.printf("Read value=%x\r\n\n",dataArray[0]); - - pc.printf("Test 3: write & read a value: \r\n"); - mpr121.read(ELE0_T); - mpr121.write(ELE0_T,0x22); - dataArray[0] = mpr121.read(ELE0_T); - pc.printf("Read value=%x\r\n\n",dataArray[0]); - - pc.printf("Test 4: Write many values: \r\n"); - unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41}; - mpr121.writeMany(0x42,data,7); - - // Now read them back .. - key = 0x42; - count = 0; - while (count < 7) { - char result = mpr121.read(key); - key++; - count++; - pc.printf("Read value: '%x'=%x\n\r",key,result); - } - - pc.printf("Test 5: Read Electrodes:\r\n"); - key = ELE0_T; - count = 0; - while (count < 24) { - char result = mpr121.read(key); - pc.printf("Read key:%x value:%x\n\r",key,result); - key++; - count++; - } - pc.printf("--------- \r\n\n"); - - // mpr121.setProximityMode(true); - - pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG)); - - interrupt.fall(&fallInterrupt); - interrupt.mode(PullUp); - - while (1) { - wait(5); - pc.printf("."); - } -} +uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin; +SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup +int Score; +int HighScore = 0; +int question = 0; +bool answer = 1; +int index = 0; +bool check = 0; +int value = 0; +std::vector<int> v; - +int main() +{ + srand(time(NULL)); + while (true){ + answer = 1; + for (int i=0; i<10; i++){ + v.push_back(i+1); + } + check = 0; + question = 0; + uLCD.baudrate(3000000); + uLCD.background_color(BLACK); + uLCD.color(BLUE); + uLCD.cls(); + if (Score > HighScore){ + HighScore = Score; + uLCD.printf("NEW HIGH SCORE!\n %d", HighScore); + wait(2); + } + Score = 0; + uLCD.cls(); + uLCD.printf("\nTrivia Game \n Main Menu\n\n"); + uLCD.printf("High Score: %d \n", HighScore); + uLCD.printf("\nFor New Game \n Press 0"); + while (value != 1){ + value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + } + uLCD.cls(); + if (v.size() !=0){ + while (answer == 1){ + if (v.size() == 0){ + break; + } + index = rand() %v.size(); + question = v.at(index); + v.erase(v.begin()+index); + check = 0; + uLCD.cls(); + wait(1); + if (question == 1){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("Who won the World Series in 2016?\n\n\n"); + uLCD.printf("1) Cleveland\nIndians\n"); + uLCD.printf("2) Chicago Cubs\n"); + uLCD.printf("3) San Diego\nPadres\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 2){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 4){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 2){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("What is the\n capital of South\n Dakota?\n\n"); + uLCD.printf("1) Pierre\n"); + uLCD.printf("2) Bismarck\n"); + uLCD.printf("3) Lakota\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 2){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score=Score+1;; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + + } + if (question == 3){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("Who wrote The \nGrapes of Wrath?\n\n\n"); + uLCD.printf("1)Ernest Hemingway"); + uLCD.printf("2)George Orwell\n"); + uLCD.printf("3)John Steinbeck\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 2){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 8){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 4){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("What year was \nEinstein born?\n\n\n"); + uLCD.printf("1) 1900\n"); + uLCD.printf("2) 1879\n"); + uLCD.printf("3) 1873\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 2){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 4){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 5){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("Which song is by the Rolling Stones?\n\n\n"); + uLCD.printf("1) Paint It Black\n"); + uLCD.printf("2) Feel For \nThe Devil\n"); + uLCD.printf("3) Let It Be\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 2){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 6){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("Where was Hitler born?\n\n\n"); + uLCD.printf("1) Poland\n"); + uLCD.printf("2) Germany\n"); + uLCD.printf("3) Austria\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 8){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 2){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 7){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("The best selling\nmusic single of all time is?\n\n\n"); + uLCD.printf("1) Billie Jean\n"); + uLCD.printf("2) White Christmas\n"); + uLCD.printf("3) Candle in the \nWind\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 8){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 2){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 8){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("What is the \ncurrency of \nJapan?\n\n\n"); + uLCD.printf("1) Pounds\n"); + uLCD.printf("2) Yen\n"); + uLCD.printf("3) Riki\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 2){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 4){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 9){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("Who was Harry \nHoudini?\n\n\n"); + uLCD.printf("1)An Escape Artist\n"); + uLCD.printf("2)Fighter Pilot in WWII\n"); + uLCD.printf("3) Harry Potter's Cousin\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 2){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + if (question == 10){ + value = 0; + uLCD.cls(); + uLCD.locate(1,2); + uLCD.printf("What is glass \nmainly made up \nof?\n\n\n"); + uLCD.printf("1) Sand\n"); + uLCD.printf("2) Crystals\n"); + uLCD.printf("3) Highly \nCompressed \nWater\n"); + while (check == 0){ + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + if (value == 4){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + if (value == 2){ + //play correct image + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x005E); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/right_taught.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + Score = Score+1; + uLCD.printf("Score: %d", Score); + wait(1); + answer = 1; + check = 1; + } + if (value == 8){ + //play game over + uLCD.cls(); + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x009F); + uLCD.display_image(0,0); + FILE *wave_file; + wave_file=fopen("/sd/sounds/wrong_last.wav","r"); + waver.play(wave_file); + fclose(wave_file); + wait(1); + uLCD.cls(); + answer = 0; + check = 1; + } + } + } + } + + + + + + } + else{ + uLCD.printf("CONGRATS!"); + wait(1); + } + +} +} \ No newline at end of file