In this project we made a locking mechanism. We start by building the circuit, powering and grinding the board on both sides. We place a switch that will work as what activates the lock, at the bottom of the board and connect a 10k OHM resistor and Digital Pin 2 on the Arduino Board. Then wire in and ground our Piezo speaker, connecting it to Analog Pin 0 on the Arduino. While normally, we would use the Piezo as an audio output device, in this project we actually use it as an input device to detect the vibrations from knocking to unlock the mechanism after it is locked. We use the 1 MegaOHM resistor so the piezo is less sensitive to vibrations. After this we hook up the servo motor to the board, one end to the capacitor, one end to the Arduino Pin #5, and then the other to a ground. We then set up the different LED's each with a 200 OHM resistor and power from Digital Arduino Pins. These LEDs will indicate the status of the mechanism. Red for lock, Green for unlock, and yellow will indicate if the piezo is detecting the knocking to then open it.
When it comes to the code, we start by importing the servo library. We then create constants for all of the inputs and outputs. Included will be the variables that hold the values of the switch and the piezo. Afterwards are the variables for the thresholds of the knocking. Afterwards is the variable that defines if the mechanism is locked. In the setup, we code the servo to Pin #9 and define the rest of the equipment as inputs or outputs. In the loop, we start with the statement that defines when the box is unlocked. If yes, it reads the value from the switch, so if the switch gets pressed, the mechanism locks. From there we code an if statement that reads if the box is locked, to then read the piezo, and store the variable called KnockVal. We make a function called checkForKnock and KnockVal as the parameter. If the fuction remains true, the number of knocks increases by one. It will check to see if the number of knocks wis 3 or more, which will then unlock the mechanism.
Comments