2012-10-12 19:23:48 +02:00
|
|
|
/*
|
|
|
|
Arduino Starter Kit example
|
2017-07-15 00:30:34 +02:00
|
|
|
Project 12 - Knock Lock
|
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
This sketch is written to accompany Project 12 in the Arduino Starter Kit
|
2017-07-15 00:30:34 +02:00
|
|
|
|
|
|
|
Parts required:
|
2017-07-12 22:18:23 +02:00
|
|
|
- 1 megohm resistor
|
|
|
|
- 10 kilohm resistor
|
|
|
|
- three 220 ohm resistors
|
|
|
|
- piezo
|
|
|
|
- servo motor
|
|
|
|
- push button
|
|
|
|
- one red LED
|
|
|
|
- one yellow LED
|
|
|
|
- one green LED
|
|
|
|
- 100 uF capacitor
|
|
|
|
|
|
|
|
created 18 Sep 2012
|
2017-07-15 00:30:34 +02:00
|
|
|
by Scott Fitzgerald
|
|
|
|
Thanks to Federico Vanzati for improvements
|
|
|
|
|
|
|
|
http://www.arduino.cc/starterKit
|
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
This example code is part of the public domain.
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2012-10-12 19:23:48 +02:00
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
// import the library
|
2012-10-12 19:23:48 +02:00
|
|
|
#include <Servo.h>
|
2017-07-12 19:58:31 +02:00
|
|
|
// create an instance of the Servo library
|
2012-10-12 19:23:48 +02:00
|
|
|
Servo myServo;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// variable for the piezo value
|
|
|
|
int knockVal;
|
|
|
|
// variable for the switch value
|
|
|
|
int switchVal;
|
|
|
|
|
|
|
|
// variables for the high and low limits of the knock value
|
|
|
|
const int quietKnock = 10;
|
|
|
|
const int loudKnock = 100;
|
|
|
|
|
|
|
|
// variable to indicate if locked or not
|
2018-05-30 13:30:13 +02:00
|
|
|
bool locked = false;
|
2012-10-12 19:23:48 +02:00
|
|
|
// how many valid knocks you've received
|
|
|
|
int numberOfKnocks = 0;
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
void setup() {
|
2012-10-12 19:23:48 +02:00
|
|
|
// attach the servo to pin 9
|
|
|
|
myServo.attach(9);
|
|
|
|
|
|
|
|
// make the LED pins outputs
|
|
|
|
pinMode(yellowLed, OUTPUT);
|
|
|
|
pinMode(redLed, OUTPUT);
|
|
|
|
pinMode(greenLed, OUTPUT);
|
|
|
|
|
|
|
|
// set the switch pin as an input
|
|
|
|
pinMode(switchPin, INPUT);
|
|
|
|
|
|
|
|
// start serial communication for debugging
|
|
|
|
Serial.begin(9600);
|
|
|
|
|
|
|
|
// turn the green LED on
|
|
|
|
digitalWrite(greenLed, HIGH);
|
|
|
|
|
|
|
|
// move the servo to the unlocked position
|
|
|
|
myServo.write(0);
|
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// print status to the Serial Monitor
|
2012-10-12 19:23:48 +02:00
|
|
|
Serial.println("the box is unlocked!");
|
|
|
|
}
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
void loop() {
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// if the box is unlocked
|
2013-10-21 09:58:40 +02:00
|
|
|
if (locked == false) {
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// read the value of the switch pin
|
|
|
|
switchVal = digitalRead(switchPin);
|
|
|
|
|
|
|
|
// if the button is pressed, lock the box
|
2013-10-21 09:58:40 +02:00
|
|
|
if (switchVal == HIGH) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// set the locked variable to "true"
|
|
|
|
locked = true;
|
|
|
|
|
|
|
|
// change the status LEDs
|
2013-10-21 09:58:40 +02:00
|
|
|
digitalWrite(greenLed, LOW);
|
|
|
|
digitalWrite(redLed, HIGH);
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// move the servo to the locked position
|
|
|
|
myServo.write(90);
|
|
|
|
|
|
|
|
// print out status
|
|
|
|
Serial.println("the box is locked!");
|
|
|
|
|
|
|
|
// wait for the servo to move into position
|
2015-07-06 15:18:33 +02:00
|
|
|
delay(1000);
|
2012-10-12 19:23:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the box is locked
|
2013-10-21 09:58:40 +02:00
|
|
|
if (locked == true) {
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// check the value of the piezo
|
|
|
|
knockVal = analogRead(piezo);
|
|
|
|
|
|
|
|
// if there are not enough valid knocks
|
2013-10-21 09:58:40 +02:00
|
|
|
if (numberOfKnocks < 3 && knockVal > 0) {
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// check to see if the knock is in range
|
2013-10-21 09:58:40 +02:00
|
|
|
if (checkForKnock(knockVal) == true) {
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// increment the number of valid knocks
|
|
|
|
numberOfKnocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print status of knocks
|
|
|
|
Serial.print(3 - numberOfKnocks);
|
|
|
|
Serial.println(" more knocks to go");
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are three knocks
|
2013-10-21 09:58:40 +02:00
|
|
|
if (numberOfKnocks >= 3) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// unlock the box
|
|
|
|
locked = false;
|
|
|
|
|
|
|
|
// move the servo to the unlocked position
|
|
|
|
myServo.write(0);
|
|
|
|
|
|
|
|
// wait for it to move
|
|
|
|
delay(20);
|
|
|
|
|
|
|
|
// change status LEDs
|
2013-10-21 09:58:40 +02:00
|
|
|
digitalWrite(greenLed, HIGH);
|
|
|
|
digitalWrite(redLed, LOW);
|
2012-10-12 19:23:48 +02:00
|
|
|
Serial.println("the box is unlocked!");
|
2015-06-01 10:33:26 +02:00
|
|
|
|
|
|
|
numberOfKnocks = 0;
|
2012-10-12 19:23:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
// this function checks to see if a detected knock is within max and min range
|
2018-05-30 13:30:13 +02:00
|
|
|
bool checkForKnock(int value) {
|
2017-07-14 21:34:00 +02:00
|
|
|
// if the value of the knock is greater than the minimum, and larger
|
|
|
|
// than the maximum
|
2013-10-21 09:58:40 +02:00
|
|
|
if (value > quietKnock && value < loudKnock) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// turn the status LED on
|
|
|
|
digitalWrite(yellowLed, HIGH);
|
|
|
|
delay(50);
|
|
|
|
digitalWrite(yellowLed, LOW);
|
|
|
|
// print out the status
|
|
|
|
Serial.print("Valid knock of value ");
|
|
|
|
Serial.println(value);
|
|
|
|
// return true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// if the knock is not within range
|
|
|
|
else {
|
|
|
|
// print status
|
|
|
|
Serial.print("Bad knock value ");
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(value);
|
2012-10-12 19:23:48 +02:00
|
|
|
// return false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|