Bables14cfulfordiupucmet
Created February 26, 2019

Secret Knock Lock (Project 9 )

Knock the correct amount of times on the buzzer to move the servo arm.

Secret Knock Lock (Project 9 )

Things used in this project

Story

Read more

Code

Project_9.ino

C/C++
#include <Servo.h> 
Servo servo9; // Pin connected to servo mpo
const int piezo = A0;    // Pin connected to piezo 
const int switchPin = 2; // Pin connected to servo 
const int yellowLed = 3; // Pin connected to yellow LED 
const int greenLed = 4;  // Pin connected to green LED 
const int redLed = 5;    // Pin connected to red LED
int knockVal;   // Value for the knock strength 
int switchVal;
const int quietKnock = 10; // Set min value that will be accepted 
const int loudKnock = 100; // Set max value that will be accepted 
boolean locked = false;    // A true or false variable 
int numberOfKnocks = 0;    // Value for number of knocks

void setup() {
  // put your setup code here, to run once:
servo9.attach(9);  
pinMode(yellowLed, OUTPUT);   // Set LED pins as outputs  
pinMode(greenLed, OUTPUT);  
pinMode(redLed, OUTPUT);  
pinMode(switchPin, INPUT);    // Set servo pin as input  
Serial.begin(9600);  
digitalWrite(greenLed, HIGH); // Green LED is lit when the                                  
// sequence is correct  
servo9.write(0);  
Serial.println("The box is unlocked!"); 
}

void loop() {
  // put your main code here, to run repeatedly:
if (locked == false) {    
  switchVal = digitalRead(switchPin);    
  if (switchVal == HIGH) {      
    locked = true;      
    digitalWrite(greenLed, LOW);      
    digitalWrite(redLed, HIGH);      
    servo9.write(90);      
    Serial.println("The box is locked!");      
    delay(1000);    
    }  
    }  
    if (locked == true) 
    {    
      knockVal = analogRead(piezo); // Knock value is read by analog pin    
      if (numberOfKnocks < 3 && knockVal > 0) {      
        if (checkForKnock(knockVal) == true) { // Check for correct                                              
          // number of knocks        
          numberOfKnocks++;      }      
          Serial.print(3 - numberOfKnocks);      
          Serial.println(" more knocks to go");    }   
          if (numberOfKnocks >= 3) { // If 3 valid knocks are detected,                                
            // the servo moves      
            locked = false;      
            servo9.write(0);      
            delay(20);     
            digitalWrite(greenLed, HIGH);      
            digitalWrite(redLed, LOW);      
            Serial.println("The box is unlocked!");   
            }  
            } 
            }
        
      
  

boolean checkForKnock(int value) { // Checks knock value  
  if (value > quietKnock && value < loudKnock) { // Value needs to be                                                 
    // between these    
    digitalWrite(yellowLed, HIGH);    
    delay(50);    
    digitalWrite(yellowLed, LOW);    
    Serial.print("Valid knock of value ");    
    Serial.println(value);    
    return true;  }  
    else { // If value is false then send this to the IDE serial    
      Serial.print("Bad knock value ");   
      Serial.println(value);    
      return false;  } 
}

Credits

Bables14
14 projects • 2 followers
Contact
cfulford
16 projects • 4 followers
Contact
iupucmet
15 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.