Silence Button

I am working on an alarm box. If a red LED goes on I need a buzzer to sound. The issue I am having is I need to silence that alarm with momentary button, but if an another red LED gets lit, the buzzer needs to resound. For some reason I can't think of how to make this work. Will post code shortly.

//pin numbers:
int a1OpenPin = 22;    // alarm 1 pin IN
int a2OpenPin = 24;    // alarm 2 pin IN
int a3OpenPin = 26;    // alarm 3 pin IN
int a4OpenPin = 28;    // alarm 4 pin IN
int a5OpenPin = 30;    // alarm 5 pin IN
int btnPin = 11;      //Silence BTn

int a1ClosedPin = 31;    // alarm 1 pin IN
int a2ClosedPin = 33;    // alarm 2 pin IN
int a3ClosedPin = 35;    // alarm 3 pin IN
int a4ClosedPin = 37;    // alarm 4 pin IN
int a5ClosedPin = 39;    // alarm 5 pin IN

const int alarm1RedPin =  32;    // alarm 1 red led
const int alarm2RedPin =  34;    // alarm 2 red led
const int alarm3RedPin =  36;    // alarm 3 red led
const int alarm4RedPin =  38;    // alarm 4 red led
const int alarm5RedPin =  40;    // alarm 5 red led

const int alarm1GreenPin =  42;    // alarm 1 red led
const int alarm2GreenPin =  44;    // alarm 2 red led
const int alarm3GreenPin =  46;    // alarm 3 red led
const int alarm4GreenPin =  48;    // alarm 4 red led
const int alarm5GreenPin =  50;    // alarm 5 red led

const int audioA = 12; //audio alarm

// variables:
int a1OpenPinState = 0;  //alrm 1     
int a2OpenPinState = 0;  //alrm 2
int a3OpenPinState = 0;  //alrm 3
int a4OpenPinState = 0;  //alrm 4
int a5OpenPinState = 0;  //alrm 5
int a1ClosedPinState = 0;  //alrm 1     
int a2ClosedPinState = 0;  //alrm 2
int a3ClosedPinState = 0;  //alrm 3
int a4ClosedPinState = 0;  //alrm 4
int a5ClosedPinState = 0;  //alrm 5
int btnPinState = 0; //silence

void setup() {
  // initialize the G/R LED pin as an output:
  pinMode(alarm1RedPin, OUTPUT);      
  pinMode(alarm2RedPin, OUTPUT);
  pinMode(alarm3RedPin, OUTPUT);      
  pinMode(alarm4RedPin, OUTPUT);
  pinMode(alarm5RedPin, OUTPUT);
  
  pinMode(alarm1GreenPin, OUTPUT);
  pinMode(alarm2GreenPin, OUTPUT);
  pinMode(alarm3GreenPin, OUTPUT);
  pinMode(alarm4GreenPin, OUTPUT);
  pinMode(alarm5GreenPin, OUTPUT);
  
  pinMode(audioA, OUTPUT);
  

  // initialize alarms as inputs:
  pinMode(a1OpenPin, INPUT);   
  pinMode(a2OpenPin, INPUT);
  pinMode(a3OpenPin, INPUT);   
  pinMode(a4OpenPin, INPUT);
  pinMode(a5OpenPin, INPUT);
  pinMode(a1ClosedPin, INPUT);   
  pinMode(a2ClosedPin, INPUT);
  pinMode(a3ClosedPin, INPUT);   
  pinMode(a4ClosedPin, INPUT);
  pinMode(a5ClosedPin, INPUT);
  pinMode (btnPin, INPUT);
 }

void loop(){
  // read the state of inputs:
  a1OpenPinState = digitalRead(a1OpenPin);
  a2OpenPinState = digitalRead(a2OpenPin);
  a3OpenPinState = digitalRead(a3OpenPin);
  a4OpenPinState = digitalRead(a4OpenPin);
  a5OpenPinState = digitalRead(a5OpenPin);
  a1ClosedPinState = digitalRead(a1ClosedPin);
  a2ClosedPinState = digitalRead(a2ClosedPin);
  a3ClosedPinState = digitalRead(a3ClosedPin);
  a4ClosedPinState = digitalRead(a4ClosedPin);
  a5ClosedPinState = digitalRead(a5ClosedPin);
  //btnPinState = digitalRead(btnPin);


  //alarms
  if (a1ClosedPinState == HIGH and a1OpenPinState == LOW){         
    digitalWrite(alarm1GreenPin, LOW);  
    digitalWrite(alarm1RedPin, HIGH);
   }

    
  if (a1ClosedPinState == LOW and a1OpenPinState == HIGH){         
    digitalWrite(alarm1GreenPin, HIGH);  
    digitalWrite(alarm1RedPin, LOW);
    }
  
  if (a2ClosedPinState == HIGH and a2OpenPinState == LOW){     
    // turn LED on:    
    digitalWrite(alarm2GreenPin, LOW);  
    digitalWrite(alarm2RedPin, HIGH);
    } 
  
  if (a2ClosedPinState == LOW and a2OpenPinState == HIGH){     
    // turn LED on:    
    digitalWrite(alarm2GreenPin, HIGH);  
    digitalWrite(alarm2RedPin, LOW);
    }
    
  if (a3ClosedPinState == HIGH and a3OpenPinState == LOW){     
    // turn LED on:    
    digitalWrite(alarm3GreenPin, LOW);  
    digitalWrite(alarm3RedPin, HIGH);
    }
    
  if (a3ClosedPinState == LOW and a3OpenPinState == HIGH){     
    // turn LED on:    
    digitalWrite(alarm3GreenPin, HIGH);  
    digitalWrite(alarm3RedPin, LOW);
    }
    
  if (a4ClosedPinState == HIGH and a4OpenPinState == LOW){     
    // turn LED on:    
    digitalWrite(alarm4GreenPin, LOW);  
    digitalWrite(alarm4RedPin, HIGH);
    } 
    
  if (a4ClosedPinState == LOW and a4OpenPinState == HIGH){     
    // turn LED on:    
    digitalWrite(alarm4GreenPin, HIGH);  
    digitalWrite(alarm4RedPin, LOW);
    }
    
  if (a5ClosedPinState == HIGH and a5OpenPinState == LOW){     
    // turn LED on:    
    digitalWrite(alarm5GreenPin, LOW);  
    digitalWrite(alarm5RedPin, HIGH);
    }
    
  if (a5ClosedPinState == LOW and a5OpenPinState == HIGH){     
    // turn LED on:    
    digitalWrite(alarm5GreenPin, HIGH);  
    digitalWrite(alarm5RedPin, LOW);
    }

}
  //alarms

Is this comment really useful? How about something that does mean something? There is nothing in your code that explains why you are reading both the open and closed pins for each alarm. Why are you? Typically, one only cares about one state? Is someone trying to break in, or not?

  if (a2ClosedPinState == HIGH and a2OpenPinState == LOW){     
    // turn LED on:    
    digitalWrite(alarm2GreenPin, LOW);  
    digitalWrite(alarm2RedPin, HIGH);
    } 
  
  if (a2ClosedPinState == LOW and a2OpenPinState == HIGH){     
    // turn LED on:    
    digitalWrite(alarm2GreenPin, HIGH);  
    digitalWrite(alarm2RedPin, LOW);
    }

Turn which LED on? How can HIGH and LOW both turn the same LED on?

Once you have more than one of a thing, it's time to learn about arrays.

the comment was for me.

yes, i'm new to these and i need to learn more (like arrays).

I need both states because the input uses normally open and normally closed contacts.

No reason to be a snot and not help in any constructive way.

There is a lot you can do to reduce the complexity of this, and make it easier on yourself.
A schematic would be helpful if you have one, even a simple mspaint version.
Is there a reason why you need to check both the NC and NO side of the alarms? You could just test the conditions on one side, using a pullup resistor if needed.

Arrays would also help. Here's an example:

//pin numbers:
int aOpenPin[5] = {22,24,26,28,30};    // alarm pins IN

int btnPin = 11;      //Silence BTn

int aClosedPin [5] = {31,33,35,37,39};    // alarm pins IN

const int alarmRedPin[5] =  {32,34,36,38,40};    // alarm red led
const int alarmGreenPin[5] =  {42,44,46,48,50};    // alarm green led

const int audioA = 12; //audio alarm

You could do the same thing with your variables as well.
Now when you want to access an alarm you just do something like:
aOpenPin[0]
Remember, arrays start at 0, not 1, so your alarm numbers will be off by 1 (Alarm1 is Alarm[0])

This makes things done on all pins much easier:

  // initialize the G/R LED pin as an output:
int i;
for(i=0;i<5;i++)
{
  pinMode(alarmRedPin[i], OUTPUT);      
  pinMode(alarmGreenPin[i], OUTPUT);
}

Or this:

  // read the state of inputs:
int i;
for(i=0;i<5;i++)
{
  aOpenPinState[i] = digitalRead(aOpenPin[i]);
  aClosedPinState[i] = digitalRead(aClosedPin[i]);
}

I can go into more detail in a bit, and get to your actual question. I'm just heading out the door right now.

No reason to be a snot and not help in any constructive way.

Whose being a snot, now? I have no idea what your program is doing, or why you are reading both the normally open and the normally closed sides, AND expecting that they may not be the inverse of each other. There are no comments in your code to explain what it is doing.

The comments that are there are contradictory or useless. It takes no longer to type correct and useful comments.

The code to read the silence switch is commented out. There is no code to actually use the value that was read.

It seems to me that you should have some functions, with useful names, like soundTheAlarm() and silenceTheNoise(). Call the soundTheAlarm() function when an alarm is tripped. Call the silenceTheNoise() function when the "shut up" switch is pressed.

From the names, it should be pretty obvious what goes in each function, and where the functions should be called (soundTheAlarm() in 5 places; silenceTheNoise() in 1 place).

Of course, if you WERE using arrays and a for loop, then you would only need to call the soundTheAlarm() function in one place.

Hillridge - Thanks for that. I will try that out, it does make more sense.

PaulS- Thanks you for the suggestion, I will try it that way. I'll also work on my comments to appease you sir.

I have the NO and NC because the alarms are pressure switches. When the pressure goes below X the alarm sounds and alarm 1 LED goes on. When the pressure goes above Y the alarm 1 Green LED turns on and the red turns off.

There is a range in between the X and the Y. So it is simply not on or off.

Thanks for the help.

Ok, to answer your original question, here's one way to have each alarm set off the buzzer, even if it has been previously silenced. There may be better ways, but this is a starting point. This is partial psuedocode, so don't just copy and paste it. You'll need to fit it to your pin and variable definitions.

//in your setup function:
bool AlarmAudible = false;

//in your loop function
if( (Alarm conditions are met)&&(Alarm LED is not currently lit) )
{
     AlarmAudible = true;
}

//if the silence button is pressed:
AlarmAudible = false;

if(AlarmAudible)
{
  //sound the alarm
}
else
{
  //turn off the alarm
}

This statement:
if( (Alarm conditions are met)&&(Alarm LED is not currently lit) )

is basically saying that you need a way to see if an alarm has already been triggered, so you don't set the AlarmAudible flag to true more than once for each alarm. That way you can silence one, but a different one can still reactivate the noise.

There is a range in between the X and the Y. So it is simply not on or off.

So, put a pullup resistor (or pulldown) on one of the switch pins to make the output either on or off and never anything else, or enable the Arduino internal pullup resistors on the input pins.

Mind you, as it stands, I wonder what the voltage on the switch pins is when they are neither open or closed. Have you heard of the Grand Old Duke of York who marched his men to the top of the hill then marched them back again ? Google it to see the relevance.