Project 12


I am very new to Arduino and have started to work through the examples in the Project book of the Starter kit.

I have reach project 12 however I am facing a strange situation that I wasn't able to find a solution for on the forums.

I am able to execute all of the lines of code (that is unlock, lock, knock 3 times and unlock again). However, after line 52 servo motor tends to stay in the unlocked position and won't allow me to lock again. Even when I try to press the push button to make the servo motor go into a lock ed position, it would not respond.

I tried to run a similar code that I found on the forums and the circuit works fine. However when I run my code, I am getting an error.

I believe that I wrote the exact same code as what is put out there but for some reason I am getting a glitch in my program. I have tried to compare both my program and the forum's line by line and haven't been able to find any discrepancy. I was hoping someone can have a look at my program and maybe identify something that I am not seeing.

please see attached.

Project_12.ino (1.82 KB)

locked == false;

'==' is for compare, '=' is for assignment

Please learn to post code inline unless it exceeds close to 9000 characters (max size of a post).

** **[code]** **

Paste your code after that.
** **[/code]** **
after that.

Your code below

#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(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(switchPin, INPUT);

  digitalWrite(greenLed, HIGH);
  Serial.println("The box is unlocked");    
  // put your setup code here, to run once

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.print(3 - numberOfKnocks);
      Serial.println(" more knocks to go");

    if(numberOfKnocks >= 3){
     locked == false;
     digitalWrite(greenLed, HIGH);    
     digitalWrite(redLed, LOW); 
     Serial.println("The box is unlocked!");  
     numberOfKnocks = 0;   

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

What does your serial output say?

Ok thank you very much sterretje.

I changed the expression "locked == false" to "locked = false" and it works now. Sorry for the late reply. I didn't know I got a reply for my post until just now. I assumed that would get a notification or sometime. Like I said, I am very new to this. Thanks again.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.