Knock lock reset

My knock lock will only reset if I use the reset on the uno board, not the switch on the breadboard. My understanding is that the knockVal is not reseting to zero once the switch is pressed. I tried adding "int knockVal = 0" to the reset loop, but this had no effect on the CCT.

Hi, please explain what your project is and what it is doing, also a picture of your project would help too.

At the moment we have no idea of what your problem is or what you are trying to achieve.

You have to regard us as people who have never heard of your project and no nothing of how it works.

Tom...... :) Also please do not double post your requests.

Are you asking for help or just saying that your lock doesn’t work?

How are we supposed to know what is wrong when you don’t give us the code?

I’m sure there is nothing wrong with the CCT as it works fine the first time.

Here is my Code.

#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!”);
delay (1000);
}
}
if(locked == true){
knockVal = analogRead(piezo);
if(numberOfKnocks < 3 && knockVal > 0){
if(checkForKnock(knockVal) == true){
numberOfKnocks++;
}
Serial.print(3-numberOfKnocks);
Serial.print(" more knocks to go");
}
if(numberOfKnocks >= 3){
locked = false;
myServo.write(0);
delay(20);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.print(“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.println(value);
return false;
}
}

Please use code tags to post code.

You never reset numberOfKnocks, so it's always going to be >=3 after the first unlocking.

Sorry I'm new to this, what are code tags?

Where would I put the reset line of code?

Code tags are explained here.

You put the reset line wherever you want to reset the number. I'd guess a good place for it would be right after the moment the lock is unlocked.

Thanks for that, I added an additional if statement on row 5 and now works correctly. XD

void loop(){
  if(locked == false){
    switchVal = digitalRead(switchPin);
    if(switchVal == HIGH){
      locked = true;
      if (numberOfKnocks >=3){
        numberOfKnocks = 0;
      }
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);
      myServo.write(90);
      Serial.println("The box is locked!");
      delay (1000);