Beep Sound on Keypress

Hello guys,
I'm new to Arduino and looking for your help, I'm working on a Door Lock System tutorial which I found on arduinogetstarted.com with a 3x4 Keypad, LCD, BUZZER and a Relay, I modified the code to fit my own idea and I'm having a problem, simply I'm trying to make the BUZZER make a beep sound every time I press a key, I tried different methods using tone() but it didn't work, so please advice

Full code is attached

#include <Keypad.h>

#include <LiquidCrystal.h>

const byte ROWS = 4; //four rows

const byte COLS = 3; //three columns

char keys[ROWS][COLS] =

{

{'1','2','3'},

{'4','5','6'},

{'7','8','9'},

{'*','0','#'}

};

byte rowPins[ROWS] = {6 , A1, 2, 4}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {5, 7, 3}; //connect to the column pinouts of the keypad

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

const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int RELAY_PIN = A0; // the Arduino pin, which connects to the IN pin of relay

const String password_1 = "03121990"; // change your password here

String input_password;

int cursorColumn = 0;

void setup()

{

input_password.reserve(16); // maximum input characters is 33, change if needed

pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.

digitalWrite(RELAY_PIN, HIGH); // lock the door

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

}

void loop()

{

{

lcd.setCursor(0, 0);

lcd.print("ENTER PASSWORD:");

}

char key = keypad.getKey();

if (key)

{

if(key == '*')

{

input_password = ""; // reset the input password

lcd.clear();

} else if(key == '#')

{

lcd.clear();

if(input_password == password_1)

{

lcd.setCursor(0, 0);

lcd.print("CORRECT!");

lcd.setCursor(0, 1);

lcd.print("DOOR UNLOCKED!");

digitalWrite(RELAY_PIN, LOW); // unlock the door for 20 seconds

delay(2000);

lcd.clear();

delay(10000);

digitalWrite(RELAY_PIN, HIGH); // lock the door

}

else

{

lcd.setCursor(0, 0);

lcd.print("INCORRECT!");

lcd.setCursor(0, 1);

lcd.print("ACCESS DENIED!");

delay(2000);

lcd.clear();

}

input_password = ""; // reset the input password

}

else

{

if(input_password.length() == 0) {

lcd.clear();

}

input_password += key; // append new character to input password string

lcd.setCursor(input_password.length(), 2); // move cursor to new position

lcd.print('*'); // print * key as hiden character

}

}

}

sketch_oct21b.ino (2.3 KB)

  char key = keypad.getKey();
  if (key)
  {
  // This would be a good place to beep on every input

First I really appreciate your help, I've added { tone(A2, 750, 100) } ; in the mentioned location and it works but now the LCD screen are showing " * " on every blank place, it looks like this:

ENTER THE PASSWORD:*


sorry for inconvenience, i realized my fault, i edited the code and it's now:
{
lcd.setCursor(0, 0);
lcd.print("ENTER PASSWORD:");
}
char key = keypad.getKey();
{
if (key)
tone(A2, 750, 100);
}
if (key)
{
if(key == '*')
{
input_password = ""; // reset the input password
lcd.clear();

it worked, really appreciate your help and thanks you :slight_smile:

Karma for the most amazing code tagging I've ever seen!

-jim lee

Excuse my rusty experience in forums :grin: , I didn't use them for 15 years and that is about half of my age, and thanks for the Karma, anyway i don't know what is the Karma, could you explain should i worry??!! :o ;D

It's like a pat on the back.

-jim lee

OP

Watch your placement of braces { and }.

{
if (key)
tone(A2, 750, 100);
}


Use ‘CRTL T’ to format your sketches.