project 12 / knock lock: problem reading the input value of piezo

Comme:
Hi to everyone!

This is my first post and i hope is going to be helpful.

I found somewhere in the internet another way to connect the piezo. It works, and the values returned (in my case) are between 1 and 10. I attached the scheme of the connections.
Furthermore, the code in the book when the counter goes to three is wrong: it does not reset the counter numberOfKnocks, causing a persistent state of "unlocked".

Here is the code:

if(numberOfKnocks >= 3){

locked = false;
      myServo.write(0);
      delay(20);
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW);
      Serial.println("The box is unlocked!");
      numberOfKnocks = 0; //line added
    }

Thank you Comme! That helped me so much. I was having such a hard time figuring out the wiring. Now it's working, and I can finally go to bed! :slight_smile: Thanks again!