Hello,
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.
Cheers!
#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");
}
}
}
}
boolean checkForKnock(int value)
{
if(value > quietKnock && value < loudKnock)
{
digitalWrite(yellowLED, HIGH);
delay(50);
digitalWrite(yellowLED, LOW);
Serial.print("Valid knock of value ");
Serial.print(value);
return true;
}
else
{
Serial.print("Bad knock Value ");
Serial.println(value);
return false;
}
}