#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 ?
2 Answers 2
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 = "";
}
}
-
Nice answer ! You corrected the code I wasn't able to ! (+1)Sam Ruben Abraham– Sam Ruben Abraham2021年04月25日 07:18:51 +00:00Commented Apr 25, 2021 at 7:18
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.
-
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.Edgar Bonet– Edgar Bonet2021年04月24日 08:16:57 +00:00Commented 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 !! :DSam Ruben Abraham– Sam Ruben Abraham2021年04月24日 13:35:00 +00:00Commented Apr 24, 2021 at 13:35
void loop()
shell
app. You'll see the same strange thing happeningvoid loop()
in you sketch and see if it works.