I'm very new to Arduino and programming so please go easy on me. I'm trying to create a simple alarm system that turns a red led and a buzzer on as long as the alarm is triggered.
But I also want to be able to disable the alarm of a period of time. So I want to put another switch that when pressed disables the alarm for 15 seconds, it also turns a yellow led on as long as the alarm is disabled.
I intend to make things more complex later but as I mentioned, I'm very new to Arduino and programming.
Thanks
Here is the code without the disabling button
//Simple Security Proj
//Alarm Switches
int lever=5;
int mercury=6;
int button=7;
//Output Switches containing three leds and a buzzer
int green=8;
int yellow=9;
int red=10;
int buzzer=11;
void setup() {
pinMode(lever,INPUT); //lever trigger switch x3
pinMode(mercury,INPUT); // mercury switch
pinMode(button,INPUT); // button for the heck of it
pinMode(green,OUTPUT); //green led stays on as long as the alarm is not set off
pinMode(yellow,OUTPUT); //
pinMode(red,OUTPUT); //red led turns on if the alarm is triggered
pinMode(buzzer,OUTPUT); //buzzer sounds off if the alarm is triggered
}
void loop() {
if (digitalRead(lever)==HIGH||digitalRead(mercury)==HIGH||digitalRead(button)==HIGH){
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
digitalWrite(buzzer,HIGH);
}
else{
digitalWrite(green,HIGH);
digitalWrite(red,LOW);
digitalWrite(buzzer,LOW);
}
}
google "arduino boolean" (my browser is not working correctly so I can't open the link for Arduino- Boolean)
Here's an example of using a boolean:
You need to create a boolean flag for "alarm_Enabled" or "alarm_Disabled" or both.
Then is this code :
if (digitalRead(lever)==HIGH||digitalRead(mercury)==HIGH||digitalRead(button)==HIGH){
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
digitalWrite(buzzer,HIGH);
You need to add a boolean AND (&&) operation with the "alrm_Enabled" flag , (ie "&& alrm_Enabled == true")
I'm very new to Arduino and programming so please go easy on me. I'm trying to create a simple alarm system that turns a red led and a buzzer on as long as the alarm is triggered.
But I also want to be able to disable the alarm of a period of time. So I want to put another switch that when pressed disables the alarm for 15 seconds, it also turns a yellow led on as long as the alarm is disabled.
I intend to make things more complex later but as I mentioned, I'm very new to Arduino and programming.
Thanks
a state change technique is consistent with the Alarm System paradigm:
something like this:
//Simple Security Proj
//Alarm Switches
int pauseButton = 4;// NEW
int lever=5;
int mercury=6;
int button=7;
//Output Switches containing three leds and a buzzer
int green=8;
int yellow=9;
int red=10;
int buzzer=11;
int lastButtonState;
unsigned long disabledStart;
boolean state;
void setup() {
pinMode(lever,INPUT); //lever trigger switch x3
pinMode(mercury,INPUT); // mercury switch
pinMode(button,INPUT); // button for the heck of it
pinMode(pauseButton,INPUT);
pinMode(green,OUTPUT); //green led stays on as long as the alarm is not set off
pinMode(yellow,OUTPUT); //
pinMode(red,OUTPUT); //red led turns on if the alarm is triggered
pinMode(buzzer,OUTPUT); //buzzer sounds off if the alarm is triggered
}
void loop()
{
int buttonState = digitalRead(pauseButton);
if (buttonState == HIGH)
{
if (lastButtonState == LOW)
{
if (state == 0)//*** remove starred lines to just extend the disabled time when button pressed
{//***
state = 1;
disabledStart = millis();
}//***
}
}
lastButtonState = buttonState;
//
if (state == 0)
{
if (millis() - disabledStart <= 15000UL)
{
digitalWrite(green,LOW);
digitalWrite(red,LOW);
digitalWrite(yellow, HIGH);
}
else
{
state = 1;
}
}
else if (state == 1)
{
if (digitalRead(lever)==HIGH||digitalRead(mercury)==HIGH||digitalRead(button)==HIGH)
{
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
digitalWrite(buzzer,HIGH);
}
else
{
digitalWrite(green,HIGH);
digitalWrite(red,LOW);
digitalWrite(buzzer,LOW);
}
}
}
I use the code you provided but somehow it didn't work. Once I upload the sketch the yellow led stays on, triggering ant of the switches won't do anything and pressing the pause button just brings the green button on along side the yellow one.
Still, thanks the code I was able figure out how boolean works and I was able to make out my own code.
Now when I press the pause button (now the disablebutton) the green goes off, the yellow stays on and the triggers are disabled for 5 seconds.
int disablebutton=4;
int lever=5;
int magnet=6;
int button=7;
int green=8;
int yellow=9;
int red=10;
int buzzer=11;
boolean disablestate=false;
boolean buttonstate;
void setup() {
pinMode(lever,INPUT);
pinMode(magnet,INPUT);
pinMode(button,INPUT);
pinMode(green,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(red,OUTPUT);
pinMode(buzzer,OUTPUT);
}
void loop() {
buttonstate=digitalRead(disablebutton);
if(buttonstate==true){
disablestate=true;
digitalWrite(yellow,HIGH);
digitalWrite(green,LOW);
delay(5000);
disablestate=false;
digitalWrite(yellow,LOW);
digitalWrite(green,HIGH);
}
if(disablestate==false){
if(digitalRead(lever)==HIGH||digitalRead(magnet)==HIGH||digitalRead(button)==HIGH){
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
digitalWrite(buzzer,HIGH);
}
else{
digitalWrite(green,HIGH);
digitalWrite(red,LOW);
digitalWrite(buzzer,LOW);
}
}
}
yeah, I forgot to add the turning off of the yellow led.
You will grow to regret your decision to put delays into your coding... and most will recommend that you get rid of it all together, if at all possible (and i this case you can).
try this with the fix...
//Simple Security Proj
//Alarm Switches
int pauseButton = 4;// NEW
int lever=5;
int mercury=6;
int button=7;
//Output Switches containing three leds and a buzzer
int green=8;
int yellow=9;
int red=10;
int buzzer=11;
int lastButtonState;
unsigned long disabledStart;
boolean state;
void setup() {
pinMode(lever,INPUT); //lever trigger switch x3
pinMode(mercury,INPUT); // mercury switch
pinMode(button,INPUT); // button for the heck of it
pinMode(pauseButton,INPUT);
pinMode(green,OUTPUT); //green led stays on as long as the alarm is not set off
pinMode(yellow,OUTPUT); //
pinMode(red,OUTPUT); //red led turns on if the alarm is triggered
pinMode(buzzer,OUTPUT); //buzzer sounds off if the alarm is triggered
}
void loop()
{
int buttonState = digitalRead(pauseButton);
if (buttonState == HIGH)
{
if (lastButtonState == LOW)
{
if (state == 0)//*** remove starred lines to just extend the disabled time when button pressed
{//***
state = 1;
disabledStart = millis();
}//***
}
}
lastButtonState = buttonState;
//
if (state == 0)
{
if (millis() - disabledStart <= 15000UL)
{
digitalWrite(green,LOW);
digitalWrite(red,LOW);
digitalWrite(yellow, HIGH);
}
else
{
state = 1;
digitalWrite(yellow, LOW);
}
}
else if (state == 1)
{
if (digitalRead(lever)==HIGH||digitalRead(mercury)==HIGH||digitalRead(button)==HIGH)
{
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
digitalWrite(buzzer,HIGH);
}
else
{
digitalWrite(green,HIGH);
digitalWrite(red,LOW);
digitalWrite(buzzer,LOW);
}
}
}