In: Computer Science
For this lab, you are to construct a mock burglar alarm. The system will have three states, armed, disarmed, and alarm, two inputs in the form of push buttons, and two outputs in the form of LEDs.
The system should start in the disarmed state with a green LED lit. Pressing button 1 moves us to the armed state, pressing button 2 (our stand-in for a door contact sensor) does nothing.
In the armed state, the red LED should blink off and on. Pressing button 1 disarms the system and returns to that state. Pressing button 2 transitions the system to the alarm state
In the alarm state, the red LED is solid (not blinking). Pressing button 2 does nothing while pressing button 1 disarms the alarm.
Can someone please create a writing schematic and code in Arduino?
Here I am writing the code in Arduino IDE and I am pasting sketch here also I will explain about that line after this // .
alarm sketch:
int push_button1=7; // declration of pin of push button and LED
pins
int push_button2=8;
int green_LED=9;
int red_LED=10;
int val1=0,val2=0,i=0,j=0; // taking some variable
which will be use in program to make this system
void setup() {
pinMode(push_button1,INPUT); //intializing
pinmode of the LED's and push button's
pinMode(push_button2,INPUT);
pinMode(green_LED,OUTPUT);
pinMode(red_LED,OUTPUT);
disarmed_state();
// according to question system initial start with disarmed state
so it's need to be in setup part
}
void
disarmed_state()
// disarmed function that which we call in main function according
to our use
{
digitalWrite(red_LED,LOW); // if disarmed state is on
so it needs to be red led off and green led on
digitalWrite(green_LED,HIGH);
}
void
armed_state()
// creating armed state function which we call in the main function
according to our use
{
digitalWrite(green_LED,LOW); // armed state so
it's need to be green led off and red led will blink
digitalWrite(red_LED,HIGH); // red led on
delay(500);
// give it to 0.5 sec time to be on
digitalWrite(red_LED,LOW); // then off
delay(500);
// again give it to 0.5 sc to off then it will again on because
loop will run continue
}
void
alarm()
// alarm state function again we will call it according to our
use
{
digitalWrite(green_LED,LOW); // in this state green led
need to off
digitalWrite(red_LED,HIGH); //and red led to on
}
void loop()
{
//main function
val1=digitalRead(push_button1); // read push
button value when it will push then it value become 1 if push will
realise then it's value become 0
val2=digitalRead(push_button2); // similarly for
pushbutton 2
if(val1==HIGH)
//checking for input come or not if come val =1 and program enter
to this loop
{
i=1;
delay(1000);
// giving program to 1sec time to excute properly
while(i) // when i
=1 then program enter to while loop it will run in this loop untill
i become 0
{
armed_state(); //calling armed state
because according to question when push button 1 is press then it
will go to armed state
if(val1==HIGH) // again checking for
new input from push button 1
{
disarmed_state(); // if
come then program will go to disarmed state
i=0;
// and i become 0 and program will exit this loop
}
}
}
if(val2==HIGH && i==0) // now if push button 2 will press
and push button 1 not pressed or system neither in any state
{
j=1; // then program will
enter in this loop
delay(500); // giving 0.5 sec time to program
for work properly
while(j) // enter in while loop when
push button2 will pressed and system not in any state or in
disarmed state
{
alarm(); // alarm state will
activate
if(val1==HIGH) // again checking for
push button 1 input if come
{
delay(500);
disarmed_state(); //
then disarmed state will activate
j=0; // and
program will exit this loop
}
}
}
}