I'm working on a kit project. Learning. The project is a LOCK that gets close with a switch and opens with three knocks on a piezo (? ). There is extensive debugging through serial and LEDs confirm the different status points so I can fairly be sure that all the rest works. I planned to take ahead this example for porting to Nano Every so I need to understand better and make it work.
The servo motor (out of the kit) should rotate 90° form open to close when the close command is given and viceversa. Only change I made on the drawing was to change the connection because the motor wires are stuck together and cannot be position as shown. It does not move.
Cannot check straight away the motor alone. I used in a previous project so it should be in working conditions.
I attach the sketch and the actual breadboard picture with schematics.
Tks
F
// Sketch PROGETTO 12 - FS20200217
#include <Servo.h> // import the library
Servo myServo; // create an instance of the Servo library
const int piezo = A0; // pin the piezo is attached to
const int switchPin = 2; // pin the switch is attached to
const int yellowLed = 3; // pin the yellow LED is attached to
const int greenLed = 4; // pin the green LED is attached to
const int redLed = 5; // pin the red LED is attached to
int knockVal; // variable for the piezo value
int switchVal; // variable for the switch value
// variables for the high and low limits of the knock value
const int quietKnock = 10;
const int loudKnock = 100;
boolean locked = false; // variable to indicate if locked or not
int numberOfKnocks = 0; // how many valid knocks you've received
void setup() {
myServo.attach(9); // attach the servo to pin 9
// make the LED pins outputs
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(switchPin, INPUT); // set the switch pin as an input
Serial.begin(9600); // start serial communication for debugging
digitalWrite(greenLed, HIGH); // turn the green LED on
myServo.write(0); // move the servo to the unlocked position
Serial.println("the box is unlocked!"); //DEBUG print status to the Serial Monitor
}
void loop() {
// if the box is unlocked
if (locked == false) {
switchVal = digitalRead(switchPin); // read the value of the switch pin
if (switchVal == HIGH) {
locked = true; // if the button is pressed, lock the box i.e. set the locked variable to "true"
// change the status LEDs
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
myServo.write(90); // move the servo to the locked position
Serial.println("the box is locked!"); //DEBUG print status to the Serial Monitor
delay(1000); // wait for the servo to move into position
}
}
// if the box is locked
if (locked == true) {
knockVal = analogRead(piezo); // check the value of the piezo
if (numberOfKnocks < 3 && knockVal > 0) { // if there are not enough valid knocks
if (checkForKnock(knockVal) == true) { // check to see if the knock is in range
numberOfKnocks++; // increment the number of valid knocks
}
// DEBUG print status of knocks
Serial.print(3 - numberOfKnocks);
Serial.println(" more knocks to go");
}
}
// if there are three knocks
if (numberOfKnocks >= 3) {
locked = false; // unlock the box
myServo.write(0); // move the servo to the unlocked position
delay(20); // wait for it to move
// change status LEDs
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println("the box is unlocked!"); //DEBUG
numberOfKnocks = 0;
}
}
// this function checks to see if a detected knock is within max and min range
boolean checkForKnock(int value) {
// if the value of the knock is greater than the minimum, and larger
// than the maximum
if (value > quietKnock && value < loudKnock) {
digitalWrite(yellowLed, HIGH); // turn the status LED on
delay(50);
digitalWrite(yellowLed, LOW);
Serial.print("Valid knock of value "); // DEBUG print out the status
Serial.println(value);
return true; // return true
}
// if the knock is not within range
else {
Serial.print("Bad knock value "); // DEBUG print status
Serial.println(value);
return false; // return false
}
}