COMPARING RANDOM VALUES TO NUMERIC KEYPAD INPUT

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.

Not sure of your problem. Can you explain what the code does and what is different than what you want?

FYI random(1, 9) will return numbers 1 to 8.

a random number between min and max-1 (long)

https://www.arduino.cc/en/Reference/Random

int randomValue1, randomValue2, randomValue3, randomValue4, randomValue5, randomValue6, randomValue7, randomValue8, randomValue9;

That really begs to be an array.

I'm trying to generate a random number with the use of random() function from 1-9 then comparing it to the keypad input.

amielmitra:
I'm trying to generate a random number with the use of random() function from 1-9 then comparing it to the keypad input.

So what happens when you run the program ?

based on the serial monitor it outputs 10 different random numbers from 1-9 but the problem is that it does not accept the inputs i do to the keypad. it leaves a space and then outputs also the "wut ze fuk" meaning it is either my input was wrong or it doesn't really accepts my keypad input at all

Put in a serial print to see what the keypad returns.

  char key = keypad.getKey();
  Serial.println(key);
  if (key != NO_KEY) {

What do you get?

groundfungus:
Put in a serial print to see what the keypad returns.

  char key = keypad.getKey();

Serial.println(key);
  if (key != NO_KEY) {




What do you get?

groundfungus:
Put in a serial print to see what the keypad returns.

  char key = keypad.getKey();

Serial.println(key);
  if (key != NO_KEY) {




What do you get?

groundfungus:
Put in a serial print to see what the keypad returns.

  char key = keypad.getKey();

Serial.println(key);
  if (key != NO_KEY) {




What do you get?

i just tested the keypad with that it gives me complete set from 1-9 but the problem is that when i try to incorporate it inside the randomValue, it doesn't accept the input or ignores it I dunno

doesn't accept the input or ignores it I dunno

Then put in serial prints to find out. What value does keyPressed hold?

Your code will only do the comparison if the key that you pressed is the '0' key. You subtract '0' (result is the number 0) and next you compare it a number of times to a value between 1 and 8 (both included); that will obviously always be false and repeatedly print "WUT ZE FUK?".

sterretje:
Your code will only do the comparison if the key that you pressed is the '0' key. You subtract '0' (result is the number 0) and next you compare it a number of times to a value between 1 and 8 (both included); that will obviously always be false and repeatedly print "WUT ZE FUK?".

any ideas on how can i do it? I'm trying to compare 10 random numbers with the range of (1-9) to the keypad input which is (1-9). I tried looping it but i still get the same thing. Thanks so much in advance

Solve this and your code will probably work

void loop() {
  char key = keypad.getKey();
  if (key != NO_KEY) {
    if (key == '0') {  <<-------

What is your thinking as to why you implement this condition?

Also remember that random(1,9) generates numbers from 1 to 8, not from 1 to 9.

Having an comparable problem. I think it is due to the "char" bit in initialising the keypad. Wich gives you an character as output for the keypad. Arduino can't compare a character as it has no value. Does anyone know how to change it?

Arduino can't compare a character as it has no value

Explain, please.