Two-Lock Safe: Locking Code

Servo Code:

Two Lock Safe – Overview
#include <avr/io.h> #include <util/delay.h> //Function Prototypes void lock(); void unlock(); void init_PWM(); void setDDR(int b, int c, int d); void lock(){ init_PWM(); //assume lock = -90 degrees = 1 ms pulse OCR1A = 0x946E; _delay_ms(10000); setDDR(0,0,0); } void unlock(){ init_PWM(); //assume unlock = 0 degrees = 1.5 ms pulse OCR1A = 0x9086; _delay_ms(10000); setDDR(0,0,0); } void init_PWM(){ //set data direction on port b, other ports are off setDDR(0xFF, 0, 0); //set waveform generation mode (mode #14 on datasheet: fast pwm) TCCR1A |= 1<<WGM11 | 0<<WGM10; TCCR1B |= 1<<WGM12 | 1<<WGM13; //set output mode (inverted, starts low & switches to high on compare match) TCCR1A |= 1<<COM1A1 | 1<<COM1A0; //set prescaler (clk/1, no prescaler) //001 -> clk/1 //010 -> clk/8 TCCR1B |= 0<<CS12 | 1<<CS11 |0<<CS10; //set period (20,000 counts per second -> 20ms period) ICR1 |= 0x9C3E; } void setDDR(int b, int c, int d){ DDRB = b; DDRC = c; DDRD = d; } int main(void) { while(1){ lock(); unlock(); } }

Code used to program the microcontroller to set the servo to lock and unlock.

Two-Lock Safe Schematics

The transistor is needed to control when the solenoid retracts from lock into unlock. 12 Volt 1.25 A power supply required because the specific solenoid used operates with 12 Volt 1 A. The 12 key keypad uses up seven pins and connected to PortD. DDRD was set to receive input from 12 key keypad and outputs on pin connected to transistor and for servo rotations, PortA and Port B set to output.

Two Lock Safe – Overview

The goal of this project was to construct a simple two lock mechanism using the ATmega328P microcontroller, a servo, a solenoid, and a 12 key keypad. The project wasn’t a complete success. The solenoid does retract into an unlocked position upon correct key presses and the servo does rotate into a lock state and unlock state, but the both systems cannot happen at the same time. The problem is caused by both systems using the same timer, and could be fixed by using both timer 0 and timer 1.