Keypad sending out random numbers

So, I'm making a sort of dessert machine (don't mind me censoring what's written on the lcd) and am using a keypad to make the choices, however, I only use the numbers 1, 2, and 3 (first row and all first three columns). I applied changes to the code and downloaded it on the Arduino UNO. I opened the serial monitor and did the necessary to arrive at the phase which necessities input from the keypad. It was written "49" on the serial monitor, but I didn't even touch the keypad.

Here is the code:

#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
#include <Wire.h>
#include <NewPing.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

NewPing sonar(A0, A1, 21);

// Constants for row and column sizes
const byte ROWS = 1;
const byte COLS = 3;
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'}
};

// Connections to Arduino
byte rowPins[ROWS] = {6};
byte colPins[COLS] = {5, 4, 3};

// Create keypad object
Keypad keypad1 = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  Serial.begin(9600);		// Initialize serial communications with the PC
  while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();			// Init SPI bus
  mfrc522.PCD_Init();		// Init MFRC522
  delay(4);				// Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
  pinMode(3, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.clear();
}

bool dessertSel = false;

void loop() {
  lcd.setCursor(1, 0);
  lcd.print("Bla bla bla");
  lcd.setCursor(2, 1);
  lcd.print("bla bla bla bla");
  lcd.setCursor(2, 2);
  lcd.print("bla and bla");

  while (! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  dessertSel = true;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("bla bla bla");
  lcd.setCursor(0, 1);
  lcd.print("bla bla bla bla bla");
  lcd.setCursor(0, 2);
  lcd.print("bla bla bla bla");

  while (dessertSel == true) {
    int customKey = keypad1.getKey();
    if (customKey) {
      Serial.print(customKey);
    }
  }
}
``
Can i get an explanation for why is this happening?
Thanks!

oops i didn't format the code
did it now

49 is the ASCII character code for the '1' character.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post

i do know abt autoformat, but didn't use it. noticed a lot of errors in the post, gonna go fix it

 char customKey = keypad1.getKey();

Use:
Serial.print(char(customKey));
To print the character instead of the character code.

49 is the ASCII code for '1' so the code is acting as if you pressed the '1' key. Check your wiring carefully

Also, in the code you have this while loop

  while (dessertSel == true)
  {
    int customKey = keypad1.getKey();
    if (customKey)
    {
      Serial.print(customKey);
    }
  }

Nothing in the while loop changes the value of dessertSel so how will it ever end ?

it works, but when i get to that part, it automatically prints "1" without any action from me!

I added this line of code after Serial.print(customKey);
dessertSel = false;

Upload the following sketch and check that the Buttons 1, 2, 3 are working alright; else, fix the Keypad wiring first or program logic.

#include <Keypad.h>

const byte ROWS = 1;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] =
{
  {'1', '2', '3'}
};

byte rowPins[ROWS] = {6};
byte colPins[COLS] = {5, 4, 3};

Keypad keypad1 = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(9600);    //
}

void loop()
{
  char customKey = keypad1.getKey();
  if (customKey != 0)
  {
    Serial.print(customKey);
  }
}

2 n' 3 work, 1 doesn't, im gonna double-check my wiring

Check wiring against the connector pins of the Keyapd of Fig-1.


Figure-1:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.