2

#include "Keypad.h"

char Incoming_value = 0;
String password = "123456";
String input = "";
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0)
  {
    Incoming_value = Serial.read();
    Serial.print(Incoming_value);
    Serial.print("enter value");
    if (Incoming_value == '1') {
      digitalWrite(13, HIGH);
      Serial.print("ON");
    }
    else if (Incoming_value == '0') {
      Serial.print("OFF");
      digitalWrite(13, LOW);
    }
  }
  // Read the pushed button
  char key = keypad.getKey();
  input = input + key;
  // just print the pressed key
  if (key) {
    Serial.println(key);
  }
  if(key == '*' ){
    Serial.println(input);
    if( input == password){
      digitalWrite(13, HIGH);
    }
  }
  if(key == '#'){
    digitalWrite(13, LOW);
    input = "";
  }
  
/*
  // this checkes if 4 is pressed, then do something. Here  we print the text but you can control something.
  if (key == '4') {
    Serial.println("Key 4 is pressed");
          digitalWrite(13, HIGH);
      Serial.print("ON");
  }
    if (key == '5') {
    Serial.println("Key 5 is pressed");
          digitalWrite(13, LOW);
      Serial.print("ON");
  }
  */
}

when print input variable i get strange text like

⸮⸮⸮⸮⸮⸮⸮;⸮⸮ߵ⸮⸮⸮⸮u⸮⸮⸮⸮⸮⸮?/}⸮⸮⸮⸮⸮⸮~⸮⸮⸮⸮⸮-d

what i can do ?

5
  • Probably you haven't added a delay towards the end of void loop() Apr 24, 2021 at 2:34
  • As an example : download JohnsProject/ArduOS from Github and run the main sketch there after putting the line of code that invokes the registry to run the shell app. You'll see the same strange thing happening Apr 24, 2021 at 2:36
  • Also, try adding the delay as I said in void loop() in you sketch and see if it works. Apr 24, 2021 at 2:37
  • 3
    Have you confirmed that your IDE serial monitor baud rate setting is 9600 (which is what your code assumes it is)?
    – lurker
    Apr 24, 2021 at 3:28
  • @lurker , is my answer okay ? (Perhaps I can add your point on the baud rate setting in the Serial Monitor, if you wish ;) ) Apr 24, 2021 at 5:08

2 Answers 2

3

First of all you have to make sure, that you select the same baudrate in the Serial Monitor, that you used in your sketch with Serial.begin(). This error is very common and garbage values is the symptom of this.

But your code also has problems. You have the input variable of type String, which should hold the user input for comparison with the password. Directly after getting the key, you are appending this key to the string. You are assuming here, that keypad.getKey() is waiting for the user to actually press a key and then return that. But this is not the case. keypad.getKey() will return NO_KEY, when the user currently isn't pressing a key. This is defined in the Keypad library as '\0' (the null character), which marks the end of a string. Your code runs very fast, so you are adding more and more null characters to your string. Your strings gets bigger and bigger, filling your RAM very fast.

How to solve this: You should only use the return value of keypad.getKey(), if a key was actually pressed. Put all the code after it in an if statement, checking for NO_KEY:

// Read the pushed button
char key = keypad.getKey();
if(key != NO_KEY){
    input = input + key;
    // just print the pressed key
    if (key) {
        Serial.println(key);
    }
    if(key == '*' ){
        Serial.println(input);
        if( input == password){
            digitalWrite(13, HIGH);
        }
    }
    if(key == '#'){
        digitalWrite(13, LOW);
        input = "";
    }
}
1
  • Nice answer ! You corrected the code I wasn't able to ! (+1) Apr 25, 2021 at 7:18
1

PS: @lurker's suggestion in the comments - check the baud rate setting in your serial monitor as well.

EDIT: I admit that the code in the previous version of this answer was foolishly typed out by me due to lack of patience, so I'll make up for my mistake(not in code, but as a full answer).

First of all, I didn't understand what you wanted to do with the Incoming_value variable in your sketch, so I'm not gonna talk about it; I'd suggest adding a sufficiently small delay at the end of the code in the loop. This should provide the serial port some time to handle the incoming data and stop printing the confusing output (perhaps; just because delays make your microcontroller wait for a while to get things done elsewhere , especially where the reception of data is slow and thus avoid collisions)

Also note that I am not a CS student or an electronics geek, but I am speaking from my guesswork. Any correction is accepted.

2
  • Given that most often delays do more harm than good, what is the rationale for adding them here? I can see how they could help on a program that sends data to a very slow receiver, but this is not the case here. To make the code “run smoothly” is overly vague. Apr 24, 2021 at 8:16
  • 1
    @EdgarBonet sorry for the late reply. Note that at the place where the system is made to receive the keypresses, adding a delay allows the person to type the password (As in the example) key by key - and now that reminds me of how foolishly I have added the code in my answer. Thanks a lot !! :D Apr 24, 2021 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.