My problem is that whenever I try to use random numbers and compare them to the input of the keypad, the program doesn't seem to accept or read the input from the keypad, especially when the loop is inside the random code.
I'm trying to display 10 different random numbers from 1-9 and then I need to compare them with the answer (numeric keypad input).
Here's my code:
#include<Keypad.h>
int randomValue1, randomValue2, randomValue3, randomValue4, randomValue5, randomValue6, randomValue7, randomValue8, randomValue9;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '0') {
int keyPressed = key - '0';
randomValue1 = random(1, 9);
Serial.println(randomValue1);
if (keyPressed == randomValue1) {
Serial.println("U SHIET1");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue2 = random(1, 9);
Serial.println(randomValue2);
if (keyPressed == randomValue2) {
Serial.println("U SHIET2");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue3 = random(1, 9);
Serial.println(randomValue3);
if (keyPressed == randomValue3) {
Serial.println("U SHIET3");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue4 = random(1, 9);
Serial.println(randomValue4);
if (keyPressed == randomValue4) {
Serial.println("U SHIET4");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue5 = random(1, 9);
Serial.println(randomValue5);
if (keyPressed == randomValue5) {
Serial.println("U SHIET5");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue6 = random(1, 9);
Serial.println(randomValue6);
if (keyPressed == randomValue6) {
Serial.println("U SHIET6");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
randomValue7 = random(1, 9);
Serial.println(randomValue7);
if (keyPressed == randomValue7) {
Serial.println("U SHIET7");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue8 = random(1, 9);
Serial.println(randomValue8);
if (keyPressed == randomValue8) {
Serial.println("U SHIET8");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
randomValue9 = random(1, 9);
Serial.println(randomValue9);
if (keyPressed == randomValue9) {
Serial.println("U SHIET9");
Serial.println(key);
} else {
Serial.println("WUT ZE FUK?");
Serial.println(key);
}
delay(2000);
}
}
}
I tried using the for loop at first because I needed to get and compare 10 random generated numbers from 1-9 with the 1-9 input of the numeric keypad.
I know my question is very easy for some but please help me. I'm new to the programming world. Thanks a lot.