## Five Guys – Security System – Schematic and Discussion

The first phase of testing for the project consisted only of the A3BU, the LED and the piezo-buzzer, and the IR distance sensor. There was no password interface or mechanism for disarming the alarm, except for removing the power to the A3BU. This first iteration of the project can be seen in Figure 1 below.

Figure 1

The infrared distance sensor works using two beams of infrared light with effective measurement ranges of 4 cm to 1 m. If either of the beams is interrupted by a solid object, the distance sensor will transmit a voltage signal back to the A3BU – 3.3 V for 4 cm distance and 0.4 V for the maximum measureable distance. Once this voltage signal is received by the A3BU, it uses an Analog to Digital Conversion (ADC) input to translate the signal into an 8-bit pattern (0 to 127 decimal) that can be translated using a linear regression into a physical distance interpretation.

Based on conditional logic in the programming (see “Source Code”), the LED and piezo-buzzer react differently at different bit patterns. For example, once the bit pattern reaches 70, the LED begins to flash at 50% duty cycle (1 kHz) as a warning that the system is armed. The LED and piezo-buzzer were powered from the A3BU and received a Pulse Width Modulated (PWM) signal at 1 kHz and varying duty cycles, based on the bit pattern received post-ADC from the IR distance sensor.

The second and final iteration of the prototype included password prompts from the A3BU, entered using SW1 and SW2 buttons native to the microcontroller. It also included an emergency switch to remove power from the piezo-buzzer in the instance of a false trip during our demo. This final product is depicted in Figure 2 below.

Figure 2

Once the bit pattern reached 100, the user would be prompted for a password on the LCD screen. If the password given was incorrect, the buzzer would sound in a “breathing” duty cycle pattern, similar to that used in Lab 4. If the correct password was given, then the logic was interrupted and the system went back to its pre-alert state. If the alarm was tripped, the user could press SW1 to deactivate the system, in the instance of a false trip. The emergency switch was also an option, but was only used to interrupt the piezo-buzzer. An example of the password prompt is shown in Figure 3.

Figure 3

The basic schematic form of the circuits are shown in the “Schematics (Hardware)” section of this report. The schematic was drawn using Microsoft Visio.

## Five Guys – Security System – Code

```
#include <asf.h>

#include "keyboard.h"

#include "touch_api.h"

#include <stdio.h>

#include <conf_example.h>

#include <gfx_mono.h>

&nbsp;

//define our own codes for easier use with buttons

#define BOTTOM_RIGHT  40

#define TOP_RIGHT    38

#define TOP_LEFT  8

#define BOTTOM_LEFT 13

&nbsp;

//global

struct pwm_config mypwm[2]; //For your PWM configuration –CKH

struct keyboard_event input; // probes for button push

&nbsp;

//function declarations

bool CheckKey();

bool KeyToCheck(<em>uint8_t</em>);

&nbsp;

{

#ifdef CONF_BOARD_OLED_UG_2832HSWEG04

gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);

#endif

<em>int32_t</em> value;

char out_str[OUTPUT_STR_SIZE];

char *outputString = <em>NULL</em>;

bool valid;

bool goingUp = true;

int alarmSound = 10;

&nbsp;

value = (<em>int8_t</em>)(result);

&nbsp;

if(value > 70){//"low level" alert - LED flashes

pwm_start(&mypwm[0], 50);

}

if(value > 100){//"high level alert" - LED lit and buzzer armed

//alarm is going off and we need to check the password

gfx_mono_init();

pwm_start(&mypwm[0], 100); // LED fully lit

valid = CheckKey();

if(!valid){

gfx_mono_init();

gfx_mono_draw_string("Calling Police",  5, 5, &sysfont);

while(true){

keyboard_get_key_state(&input);

if (input.type == KEYBOARD_RELEASE)

{

if(input.keycode == TOP_RIGHT){//end logic loop if password is correct

break;

}

}

delay_ms(250);

pwm_start(&mypwm[1], alarmSound);

if(alarmSound == 50){//restart "breathing" alarm loop

goingUp = false;

}

if(alarmSound == 10){ // "breathing" alarm loop

goingUp = true;

}

if(goingUp){

alarmSound += 10;

}

else{

alarmSound -= 10;

}

}

gfx_mono_init();

gfx_mono_draw_string("CORRECT!", 5, 5, &sysfont);

}

}

else{

pwm_start(&mypwm[0], 0); // restart LED if password is correct

pwm_start(&mypwm[1], 0);// restart buzzer if password is correct

}

//

//snprintf(out_str, OUTPUT_STR_SIZE, "%4d", value);

//gfx_mono_draw_string(out_str, 0, 0, &sysfont);

// Start next conversion.

}

&nbsp;

int main(void)

{

&nbsp;

board_init();

sysclk_init();

sleepmgr_init();

irq_initialize_vectors();

cpu_irq_enable();

gfx_mono_init();

&nbsp;

// Enable backlight if display type is not OLED

#ifndef CONF_BOARD_OLED_UG_2832HSWEG04

ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN);

#endif

&nbsp;

&nbsp;

&nbsp;

/* Set up a PWM channel with 500 Hz frequency. Here we will output 4 different pulse trains on 4 pins of the A3BU Xplained.*/

pwm_init(&mypwm[0], PWM_TCC0, PWM_CH_A, 1000);//this is SDA on J1 on the A3BU Xplained - LED

pwm_init(&mypwm[1], PWM_TCC0, PWM_CH_B, 1000);// this is SCL on J1 - buzzer

// both at 1 kHz PWM

pwm_start(&mypwm[0], 100);

pwm_start(&mypwm[1], 100);

// Initialize configuration structures.

&nbsp;

&nbsp;

&nbsp;

* - single-ended measurement from temperature sensor

* - interrupt flag set on completed conversion

* - interrupts disabled

*/

1);

&nbsp;

&nbsp;

// Enable the ADC and start the first conversion.

&nbsp;

do {

// Sleep until ADC interrupt triggers.

sleepmgr_enter_sleep();

} while (1);

}

&nbsp;

{

bool isValid = false;

&nbsp;

isValid = KeyToCheck(TOP_RIGHT);

if(!isValid) return false;

isValid = KeyToCheck(BOTTOM_RIGHT);

if(!isValid) return false;

&nbsp;

return true;

}

&nbsp;

{

// any key will exit the loop

while (true)

{

keyboard_get_key_state(&input);

if (input.type == KEYBOARD_RELEASE)

{

if(input.keycode == nextCorrectKey){

return true;

}

else{

return false;

}

}

}

}

```

## Five Guys – Security System – Abstract

Our final project is the culmination of what we’ve been working on all semester. We set out to design a prototype “home security system” that would use an infrared (IR) distance sensor to detect the presence of an intruder, warn the intruder, prompt for a password, then set off an alarm if the proper password wasn’t given.

The IR sensor was set up in a logical loop with the LED and piezo-buzzer “alarm”, with the Atmel XmegaA3BU microcontroller being used as the power source and the “bridge” between the two control loops. This process will be discussed in greater detail in the “Body” section of this report.

Our equipment consisted of:

• An Atmel XmegaA3BU Xplained microcontroller
• An infrared (IR) distance sensor manufactured by SHARP
• A green LED
• A piezo-buzzer