4180 Project Master Code

Dependencies:   mbed 4DGL-uLCD-SE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "questions.h"
00004 
00005 DigitalOut myled1(LED1); // leds for debugging
00006 DigitalOut myled2(LED2);
00007 DigitalOut myled3(LED3);
00008 DigitalOut myled4(LED4);
00009 
00010 DigitalOut lidar_enable(p29); //to determine when lidar should operate
00011 
00012 DigitalIn p1_enable(p23); // data from player 1
00013 BusIn p1_ans(p22, p21);
00014 
00015 DigitalIn p2_enable(p18); // data from player 2
00016 BusIn p2_ans(p19, p20);
00017 
00018 uLCD_4DGL uLCD(p13, p14, p12);
00019 
00020 Serial pc(USBTX,USBRX); // for teraterm debugging
00021 
00022 PwmOut speaker(p26);
00023 
00024 int msg;
00025 int msg2;
00026 int questionIndex = 0;
00027 string *p;
00028 int player = 0;
00029 
00030 int player1Score = 0;
00031 int player2Score = 0;
00032 
00033 int p1Val = 0;
00034 int p2Val = 0;
00035 
00036 questions arr[30];
00037 
00038 // Light
00039 class RGBLed
00040 {
00041 public:
00042     RGBLed(PinName redpin, PinName greenpin)://, PinName bluepin);
00043     _redpin(redpin), _greenpin(greenpin) //, _bluepin(bluepin)
00044     {
00045         //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
00046         _redpin.period(0.0005);
00047     }
00048 
00049     void write(float red,float green)//, float blue);
00050     {
00051         _redpin = red;
00052         _greenpin = green;
00053 //    _bluepin = blue;
00054     }
00055 private:
00056     PwmOut _redpin;
00057     PwmOut _greenpin;
00058 //    PwmOut _bluepin;
00059 };
00060 
00061 
00062 //class could be moved to include file
00063 
00064 
00065 //Setup RGB led using PWM pins and class
00066 RGBLed myRGBled(p24,p25);//,p21); //RGB PWM pins
00067 
00068 void shuffleQuestions()
00069 {
00070     for (int i = 0; i < 90; i++) {
00071         int rand1 = rand() % 30;
00072         int rand2 = rand() % 30;
00073         questions temp = arr[rand1];
00074         arr[rand1] = arr[rand2];
00075         arr[rand2] = temp;
00076     }
00077 }
00078 
00079 void addQuestions()
00080 {
00081     questions q1("Which of these\nchess figures is\nclosely related to'Bohemian\nRhapsody?\n","King","Queen","Pawn","Bishop","Queen");
00082     questions q2("What religion is\nthe most practicedone in India?\n","Sikhism","Islam","Hinduism","Shinto","Hinduism");
00083     questions q3("What is the name\nof the Jewish New\nYear?\n","Yom Kippur","Kwanzaa","Hanukkah","Rosh Hashanah","Rosh Hashanah");
00084     questions q4("What funk/soul\nband was Lionel\nRichie a member\nof?\n","Commodores","Amigas","Ataris","Spectrums","Commodores");
00085     questions q5("Which country\nhosted the Summer\nOlympics in 2016?\n","Greece","Brazil","China","Spain","Brazil");
00086     questions q6("Which of these\ncities is closest\nto London, UK?\n","Boston, MA","Miami, FL","Atlanta, GA","New York, NY","Boston, MA");
00087     questions q7("What did Alfred\nNobel Develop?\n","Atomic bomb","Nobelium","Gunpowder","Dynamite","Dynamite");
00088     questions q8("Which US Presidentis on the far leftof the Mount\nRushmore\nNational\nMemorial?\n","Theodore\n   Roosevelt","Thomas\n   Jefferson","George\n   Washington","Abraham\n   Lincoln","George\n   Washington");
00089     questions q9("Which actress\nplays a major\nrole in the TV\nshow 'The Big\nBang Theory'\n","Portia de\n   Rossi","Kaley Cuoco","Emily\n   Deschanel","Sofia Vergara","Kaley Cuoco");
00090     questions q10("Which of these\nantagonist\ncharacters was\ncreated by\nnovelist J.K.\nRowling?\n","Professor\n   Moriarty","Lord Voldemort","Darth Vader","Lord Farqaad","Lord Voldemort");
00091     questions q11("Which mammal firstreached Earth's\norbit alive?\n","Monkey","Human","Cat","Dog","Dog");
00092     questions q12("How many spaces\nare on a standard\nmonopoly board?\n","40","20","80","60","40");
00093     questions q13("Which actor does\nnot appear in\n'Saving Private\nRyan'?\n","Tom Hanks","Vin Diesel","Giovanni\n   Ribisi","Ralph Fiennes","Ralph Fiennes");
00094     questions q14("Which city's\nlandmard include:\nThe Pantheon, The\nSpanish Steps and\nTrevi Fountain?\n","Rome","Athens","Istanbul","Barcelone","Rome");
00095     questions q15("How many blue\nstripes does the\nUnited States of\nAmerica national\nflag have?\n","13","6","0","7","0");
00096     questions q16("Which of these\ncountries was not\na Soviet Republic\nin USSR?\n","Moldova","Serbia","Kyrgyzstan","Azerbaijan","Serbia");
00097     questions q17("What temperature\nis the same in\nCelsius and\nFahrenheit?\n","0","-40","+100","+40","-40");
00098     questions q18("What is the name\nof the boxer whoselife story is\ndepicted in the\n1999 movie 'The\nHurricane'?\n","Rubin Carter","Jake LaMotta","Muhammad Ali","Rocky Marciano","Rubin Carter");
00099     questions q19("Adele performed\nthe theme song to\nwhich James Bond\nfilm?\n","Casino Royale","From Russia\n   With Love","Quantum of\n   Solace","Skyfall","Skyfall");
00100     questions q20("What restaurant\nfranchise advises\nyou to 'Eat\nFresh'?\n","Subway","McDonald's","KFC","Taco Bell","Subway");
00101     questions q21("In which language\nwas the book 'War\nand Peace' originally written?\n", "Russian","English","French","German","Russian");
00102     questions q22("Which movie was\nthe first to win\n11 Academy\nAwards?\n","Sound of Music","La La Land","Ben-Hur","Titanic","Ben-Hur");
00103     questions q23("What is the\nlargest country,\nby area, that has\nonly one time\nzone?\n", "China","Russia","Australia","Turkey","China");
00104     questions q24("Which truck is\nproduced by the\nFord Motor\nCompany?\n", "Silverado 1500","CR-V","F-150","RAV4","F-150");
00105     questions q25("What is the\nlargest planet in our Solar System?\n","Pluto","Earth","Saturn","Jupiter","Jupiter");
00106     questions q26("Other than eggs,\nwhat is a primary\ningredient in\nEggs Florentine?\n", "Avocado","Gorgonzola","Spinach","Ham","Spinach");
00107     questions q27("According to\nPersian folklore,\nwho is the\nstoryteller of\n'One Thousand and\nOne Nights'?\n","Kanaan","Hatshepsut","Homer","Scheherazade","Scheherazade");
00108     questions q28("Which of these\nNBA franchises\nhas never signed\nLeBron James?\n", "Miami Heat","Los Angeles\n   Lakers","Cleveland\n   Cavaliers","Boston Celtics","Boston Celtics");
00109     questions q29("What is the color\nof Donald Duck's\nbowtie?\n","Yellow","Green","Red","Blue","Red");
00110     questions q30("What was the\nfirst successful\nvaccine developed\nin history?\n","Smallpox","Rabies","Cholera","Scarlet Fever","Smallpox");
00111 
00112     arr[0] = q1;
00113     arr[1] = q2;
00114     arr[2] = q3;
00115     arr[3] = q4;
00116     arr[4] = q5;
00117     arr[5] = q6;
00118     arr[6] = q7;
00119     arr[7] = q8;
00120     arr[8] = q9;
00121     arr[9] = q10;
00122     arr[10] = q11;
00123     arr[11] = q12;
00124     arr[12] = q13;
00125     arr[13] = q14;
00126     arr[14] = q15;
00127     arr[15] = q16;
00128     arr[16] = q17;
00129     arr[17] = q18;
00130     arr[18] = q19;
00131     arr[19] = q20;
00132     arr[20] = q21;
00133     arr[21] = q22;
00134     arr[22] = q23;
00135     arr[23] = q24;
00136     arr[24] = q25;
00137     arr[25] = q26;
00138     arr[26] = q27;
00139     arr[27] = q28;
00140     arr[28] = q29;
00141     arr[29] = q30;
00142 }
00143 
00144 void readQuestion()
00145 {
00146     arr[questionIndex].shuffleOptions();
00147     //uLCD.cls();
00148     uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00149     uLCD.locate(0,0);
00150     uLCD.printf(arr[questionIndex].getQuestion().c_str());
00151     p = arr[questionIndex].getAllOptions();
00152     uLCD.printf("\n1: ");
00153     uLCD.printf((*(p)).c_str());
00154     uLCD.printf("\n2: ");
00155     uLCD.printf((*(p+1)).c_str());
00156     uLCD.printf("\n3: ");
00157     uLCD.printf((*(p+2)).c_str());
00158     uLCD.printf("\n4: ");
00159     uLCD.printf((*(p+3)).c_str());
00160 }
00161 
00162 int main()
00163 {
00164     uLCD.baudrate(3000000); // lcd settings + randomize questions
00165     srand(time(NULL));
00166     addQuestions();
00167     shuffleQuestions();
00168     uLCD.display_control(PORTRAIT);
00169 
00170     while(1) {
00171         if (questionIndex == 30) {
00172             uLCD.filled_rectangle(0, 0, 130, 130, BLACK); //game over + win screen
00173             uLCD.locate(0,0);
00174             uLCD.printf("The game is over;");
00175             wait(2);
00176             uLCD.locate(0,2);
00177             uLCD.printf("Player 1: %d", player1Score);
00178             uLCD.locate(0,4);
00179             uLCD.printf("Player 2: %d", player2Score);
00180             wait(3);
00181             // gameover....
00182             uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00183             uLCD.locate(1,6);
00184             if (player1Score > player2Score) {
00185                 uLCD.printf("PLAYER 1 WINS!!!");
00186             } else if (player1Score < player2Score) {
00187                 uLCD.printf("PLAYER 2 WINS!!!");
00188             } else {
00189                 uLCD.locate(3,6);
00190                 uLCD.printf("IT'S A TIE!!!");
00191             }
00192         } else {
00193             readQuestion(); //print questions
00194             lidar_enable = 1; // tell players that lidar can now work as buzzer
00195         }
00196         while ((!p1Val)&&(!p2Val)) {
00197             msg = -1;
00198             msg2 = -1;
00199 
00200             if (p1_enable) { //if player 1 is ready
00201                 p1Val = p1_enable;
00202                 myled1 = !myled1;
00203                 msg = (p1_ans + 1); //answer from player mbeds + 1 to make it numbers between 1-4
00204                 while (msg < 1);
00205                 //checking if answer is right
00206                 if (msg == 1) {
00207                     if ((*(p)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00208                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00209                         uLCD.locate(0,0);
00210                         uLCD.printf("Correct");
00211                         myRGBled.write(0.0,1.0);
00212                         speaker.period(1.0/1000.0); // 500hz period 
00213                         speaker =0.5;
00214                         wait(1);
00215                         speaker =0.0;
00216                         player1Score++;
00217                         p1Val = 1;
00218                         break;
00219                     } else {
00220                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00221                         uLCD.locate(0,0);
00222                         uLCD.printf("Incorrect");
00223                         myRGBled.write(1.0,0.0);
00224                         speaker.period(1.0/500.0); // 500hz period
00225                         speaker =0.5;
00226                         wait(1);
00227                         speaker =0.0;
00228                         player1Score--;
00229                         p1Val = 1;
00230                         break;
00231                     }
00232                 } else if (msg == 2) {
00233                     if ((*(p+1)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00234                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00235                         uLCD.locate(0,0);
00236                         uLCD.printf("Correct");
00237                         myRGBled.write(0.0,1.0);
00238                         speaker.period(1.0/1000.0);// 500hz period
00239                         speaker =0.5;
00240                         wait(1);
00241                         speaker =0.0;
00242                         player1Score++;
00243                         p1Val = 1;
00244                         break;
00245                     } else {
00246                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00247                         uLCD.locate(0,0);
00248                         uLCD.printf("Incorrect");
00249                         myRGBled.write(1.0,0.0);
00250                         speaker.period(1.0/500.0); // 500hz period
00251                         speaker =0.5;
00252                         wait(1);
00253                         speaker =0.0;
00254                         player1Score--;
00255                         p1Val = 1;
00256                         break;
00257                     }
00258                 } else if (msg == 3) {
00259                     if ((*(p+2)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00260                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00261                         uLCD.locate(0,0);
00262                         uLCD.printf("Correct");
00263                         myRGBled.write(0.0,1.0);
00264                         speaker.period(1.0/1000.0); // 500hz period
00265                         speaker =0.5;
00266                         wait(1);
00267                         speaker =0.0;
00268                         player1Score++;
00269                         p1Val = 1;
00270                         break;
00271                     } else {
00272                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00273                         uLCD.locate(0,0);
00274                         uLCD.printf("Incorrect");
00275                         myRGBled.write(1.0,0.0);
00276                         speaker.period(1.0/500.0); // 500hz period
00277                         speaker =0.5;
00278                         wait(1);
00279                         speaker =0.0;
00280                         player1Score--;
00281                         p1Val = 1;
00282                         break;
00283                     }
00284                 } else if (msg == 4) {
00285                     if ((*(p+3)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00286                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00287                         uLCD.locate(0,0);
00288                         uLCD.printf("Correct");
00289                         myRGBled.write(0.0,1.0);
00290                         speaker.period(1.0/1000.0); // 500hz period
00291                         speaker =0.5;
00292                         wait(1);
00293                         speaker =0.0;
00294                         player1Score++;
00295                         p1Val = 1;
00296                         break;
00297                     } else {
00298                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00299                         uLCD.locate(0,0);
00300                         uLCD.printf("Incorrect");
00301                         myRGBled.write(1.0,0.0);
00302                         speaker.period(1.0/500.0); // 500hz period
00303                         speaker =0.5;
00304                         wait(1);
00305                         speaker =0.0;
00306                         player1Score--;
00307                         p1Val = 1;
00308                         break;
00309                     }
00310                 }
00311             } else if (p2_enable) {
00312                 p2Val = p2_enable;
00313                 myled2 = !myled2;
00314                 msg2 = (p2_ans + 1);
00315                 while (msg2 < 1);
00316                 if (msg2 == 1) {
00317                     if ((*(p)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00318                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00319                         uLCD.locate(0,0);
00320                         uLCD.printf("Correct");
00321                         player2Score++;
00322                         myRGBled.write(0.0,1.0);
00323                         speaker.period(1.0/1000.0); // 500hz period
00324                         speaker =0.5;
00325                         wait(1);
00326                         speaker =0.0;
00327                         p2Val = 1;
00328                         break;
00329                     } else {
00330                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00331                         uLCD.locate(0,0);
00332                         uLCD.printf("Incorrect");
00333                         myRGBled.write(1.0,0.0);
00334                         speaker.period(1.0/500.0); // 500hz period
00335                         speaker =0.5;
00336                         wait(1);
00337                         speaker =0.0;
00338                         player2Score--;
00339                         p2Val = 1;
00340                         break;
00341                     }
00342                 } else if (msg2 == 2) {
00343                     if ((*(p+1)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00344                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00345                         uLCD.locate(0,0);
00346                         uLCD.printf("Correct");
00347                         myRGBled.write(0.0,1.0);
00348                         speaker.period(1.0/1000.0); // 500hz period
00349                         speaker =0.5;
00350                         wait(1);
00351                         speaker =0.0;
00352                         player2Score++;
00353                         p2Val = 1;
00354                         break;
00355                     } else {
00356                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00357                         uLCD.locate(0,0);
00358                         uLCD.printf("Incorrect");
00359                         myRGBled.write(1.0,0.0);
00360                         speaker.period(1.0/500.0); // 500hz period
00361                         speaker =0.5;
00362                         wait(1);
00363                         speaker =0.0;
00364                         player2Score--;
00365                         p2Val = 1;
00366                         break;
00367                     }
00368                 } else if (msg2 == 3) {
00369                     if ((*(p+2)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00370                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00371                         uLCD.locate(0,0);
00372                         uLCD.printf("Correct");
00373                         myRGBled.write(0.0,1.0);
00374                         speaker.period(1.0/1000.0); // 500hz period
00375                         speaker =0.5;
00376                         wait(1);
00377                         speaker =0.0;
00378                         player2Score++;
00379                         p2Val = 1;
00380                         break;
00381                     } else {
00382                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00383                         uLCD.locate(0,0);
00384                         uLCD.printf("Incorrect");
00385                         myRGBled.write(1.0,0.0);
00386                         speaker.period(1.0/500.0); // 500hz period
00387                         speaker =0.5;
00388                         wait(1);
00389                         speaker =0.0;
00390                         player2Score--;
00391                         p2Val = 1;
00392                         break;
00393                     }
00394                 } else if (msg2 == 4) {
00395                     if ((*(p+3)).compare(arr[questionIndex].getCorrectAnswer()) == 0) {
00396                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00397                         uLCD.locate(0,0);
00398                         uLCD.printf("Correct");
00399                         myRGBled.write(0.0,1.0);
00400                         speaker.period(1.0/1000.0); // 500hz period
00401                         speaker =0.5;
00402                         wait(1);
00403                         speaker =0.0;
00404                         player2Score++;
00405                         p2Val = 1;
00406                         break;
00407                     } else {
00408                         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00409                         uLCD.locate(0,0);
00410                         uLCD.printf("Incorrect");
00411                         myRGBled.write(1.0,0.0);
00412                         speaker.period(1.0/500.0); // 500hz period
00413                         speaker =0.5;
00414                         wait(1);
00415                         speaker =0.0;
00416                         player2Score--;
00417                         p2Val = 1;
00418                         break;
00419                     }
00420                 }
00421             }
00422         }
00423         //reset all values
00424         wait(3);
00425         p1Val = 0;
00426         p2Val = 0;
00427         myled1 = 0;
00428         myled2 = 0;
00429         msg = -1;
00430         msg2 = -1;
00431         lidar_enable = 0;
00432         myRGBled.write(0.0,0.0);
00433         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00434         uLCD.locate(0,0);
00435         uLCD.printf("Player 1: %d", player1Score);
00436         uLCD.locate(0,2);
00437         uLCD.printf("Player 2: %d", player2Score);
00438         wait(2);
00439         uLCD.filled_rectangle(0, 0, 130, 130, BLACK);
00440         questionIndex++;
00441     }
00442 }