Project 12

Hi,

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).

Type
** **[code]** **

Paste your code after that.
Type
** **[/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() {
  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");    
  // 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);
    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.println(" more knocks to go");
    } 

    if(numberOfKnocks >= 3){
     locked == false;
     myServo.write(0);
     delay(20);
     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);
    delay(50);
    digitalWrite(yellowLed, LOW);
    Serial.print("Valid knock value ");
    Serial.println(value);
    return true;    
    }
  else{
    Serial.print("Bad knock value");
    Serial.println(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.