// Small math game based on Nuelectronics LCD screen with keypad
// Printing values on LCD screen with "Itoa()" function
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
char msgs[2][5] = {" * ", " + " };
int adc_key_val[5] ={30, 150, 360, 535, 760 }; // Analog in defines witch key is pressed
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
int value0; // new random value every loop
int value1; // first number
int value2; // second number
int value3; // 1*2
int value4; // a value close to value3 is answer proposal (to reduce key presses)
int x; // choose char * or + to display
char buf[12]; // "-2147483648\0" a probabely far to big buffer
void setup() {
lcd.init();
lcd.clear();
lcd.printIn("Start Game"); // multiplication game
delay(1000);
}
void loop() {
value0 = random(1,99); // without this you get the same "random" numbers each reset
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey){ // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey){
oldkey = key;
if (key == 4){ // most left button starts a new sum
if (value0 < 50){ // multiply when random value0 is below 50
value1 = random(1,9); // values used are below ten
value2 = random(1,9);
value3 = value1*value2;
x = 0; // choose char * to display
}
else{ // addition when random value0 is >= 50
value1 = random(1,50); // values used are below 50
value2 = random(1,49);
value3 = value1+value2; // add up to 99 maximum
x = 1; // choose char + to display
}
value4 = value3 + random(-5,+5); // proposed answer near correct answer
if (value4 <= 0){
value4 = value4 + 5; //
}
lcd.clear();
delay(500);
lcd.cursorTo(1, 0);
lcd.printIn(itoa(value1, buf, 10)); // display first number
lcd.printIn(msgs[x]); // display char * or +
lcd.printIn(itoa(value2, buf, 10)); // display 2nd number
delay(1000);
lcd.cursorTo(2, 0);
lcd.printIn(itoa(value4, buf, 10)); // display proposed answer
}
else{
if (key == 1){
value4 = value4 + 1; // up-button adds 1 to the answer
lcd.cursorTo(2, 0);
lcd.printIn(itoa(value4, buf, 10));
}
else{
if (key == 2){
if (value4 == 0){
value4 = value4 +1;
}
else{
value4 = value4 - 1; // down-button subtracts 1 from answer
}
lcd.cursorTo(2, 0);
lcd.printIn(" "); // to remove trailing zero below ten
lcd.cursorTo(2, 0);
lcd.printIn(itoa(value4, buf, 10));
}
else{
if (key == 0){ // right-button to check the answer
lcd.cursorTo(2, 6);
if (value4 == value3){
lcd.printIn("Okay");
delay(1500);
lcd.clear();
delay(500);
lcd.cursorTo(1, 0);
lcd.printIn("New game");
}
else{
lcd.printIn("False"); // wrong answer, new chance
delay(1500);
lcd.cursorTo(2, 6);
lcd.printIn(" ");
}
}
}
}
}
}
}
}
// Convert analog in to key number
int get_key(unsigned int input){
int k;
for (k = 0; k < NUM_KEYS; k++){
if (input < adc_key_val[k]){
return k;
}
}
if (k >= NUM_KEYS){
k = -1; // No valid key pressed
return k;
}
}