(code update at the bottom)
I changed it, and the values are now random because it's reading a value of an unconnected analog pin. Now, the randomization works properly (the values are pretty large at the moment, but I'll make them smaller later. So I have two questions. One, when the screen says "press any button to start", it actually says"0+0ss any button to start"... I have no idea why it's doing this because the equation happens in the loop section, the "press any button to start" is in the setup. Secondly, how do I make it type the characters in one after another?(at the moment, when I can type in numbers(and letters) it replaces the number I previously typed, so I can't get more than one character typed...) I think this is happening because I make the cursor go to that spot beforehand, but I need to do this because the cursor is somewhere else beforehand. how do I fix this? also, I got the 'C' to clear the second line when I'm typing, but how do I make it so I can only enter in numbers and not letters and other characters? I tried to do this: "if (key == '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'0'){lcd.print(key);}" It didn't work, the place where the number is supposed to go turns into some sort of glitchy square kinda thing.
code:
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const byte ROWS = 4;
const byte COLS = 4;
int val1;
int val2;
int result;
int Random;
int Random2;
bool state;
LiquidCrystal_I2C lcd(0x27, 16, 2);
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6, 9};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Maths Game 1.0");
lcd.setCursor(0, 1);
lcd.print("Loading...");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press any Button");
lcd.setCursor(0, 1);
lcd.print("to Start");
}
void loop(){
char key = keypad.getKey();
if(key){
lcd.clear();
state = true;
}
{
Random = random(0, 100);
Random2 = random(0, 100);
}
if(state == true){
val1 = analogRead(A0);
val2 = analogRead(A1);
result = val1+val2;
state = false;
}
lcd.setCursor(0, 0);
lcd.print(val1);
lcd.print("+");
lcd.print(val2);
lcd.setCursor(0, 1);
if(key ) {
lcd.print(key);
}
if (key == 'C') {
lcd.setCursor(0, 1);
lcd.print(" ");
}
if (key == 'A') {
}
}