This simple arduino alarm system works by the press of a touch sensor, whereafter three LED's will turn on in order and then a buzzer will make a noise. Throught his instructables I will show you step by step how this was set up and programmed in arduino.
Step 1: Step 1: Building
To create this alarm system you will need a breadboard, a MEGA 2560, a small buzzer, touch sensor, 3 LED's (2 green and 1 red) and lastly a button.
It should be wired together as seen on the picture, make sure to double check the connections.
Step 2: Step 2: Programming - Before Setup
To start programming this alarm system you will need the Arduino software which can be found on the official arduino website. When that is done, create a new project.
Before the setup we define a few variables like millis, intergers and the library needed for the notes in the buzzer
Step 3: Step 3: Programming - Void Setup
Now the setup comes, where both inputs and outputs are defined. These inputs and outputs are for buttons, LED’s and the touch sensor.
Step 4: Step 4: Programming - Void Loop
Now as we are in the void loop we need to make sure the program checks if the touch sensor is activated. This is done with an If statement that says if it is HIGH (activated) then it sets the variable “touchToggle” to 1, and then it also sets three millis variables with 2000 millis difference. These millis are used to make the delay between the LED’s without using the built in delay function that Arduino has because that function will basically stop the whole program for the duration of the delay.
Step 5: Step 5: Programming - Void Loop Pt2
When the touch sensor is activated we can decide what happens using a switch case function. This function checks the variable “touchToggle” and what it is currently set to. For example if it is 1 (case 1) then it will check the millis variables and turn on LED’s when they are equal of less than the previous millis value (essentially just turning on an LED every 2 second). After the third LED is activated (the red one) it will also turn on the buzzer which emits a continuous buzzing sound until interrupted.
If the variable is 0 (case 0) then it will turn off all LED’s and also turn off the buzzer if it was activated.
Afterwards there is a break which stops the whole switch case.