Arduino Project Book Project 12 Problem

Hi, I am trying to do every project in the book specified in the title and have had no problems until now. I have built the circuit twice and checked and rechecked my code but nothing is happening. The Arduino looks like it is running the setup portion of code as the green LED is on and the serial monitor gives me the message “The box is locked!” but when I press the button nothing happens and if I knock nothing happens either so I’m a bit stuck and confused as to what is going wrong.

This 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() {
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(greenLED, HIGH);
  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);
      Serial.println("The box is locked!");

  if(locked == true){
    knockVal = analogRead(piezo);
    if(numberOfKnocks < 3 && knockVal > 0){
      if(checkForKnock(knockVal) == true){
      Serial.println(" more knocks to go");

    if(numberOfKnocks >= 3){
      locked = false;
      digitalWrite(greenLED, HIGH);
      digitalWrite(redLED, LOW);
      Serial.println("The box is unlcoked!");

boolean checkForKnock(int value){
  if(value > quietKnock && value < loudKnock){
    digitalWrite(yellowLED, HIGH);
    digitalWrite(yellowLED, LOW);
    Serial.print("Valid knock of value ");
    return true;
    Serial.print("Bad knock value ");
    return false;

Again I have checked my code but if anyone can give me any suggestions I would appreciate it!

This is the diagram I am working from in the book and my Arduino:

Do you see anything on the Serial monitor after the initial message ?

UKHeliBob: Do you see anything on the Serial monitor after the initial message ?

No, the serial monitor doesn't change from the initial message of "The box is locked!" even if I press the button or make a noise, I get nothing, which is why I find this really weird. If stuff was showing in the monitor I know its just a component problem, but nothings happening in there either!

Try printing knockVal after reading the piezo pin

Try printing knockVal after reading the piezo pin

How do you mean, I don’t really know where I should place the print value?

const int piezo = A0;

After you read this pin.

The problem is on the code. Thats simply that you wrote at the second line of the loop :

"switchVal == digitalRead(switchPin);" instead of "switchVal = digitalRead(switchPin);"

Because the "==" is meaning equal and to and "=" is meaning "is taking".