Reuben Juste / Mbed 2 deprecated miniproject

Dependencies:   4DGL-uLCD-SE mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <math.h>
00002 #include "mbed.h"
00003 #include "uLCD_4DGL.h"
00004 #include <time.h>
00005 
00006 uLCD_4DGL uLCD(p9, p10,p11);
00007 int operand1;
00008 int operand2;
00009 int op;
00010 int answer;
00011 int ans = 0;
00012 int score = 0;
00013 int seconds = 30;
00014 float seed;
00015 Timer t;
00016 DigitalIn pb1(p13);
00017 DigitalIn pb2(p14);
00018 DigitalIn pb3(p15);
00019 DigitalIn pb4(p16);
00020 DigitalIn pb5(p17);
00021 DigitalIn pb6(p18);
00022 
00023 
00024 void generateProblem(){
00025     //generates random numbers between 1 and 40 as operands
00026     operand1 = rand() % 40 + 1;
00027     operand2 = rand() % 40 + 1;
00028     op = rand() % 3;
00029     
00030     //genearte random operator and appropriate answer
00031     if (op == 0){
00032         uLCD.locate(0,0);
00033         uLCD.printf("%d + %d\n",operand1, operand2);
00034         answer = operand1 + operand2;
00035         }
00036     else if (op == 1){
00037         uLCD.locate(0,0);
00038         uLCD.printf("%d - %d\n",operand1, operand2);
00039         answer = operand1 - operand2;
00040         }
00041     else if (op == 2){
00042         uLCD.locate(0,0);
00043         uLCD.printf("%d * %d\n",operand1, operand2);
00044         answer = operand1 * operand2;        
00045         }
00046   
00047     
00048     }
00049 int main()
00050 {
00051     pb1.mode(PullUp);
00052     pb2.mode(PullUp);
00053     pb3.mode(PullUp);
00054     pb4.mode(PullUp);
00055     pb5.mode(PullUp);
00056     pb6.mode(PullUp);
00057     
00058 
00059     bool randomGen = true;
00060     t.start();
00061     while(randomGen){ //push any button to start game
00062         uLCD.locate(1,0);
00063         uLCD.printf("Push any button to start the game");
00064         if(pb1==0 || pb2==0 || pb3==0 || pb4==0 || pb5==0 || pb6 == 0){
00065             seed = t.read_us();
00066         randomGen= false;
00067         t.reset();
00068         uLCD.cls();
00069         }
00070            } 
00071 
00072     
00073     
00074     srand((int)seed);
00075     
00076     
00077     
00078    while(true){
00079     generateProblem(); //generate problem
00080     uLCD.locate(1,14);
00081     uLCD.printf("Score: %d",score);//display score
00082     t.start();
00083     
00084     while(t.read() <= seconds){
00085            if(seconds - (int)t.read() < 10){ //display time limit
00086                uLCD.locate(13,1);
00087                uLCD.printf("%d",(seconds -(int)t.read()));
00088                } else{
00089            uLCD.locate(12,1);
00090            uLCD.printf("%d",(seconds -(int)t.read()));
00091            }
00092            //push pushbutton to add up your answer to approach correct answer
00093        if(pb1 == 0)
00094         {
00095         ans = ans + 1;
00096         uLCD.locate(1,2);
00097         uLCD.printf("%d",ans);
00098         wait(0.2);
00099         }
00100         else if(pb2 == 0)
00101         {
00102         ans = ans + 10;
00103          uLCD.locate(1,2);
00104          uLCD.printf("%d",ans);
00105          wait(0.2);
00106         }
00107         else if(pb3 == 0) 
00108         {
00109         ans = ans + 100;
00110          uLCD.locate(1,2);
00111          uLCD.printf("%d",ans);
00112          wait(0.2);
00113         }
00114         else if(pb4 == 0)
00115         {
00116         ans = ans  - 1;
00117          uLCD.locate(1,2);
00118          uLCD.printf("%d",ans);
00119          wait(0.2);
00120         }
00121         else if(pb5 == 0) 
00122         {
00123         ans = ans - 10;
00124          uLCD.locate(1,2);
00125          uLCD.printf("%d",ans);
00126          wait(0.2);
00127         }
00128         else if(pb6 == 0)
00129         {
00130         ans = ans - 100;
00131          uLCD.locate(1,2);
00132          uLCD.printf("%d",ans);
00133          wait(0.2);
00134         }
00135       }
00136       
00137       if( ans == answer){//if correct answer then add score and decrease time limit
00138         score += 1;
00139         seconds -=3;
00140         }else
00141         {
00142            uLCD.cls();
00143            uLCD.printf("You lose!\n Your score is %d",score);
00144            wait(5);
00145            score = 0;
00146            seconds = 30;
00147         }
00148         t.reset();
00149         
00150       ans = 0;
00151       
00152       uLCD.cls();
00153     
00154     
00155     }
00156     
00157 }