Project 12 (true/false not working?)


I’m quite new to arduino and this is the first problem I’ve encountered.

I attached correctly the project to the breadboard (I checked 6 times). Yet the piezo is not reading anything.

I only have the red LED on and the Serial monitor shows. “The box is unlocked.
The box is locked.” which is expected when you first run the code.

I have checked my code and I noticed the my “true/false” statements were not colored as it suggests in the book (I will attach a printscreen)

Also below I copy pasted my code in case people want to have a closer look.

Looking forward to answers.


#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");
  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; 

First of all, check your line 37.

Secondly, have you missed out a large chunk of code on purpose? It starts:

if(numberOfKnocks >= 3)

I have it in code i just miss clicked when I Copy-Pasted.

Line 37 is good.

any idea why "true/false" doesn't change color in the IDE?

Line 37 is good.

if(switchVal == HIGH);

Do you really want to do nothing when switchVal is HIGH?
If not, remove the semicolon.

any idea why “true/false” doesn’t change color in the IDE?

It is highlighted, but in the default syntax highlight style the color is black.

     <style token="LITERAL_BOOLEAN" fg="000000" bold="false"/>