| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 12 - Knock Lock |
| 4 |
|
| 5 |
This sketch is written to accompany Project 12 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- 1 megohm resistor |
| 9 |
- 10 kilohm resistor |
| 10 |
- three 220 ohm resistors |
| 11 |
- piezo |
| 12 |
- servo motor |
| 13 |
- push button |
| 14 |
- one red LED |
| 15 |
- one yellow LED |
| 16 |
- one green LED |
| 17 |
- 100 uF capacitor |
| 18 |
|
| 19 |
created 18 Sep 2012 |
| 20 |
by Scott Fitzgerald |
| 21 |
Thanks to Federico Vanzati for improvements |
| 22 |
|
| 23 |
https://store.arduino.cc/genuino-starter-kit |
| 24 |
|
| 25 |
This example code is part of the public domain. |
| 26 |
*/ |
| 27 |
|
| 28 |
// import the library |
| 29 |
#include <Servo.h> |
| 30 |
// create an instance of the Servo library |
| 31 |
Servo myServo; |
| 32 |
|
| 33 |
const int piezo = A0; // pin the piezo is attached to |
| 34 |
const int switchPin = 2; // pin the switch is attached to |
| 35 |
const int yellowLed = 3; // pin the yellow LED is attached to |
| 36 |
const int greenLed = 4; // pin the green LED is attached to |
| 37 |
const int redLed = 5; // pin the red LED is attached to |
| 38 |
|
| 39 |
// variable for the piezo value |
| 40 |
int knockVal; |
| 41 |
// variable for the switch value |
| 42 |
int switchVal; |
| 43 |
|
| 44 |
// variables for the high and low limits of the knock value |
| 45 |
const int quietKnock = 10; |
| 46 |
const int loudKnock = 100; |
| 47 |
|
| 48 |
// variable to indicate if locked or not |
| 49 |
bool locked = false; |
| 50 |
// how many valid knocks you've received |
| 51 |
int numberOfKnocks = 0; |
| 52 |
|
| 53 |
void setup() { |
| 54 |
// attach the servo to pin 9 |
| 55 |
myServo.attach(9); |
| 56 |
|
| 57 |
// make the LED pins outputs |
| 58 |
pinMode(yellowLed, OUTPUT); |
| 59 |
pinMode(redLed, OUTPUT); |
| 60 |
pinMode(greenLed, OUTPUT); |
| 61 |
|
| 62 |
// set the switch pin as an input |
| 63 |
pinMode(switchPin, INPUT); |
| 64 |
|
| 65 |
// start serial communication for debugging |
| 66 |
Serial.begin(9600); |
| 67 |
|
| 68 |
// turn the green LED on |
| 69 |
digitalWrite(greenLed, HIGH); |
| 70 |
|
| 71 |
// move the servo to the unlocked position |
| 72 |
myServo.write(0); |
| 73 |
|
| 74 |
// print status to the Serial Monitor |
| 75 |
Serial.println("the box is unlocked!"); |
| 76 |
} |
| 77 |
|
| 78 |
void loop() { |
| 79 |
|
| 80 |
// if the box is unlocked |
| 81 |
if (locked == false) { |
| 82 |
|
| 83 |
// read the value of the switch pin |
| 84 |
switchVal = digitalRead(switchPin); |
| 85 |
|
| 86 |
// if the button is pressed, lock the box |
| 87 |
if (switchVal == HIGH) { |
| 88 |
// set the locked variable to "true" |
| 89 |
locked = true; |
| 90 |
|
| 91 |
// change the status LEDs |
| 92 |
digitalWrite(greenLed, LOW); |
| 93 |
digitalWrite(redLed, HIGH); |
| 94 |
|
| 95 |
// move the servo to the locked position |
| 96 |
myServo.write(90); |
| 97 |
|
| 98 |
// print out status |
| 99 |
Serial.println("the box is locked!"); |
| 100 |
|
| 101 |
// wait for the servo to move into position |
| 102 |
delay(1000); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// if the box is locked |
| 107 |
if (locked == true) { |
| 108 |
|
| 109 |
// check the value of the piezo |
| 110 |
knockVal = analogRead(piezo); |
| 111 |
|
| 112 |
// if there are not enough valid knocks |
| 113 |
if (numberOfKnocks < 3 && knockVal > 0) { |
| 114 |
|
| 115 |
// check to see if the knock is in range |
| 116 |
if (checkForKnock(knockVal) == true) { |
| 117 |
|
| 118 |
// increment the number of valid knocks |
| 119 |
numberOfKnocks++; |
| 120 |
} |
| 121 |
|
| 122 |
// print status of knocks |
| 123 |
Serial.print(3 - numberOfKnocks); |
| 124 |
Serial.println(" more knocks to go"); |
| 125 |
} |
| 126 |
|
| 127 |
// if there are three knocks |
| 128 |
if (numberOfKnocks >= 3) { |
| 129 |
// unlock the box |
| 130 |
locked = false; |
| 131 |
|
| 132 |
// move the servo to the unlocked position |
| 133 |
myServo.write(0); |
| 134 |
|
| 135 |
// wait for it to move |
| 136 |
delay(20); |
| 137 |
|
| 138 |
// change status LEDs |
| 139 |
digitalWrite(greenLed, HIGH); |
| 140 |
digitalWrite(redLed, LOW); |
| 141 |
Serial.println("the box is unlocked!"); |
| 142 |
|
| 143 |
numberOfKnocks = 0; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// this function checks to see if a detected knock is within max and min range |
| 149 |
bool checkForKnock(int value) { |
| 150 |
// if the value of the knock is greater than the minimum, and larger |
| 151 |
// than the maximum |
| 152 |
if (value > quietKnock && value < loudKnock) { |
| 153 |
// turn the status LED on |
| 154 |
digitalWrite(yellowLed, HIGH); |
| 155 |
delay(50); |
| 156 |
digitalWrite(yellowLed, LOW); |
| 157 |
// print out the status |
| 158 |
Serial.print("Valid knock of value "); |
| 159 |
Serial.println(value); |
| 160 |
// return true |
| 161 |
return true; |
| 162 |
} |
| 163 |
// if the knock is not within range |
| 164 |
else { |
| 165 |
// print status |
| 166 |
Serial.print("Bad knock value "); |
| 167 |
Serial.println(value); |
| 168 |
// return false |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |