Hi, I'm am a very new user to Arduino, but since I have a background in C++ (ancient history) I got tasked with creating the secret door mechanism for my brother's new escape room. I cobbled some concepts together from about 4 different places, and ended up with a secret knock lock in addition to a wireless route to lock (and if necessary unlock) via a computer. The wireless has been working great, and I finally got the lock behaving properly, but last night I tried to incorporate the piezo, and it is not my friend. I had done the secret knock project that came in the starter kit book, so I thought I knew what I was expecting – wrong. The readings either come back with a constant stream of bad knock between 300-400 or higher (when nothing is going on in the room). I tried switching from A0 to A5 and now it will give me random readings which unlock the door all by itself. I’m afraid that I damaged the board or something. Has anyone seen this type of thing before?
Thanks for your help! Diane
const int piezo = A5;
const int LockPin=13;
int WirelessInput;
int knockVal;
const int quietKnock=500;
const int loudKnock=800;
boolean locked=false;
int numberOfKnocks=0;
void setup() {
Serial.begin(9600);
pinMode(LockPin, OUTPUT);
}
void loop() {
//check wireless
if (Serial.available()>0) {
WirelessInput = Serial.read();
if(WirelessInput == 'U') {
digitalWrite(LockPin, LOW);
Serial.println("The door is unlocked!");
locked=false;
}
if(WirelessInput == 'u') {
digitalWrite(LockPin, LOW);
Serial.println("The door is unlocked!");
locked=false;
}
if(WirelessInput == 'L'){
digitalWrite(LockPin, HIGH);
Serial.println("The door is locked!");
locked=true;
}
if(WirelessInput == 'l'){
digitalWrite(LockPin, HIGH);
Serial.println("The door is locked!");
locked=true;
}
}
if(locked==true)
{
knockVal=analogRead(piezo);
if(numberOfKnocks < 3 && knockVal > 0)
{
if(checkForKnock(knockVal)==true)
{
numberOfKnocks++;
}
Serial.print(3-numberOfKnocks);
Serial.println(" more knocks to go");
}
if(numberOfKnocks>=3)
{
digitalWrite(LockPin, LOW);
delay(20);
Serial.println("The box is unlocked!");
locked=false;
numberOfKnocks=0;
}
}
}
boolean checkForKnock(int value){
if(value>quietKnock&&value<loudKnock)
{
Serial.print("Valid knock of value ");
Serial.println(value);
return true;
}
else
{
Serial.print("Bad knock value ");
Serial.println(value);
return false;
}
}

