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.