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() {
myServo.attach(9);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
digitalWrite(greenLED, HIGH);
myServo.write(0);
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);
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 unlcoked!");
}
}
}
boolean checkForKnock(int value){
if(value > quietKnock && value < loudKnock){
digitalWrite(yellowLED, HIGH);
delay(50);
digitalWrite(yellowLED, LOW);
Serial.print("Valid knock of value ");
Serial.println(value);
return true;
}
else{
Serial.print("Bad knock value ");
Serial.println(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: