Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This is a simple mathemathic game I made using arduino and
- * 1602 LCD Module
- * Don't forget to open the Serial Monitor in order to play
- * Made by Naor Leizer
- * Enjoy!
- */
- #include <LiquidCrystal.h>
- void printscore();
- // change according to your connected module
- int RS = 7, E = 8, D4 = 9, D5 = 10, D6 = 11, D7 = 12;
- // declaring LCD
- LiquidCrystal lcd (RS, E, D4, D5, D6, D7);
- // score counting
- int score = 0;
- void setup()
- {
- // use for random seed
- pinMode(A0, INPUT);
- // configure lcd size and usage
- lcd.begin(16,2);
- // clear lcd before first use
- lcd.clear();
- // make sure Serial Monitor is open
- if(Serial == false)
- {
- lcd.print("O Serial Monitor");
- // Dots loading animation while waiting for Serial Monitor to open
- while(Serial == false)
- {
- // setting cursor to start of second line
- lcd.setCursor(0,1);
- for(int i = 0; i < 16; i++)
- {
- // check if Serial Monitor has opened; if so, continue
- if(Serial == true)
- break;
- lcd.print(".");
- delay(100);
- }
- // setting cursor to start of second line
- lcd.setCursor(0,1);
- for(int i = 0; i < 16; i++)
- {
- // check if Serial Monitor has opened; if so, continue
- if(Serial == true)
- break;
- lcd.print(" ");
- delay(100);
- }
- }
- }
- // clear lcd
- lcd.clear();
- // start Serial communication
- Serial.begin(9600);
- // greating message
- lcd.print("Welcome To my");
- lcd.setCursor(0,1);
- lcd.print("Math Game!");
- delay(2000);
- } // setup finished
- void loop()
- {
- // game finish when score is 10
- while(score < 10)
- {
- lcd.clear();
- // equation generator and printer
- equationout();
- // print score if it is not 10
- if(score != 10)
- printscore();
- }
- lcd.clear();
- // finish message and animation
- lcd.print(" Well Done!");
- lcd.setCursor(0,1);
- lcd.print("Press Reset");
- for(int i = 0; i < 2; i++)
- lcd.scrollDisplayRight();
- delay(4000);
- while(1)
- {
- for(int i = 0; i < 2; i++)
- {
- lcd.scrollDisplayRight();
- delay(350);
- }
- for(int i = 0; i < 4; i++)
- {
- lcd.scrollDisplayLeft();
- delay(350);
- }
- for(int i = 0; i < 2; i++)
- {
- lcd.scrollDisplayRight();
- delay(350);
- }
- }
- }
- void equationout()
- {
- // read random number from UNPLUGGED analog 0 pin
- randomSeed(analogRead(A0));
- // integers to use in game; var1 & var2 are the numbers in equation; opseed used later to generate random number to choose operation
- int var1 = random(11), var2 = random(11), result, opseed = random(0, 100);
- // make sure that var1 is greater than var2 to avoid negative results; if so, swap them
- if(var1 < var2)
- {
- var1 = var1 ^ var2;
- var2 = var1 ^ var2;
- var1 = var1 ^ var2;
- }
- // character to hold operation (+ / - / *)
- char op;
- // operation chooser
- if( opseed <= 33)
- {
- op = '+';
- result = var1 + var2;
- }
- else if(opseed > 33 && opseed <= 66)
- {
- op = '-';
- result = var1 - var2;
- }
- else if(opseed > 66)
- {
- op = '*';
- result = var1 * var2;
- }
- // build equation to print to Serial & lcd
- String output = String(var1) +' ' + op + ' ' + String(var2) + " =";
- // print equation
- lcd.print(output);
- Serial.println(output);
- // check if result is correct
- if(result == getanswer())
- {
- lcd.clear();
- lcd.print("Right Answer!");
- lcd.setCursor(0,1);
- lcd.print("Well Done!");
- // add one point to score
- score++;
- }
- else
- {
- // print wrong answer and generate new equation
- lcd.clear();
- lcd.print("Wrong Answer!");
- }
- delay(800);
- }
- int getanswer()
- {
- lcd.setCursor(0,1);
- lcd.print("Please Answer");
- // set cursor to appropriate loaction afer '=' sign and blink for animation sake
- lcd.setCursor(8, 0);
- lcd.blink();
- // user input store
- String input;
- // bool for do-while loop
- bool digit;
- // handle serial input
- do
- {
- // reset digit
- digit = false;
- // wait for serial input
- while(Serial.available() == 0);
- // get serial input
- input = Serial.readString();
- // hack to end game imidiatly - for developing purpose only
- if(input.toInt() == 257)
- {
- score = 10;
- lcd.noBlink();
- return 0;
- }
- // check input is a number
- for(int i = 0; i < input.length(); i ++)
- if(isdigit(input[i]) == false)
- digit = true;
- if(digit == true)
- {
- // print message if non numric character found
- lcd.setCursor(0,1);
- lcd.print("Numbers Only!");
- // set cursor to continue blink in the appropriate location (after '=' sign)
- lcd.setCursor(8, 0);
- // wait for new input
- continue;
- }
- }
- while(digit == true);
- // stop blinking animation
- lcd.noBlink();
- // print numric answer to serial & lcd
- lcd.print(input);
- Serial.println(input);
- delay(400);
- // return user answer
- return input.toInt();
- }
- // print user score so far
- void printscore()
- {
- lcd.clear();
- lcd.print("Your Score: " + String(score));
- lcd.setCursor(0,2);
- lcd.print("Keep Going!");
- delay(1500);
- }
Add Comment
Please, Sign In to add comment