A fun way to make a lock. You set a password initially and the ultrasonic sensor every 3 seconds measure a distance (between the sensor and an object/card) and it compares the password set with the one you enter. If the password is valid, the lock will remain unlocked as far as the distance measured is greater than 5 cm hence, you can lock the box whenever you want.
How it works:
The ultrasonic Sensor makes make a measurement every 3 seconds. That measurement corresponds to a single element of the password. The password is given in the form of an Array("pass"). Also, I created another Array("checkPass") with all of its elements equal to 0. The measurements taken by the ultrasonic Sensor replace the 0s in the "checkPass" Array. Lastly, the 2 Arrays are compared and if the values of the Array "checkpass" are equal and in the same order as the "pass" Array, the Servo rotates and unlocks the Box.
Then to lock again the Box you just need to bring an object/card close to the ultrasonic Sensor (less than 5 cm) and the Servo will move back to its initial position.