knock box

#include <Servo.h>
Servo myServo;
const int piezo = A0;
const int switchPin = 2;
const int yellowLed = 3;
const int greenLed = 4;
const int redLed = 5;
int knockVal;
int switchVal;
const int quietKnock = 10;
const int loudKnock = 100;
boolean locked = false;
int numberOfKnocks = 0;
void setup(){
myServo.attach(9);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
digitalWrite(greenLed, HIGH);
myServo.write(0);
Serial.println(“The box is unlocked!”);
}
void loop(){
if(locked == false){
switchVal = digitalRead(switchPin);
if(switchVal == HIGH){
locked = true;
digitalWrite(greenLed,LOW);
digitalWrite(redLed,HIGH);
myServo.write(90);
Serial.println(“The box is locked”);
}
if(numberOfKnocks >=3){
locked = true;
digitalWrite(greenLed,LOW);
digitalWrite(redLed,HIGH);
myServo.write(90);
Serial.println(“The box is locked”);
}
}
if(locked == true) {
knockVal = analogRead(piezo);
if(numberOfKnocks < 3 && knockVal > 0){
if (checkForKnock(knockVal) == true){
numberOfKnocks++;
}
Serial.print(3-numberOfKnocks);
}
if(numberOfKnocks >= 3){
if(locked = false);
myServo.write(0);
delay(20);
digitalWrite(greenLed,HIGH);
digitalWrite(redLed,LOW);
Serial.println(“The box is unlocked!”);
}
}
}
}
boolean checkForKnock(int value){
if(value > quietKnock && value < loudKnock){
digitalWrite(yellowLed, HIGH);
delay(50);
digitalWrite(yellowLed, LOW);
Serial.print("Valid knock of value ");
Serial.println(value);
return true;
}
else{
Serial.print("Bad Knock value ");
Serial.print(value);
return false;
}
}

this is my program and it is not reading the knocks correctly. any suggestions? or any thing wrong with the program that could fix the project

Tangential comment, but can you put that code in "code" tags please? "Modify" your post, highlight the code, press the "hash" (#) button above the smileys.

Yes mine does the same. Watching the serial monitor, every time the button is pressed, it is followed by a variable number of valid knocks. Some real knocks are ignored. I currently think the piezo is responding to spurious inputs but haven't finished debugging this.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.