Help with coding please

I’m trying to develop code in Arduino ide so that when I put the right passcode in on 4x4 keypad and press # button it makes the buzzer make a sound and when I put the wrong passcode in it makes a red led light up, for some reason my code is not working, any recommendations?

#include <Keypad.h>
#include <ezBuzzer.h>

const int BUZZER_PIN = 12;
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
const int LED = 11;

const byte keys [4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2,};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
ezBuzzer buzzer(BUZZER_PIN);

String inputString;
long inputInt;

void setup() {
Serial.begin(9600);
inputString.reserve(10);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED, OUTPUT);
}

void loop() {

//keypad.getKey();
buzzer.loop();
char Key = keypad.getKey();

if (4557 == '#') {
  Serial.println(Key);
  buzzer.beep(100);
}
else if (4557 == false) {
            digitalWrite(11, HIGH);
        }

}

Please edit your post to add code tags.

The numerical value of the ASCII character '#' is 35 decimal, so the comparison to another number is always false.

So what would I write instead?

It might make sense to compare the value of Key to '#'.

How would I make it so when you press # you can enter passcode

If you do an internet search for the keywords "arduino keypad password", you will find lots of helpful tutorials.

I'm not going to attempt to debug your code or write the program for you, but I'll give you some tips.

"Develop" your code a little at a time. You should always work on input (your buttons) and output (your LED & buzzer) separately before putting everything together.

It usually helps to start with something simple like the blink example or the button example and delete when you don't need and add one or two lines of code at a time, and then test-compile and if you can do something "useful" test-run. Make sure everything is working before moving-on and adding more code.

Now, that isn't as simple a starting at the top and working-down... It takes some practice because the compiler has to see a "complete program" For example, if you chop-off the bottom half of a program it's not going to compile.

Take advantage of the serial monitor to "print out" little messages and variable values. Just for example, you say "reading buttons" and/or you can read the buttons and print "button 1" pressed, etc. That way you can test little parts of your code and you can "see" what your program is doing (or not doing) when it doesn't behave as you expect.

When you don't need those messages anymore you can "comment them out" and the compiler will ignore them (they won't be added to the code you download into the Arduino.) And if you need to use them again for later debugging you can remove the comment marks.

Once your buzzer and LED are working, you can also use those to give you "hints" about what the program is doing. But you can get more information from with the serial monitor.

1 Like

I have been trying to get my arduino code to work, what I want the code to do is when you put the passcode 4557 into the keypad it turns on buzzer and when you put wrong code in it turns on red led, I have tried to add a string to key presses but code still does not work any help please would be greatly appreciated as I’m a beginner

const int BUZZER_PIN = 12;
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
const int LED = 11;

const byte keys [4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2,};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
ezBuzzer buzzer(BUZZER_PIN);

String password ="4557";
String input_password;

void setup() {
Serial.begin(9600);
input_password.reserve(32);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED, OUTPUT);
}

void loop() {
buzzer.loop();
char Key = keypad.getKey();

if (4557){
  Serial.println(4557);
  digitalWrite (BUZZER_PIN,HIGH);

}
else {
            digitalWrite(11, HIGH);
        }

}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

if (4557)

This will always be true

You need to read the number digit by digit and compare it to the valid entry when complete

You will get some ideas from Keypad data entry. A beginners guide

Same question here >>> Help with coding please

Cross posting is rude.

@vilk040503

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

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