Hello!
I have finished a model password door lock project with Arduino! I used a red LED, green LED, piezo buzzer, soft potentiometer, and 3 buttons. Now, I am just making improvements. When I enter the correct password, the Arduino plays a victory "charge" music. Now, I want to create an audible CLICK with the piezo (sounding like a lock opening) when I enter the correct password. But, I don't want it to sound like a little TAP. Can someone teach me how to do that?
Link to datasheet of piezo: http://www.sparkfun.com/datasheets/Components/CEM-1203.pdf
Picture of piezo (copy and paste to browser, and find CIRC-06):
http://myclass.peelschools.org/ele/7/25580/Resources/Arduino%20Resources/ARDX-EG-SPAR-PRINT-85-REV-10[1].pdf
Code for my project:
#include <MomentaryButton.h>
#include <Easy.h>
#include <avr/sleep.h>
#include <avr/power.h>
// Project 27 Keypad door lock
#include <EEPROM.h>
MomentaryButton button(2);
MomentaryButton sButton(3);
MomentaryButton nButton(4);
Song alarm(11);
String secretCode = "1234";
String code = "";
int position = 0;
boolean locked = true;
int note[] = {
262, 262, 392, 523, 392, 523};
int duration[] = {
100, 100, 100, 300, 100, 300};
int redPin = 9;
int greenPin = 8;
byte piezo = 11;
byte tries = 0;
boolean playing = 1;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
DDRD = 0;
PORTD = 0x1c;
loadCode();
flash();
updateOutputs();
button.setup();
sButton.setup();
nButton.setup();
}
void loop()
{
char key = getKey();
if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
getNewCode();
updateOutputs();
}
if (key == '#')
{
locked = true;
playing = 1;
code = "";
position = 0;
updateOutputs();
for(int i = 1; i <= 3; i++){
tone(11, 250, 250);
delay(250);
}
}
if (key >= '0' && key <= '9')///
{
code += (char)key;
position++;
}
if(!digitalRead(2)) tone(11, 500);
else noTone(11);
//if (position == 4)//
//{
if(code == secretCode && position == 4){
locked = false;
tries = 0;
updateOutputs();
for (int thisNote = 0; thisNote < 6 && playing; thisNote ++) {
// play the next note:
tone(11, note[thisNote]);
// hold the note:
delay(duration[thisNote]);
// stop for the next note:
noTone(11);
delay(25);
}
playing = 0;
}
else if (position == 4 && code != secretCode){
noTone(11);
alarm.siren();
alarm.siren();
noTone(11);
position = 0;
code = "";
tries++;
}
if(tries >= 3){
alarm.ambulance();
alarm.ambulance();
noTone(11);
sleep();
}
//}
}
void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
void getNewCode()
{
flash();
for (int i = 0; i < 4; i++ )//
{
char key;
key = getKey();
while (key == 0)
{
key = getKey();
}
flash();
secretCode[i] = key;
}
saveCode();
flash();
flash();
}
void loadCode()
{
if (EEPROM.read(0) == 1)
{
for(int i = 0; i < 4; i++)//
secretCode[i] = EEPROM.read(i+1);
}
}
void saveCode()
{
for(int i = 0; i < 4; i++){//
EEPROM.write(i+1, secretCode[i]);
}
EEPROM.write(0, 1);
tone(11, 500, 1000);
}
void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
delay(500);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
}
char getKey(){
button.check();
sButton.check();
nButton.check();
if(button.wasClicked()){
int reading = analogRead(A0);
reading = map(reading, 0, 1023, 0, 7);
return reading + 48;
}
if(sButton.wasClicked()) return '*';
if(nButton.wasClicked()) return '#';
return 0;
}
void sleep(){
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
DDRD = 0;
DDRB = 0;
PORTD = 0;
PORTB = 0;
sleep_enable();
power_adc_disable();
power_spi_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
MCUCR = _BV (BODS) | _BV (BODSE);
MCUCR = _BV (BODS);
sleep_mode();
}
Thanks!