naorleizer

Arduino Mathematic Game

Oct 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. /* This is a simple mathemathic game I made using arduino and
  2.  *  1602 LCD Module
  3.  *  Don't forget to open the Serial Monitor in order to play
  4.  *  Made by Naor Leizer
  5.  *  Enjoy!
  6.  */
  7.  
  8.  
  9. #include <LiquidCrystal.h>
  10.  
  11. void printscore();
  12.  
  13. // change according to your connected module
  14. int RS = 7, E = 8, D4 = 9, D5 = 10, D6 = 11, D7 = 12;
  15.  
  16. // declaring LCD
  17. LiquidCrystal lcd (RS, E, D4, D5, D6, D7);
  18.  
  19. // score counting
  20. int score = 0;
  21.  
  22. void setup()
  23. {
  24.   // use for random seed
  25.   pinMode(A0, INPUT);
  26.  
  27.   // configure lcd size and usage
  28.   lcd.begin(16,2);
  29.   // clear lcd before first use
  30.   lcd.clear();
  31.  
  32.   // make sure Serial Monitor is open
  33.   if(Serial == false)
  34.     {
  35.       lcd.print("O Serial Monitor");
  36.  
  37.       // Dots loading animation while waiting for Serial Monitor to open
  38.       while(Serial == false)
  39.       {
  40.         // setting cursor to start of second line
  41.         lcd.setCursor(0,1);
  42.  
  43.         for(int i = 0; i < 16; i++)
  44.         {
  45.           // check if Serial Monitor has opened; if so, continue
  46.           if(Serial == true)
  47.             break;
  48.  
  49.           lcd.print(".");
  50.           delay(100);
  51.         }
  52.  
  53.         // setting cursor to start of second line
  54.         lcd.setCursor(0,1);
  55.         for(int i = 0; i < 16; i++)
  56.         {
  57.           // check if Serial Monitor has opened; if so, continue
  58.           if(Serial == true)
  59.             break;
  60.  
  61.           lcd.print(" ");
  62.           delay(100);
  63.         }
  64.       }
  65.     }
  66.  
  67.   // clear lcd
  68.   lcd.clear();
  69.  
  70.   // start Serial communication
  71.   Serial.begin(9600);
  72.  
  73.   // greating message
  74.   lcd.print("Welcome To my");
  75.   lcd.setCursor(0,1);
  76.   lcd.print("Math Game!");
  77.   delay(2000);
  78. } // setup finished
  79.  
  80. void loop()
  81. {
  82.   // game finish when score is 10
  83.   while(score < 10)
  84.   {
  85.     lcd.clear();
  86.  
  87.     // equation generator and printer
  88.     equationout();
  89.  
  90.     // print score if it is not 10
  91.     if(score != 10)
  92.       printscore();
  93.   }
  94.   lcd.clear();
  95.  
  96.   // finish message and animation
  97.   lcd.print(" Well Done!");
  98.   lcd.setCursor(0,1);
  99.   lcd.print("Press  Reset");
  100.   for(int i = 0; i < 2; i++)
  101.     lcd.scrollDisplayRight();
  102.   delay(4000);
  103.   while(1)
  104.   {
  105.     for(int i = 0; i < 2; i++)
  106.     {
  107.       lcd.scrollDisplayRight();
  108.       delay(350);
  109.     }
  110.     for(int i = 0; i < 4; i++)
  111.     {
  112.       lcd.scrollDisplayLeft();
  113.       delay(350);
  114.     }
  115.     for(int i = 0; i < 2; i++)
  116.     {
  117.       lcd.scrollDisplayRight();
  118.       delay(350);
  119.     }
  120.   }
  121. }
  122.  
  123. void equationout()
  124. {
  125.   // read random number from UNPLUGGED analog 0 pin
  126.   randomSeed(analogRead(A0));
  127.  
  128.   // integers to use in game; var1 & var2 are the numbers in equation; opseed used later to generate random number to choose operation
  129.   int var1 = random(11), var2 = random(11), result, opseed = random(0, 100);
  130.  
  131.   // make sure that var1 is greater than var2 to avoid negative results; if so, swap them
  132.   if(var1 < var2)
  133.    {
  134.     var1 = var1 ^ var2;
  135.     var2 = var1 ^ var2;
  136.     var1 = var1 ^ var2;
  137.    }
  138.  
  139.    // character to hold operation (+ / - / *)
  140.   char op;
  141.  
  142.   // operation chooser
  143.   if( opseed <= 33)
  144.   {
  145.     op = '+';
  146.     result = var1 + var2;
  147.   }
  148.   else if(opseed > 33 && opseed <= 66)
  149.     {
  150.       op = '-';
  151.       result = var1 - var2;
  152.     }
  153.   else if(opseed > 66)
  154.   {
  155.     op = '*';
  156.     result = var1 * var2;
  157.   }
  158.  
  159.   // build equation to print to Serial & lcd
  160.   String output = String(var1) +' ' + op + ' ' + String(var2) + " =";
  161.  
  162.   // print equation
  163.   lcd.print(output);
  164.   Serial.println(output);
  165.  
  166.   // check if result is correct
  167.   if(result == getanswer())
  168.   {
  169.     lcd.clear();
  170.     lcd.print("Right Answer!");
  171.     lcd.setCursor(0,1);
  172.     lcd.print("Well Done!");
  173.  
  174.     // add one point to score
  175.     score++;
  176.   }
  177.   else
  178.   {
  179.     // print wrong answer and generate new equation
  180.     lcd.clear();
  181.     lcd.print("Wrong Answer!");
  182.   }
  183.  
  184.   delay(800);
  185. }
  186.  
  187. int getanswer()
  188. {
  189.   lcd.setCursor(0,1);
  190.   lcd.print("Please Answer");
  191.  
  192.   // set cursor to appropriate loaction afer '=' sign and blink for animation sake
  193.   lcd.setCursor(8, 0);
  194.   lcd.blink();
  195.  
  196.   // user input store
  197.   String input;
  198.  
  199.   // bool for do-while loop
  200.   bool digit;
  201.  
  202.   // handle serial input
  203.   do
  204.   {
  205.     // reset digit
  206.     digit = false;
  207.  
  208.     // wait for serial input
  209.     while(Serial.available() == 0);
  210.  
  211.     // get serial input
  212.     input =  Serial.readString();
  213.  
  214.     // hack to end game imidiatly - for developing purpose only
  215.     if(input.toInt() == 257)
  216.     {
  217.       score = 10;
  218.       lcd.noBlink();
  219.       return 0;
  220.     }
  221.  
  222.     // check input is a number
  223.     for(int i = 0; i < input.length(); i ++)
  224.       if(isdigit(input[i]) == false)
  225.         digit = true;
  226.  
  227.     if(digit == true)
  228.     {
  229.       // print message if non numric character found
  230.       lcd.setCursor(0,1);
  231.       lcd.print("Numbers Only!");
  232.  
  233.       // set cursor to continue blink in the appropriate location (after '=' sign)
  234.       lcd.setCursor(8, 0);
  235.  
  236.       // wait for new input
  237.       continue;
  238.     }
  239.      
  240.   }
  241.   while(digit == true);
  242.  
  243.   // stop blinking animation
  244.   lcd.noBlink();
  245.  
  246.   // print numric answer to serial & lcd
  247.   lcd.print(input);
  248.   Serial.println(input);
  249.   delay(400);
  250.  
  251.   // return user answer
  252.   return input.toInt();
  253. }
  254.  
  255.  
  256. // print user score so far
  257. void printscore()
  258. {
  259.   lcd.clear();
  260.   lcd.print("Your Score: " + String(score));
  261.   lcd.setCursor(0,2);
  262.   lcd.print("Keep  Going!");
  263.   delay(1500);
  264. }
Add Comment
Please, Sign In to add comment