Multiple conditions for an IF / THEN statement

Hi,

I've been searching the web for an answer and struggling.

I have 8 variables for use with an alarm function so that if the temperature falls below a set value, or exceeds a set value it sounds an alarm. This works

 if ((temperature1 >= AlarmHIGH1) || (temperature1 <= AlarmLOW1))
{
  digitalWrite(alarm, HIGH);
}

However as mentioned I have 8 variables, four low values and four high values, all which can have differing values. So rather then repeat the above four times, changing AlarmHIGH1 for AlarmHIGH2 like this

 if ((temperature1 >= AlarmHIGH1) || (temperature1 <= AlarmLOW1) ||  ((temperature2 >= AlarmHIGH2) || (temperature2 <= AlarmLOW2) || ((temperature3 >= AlarmHIGH3) || (temperature3 <= AlarmLOW3) || ((temperature4 >= AlarmHIGH4) || (temperature4 <= AlarmLOW4))

Is there a "better" way to do this ?

TIA

Malcolm

Just for greater clarity for myself I think I would do it like this

alarmTriggered = false;
if ((temperature1 >= AlarmHIGH1) || (temperature1 <= AlarmLOW1)) {
  alarmTriggered = true;
} 
if ((temperature2 >= AlarmHIGH2) || (temperature1 <= AlarmLOW2)) {
  alarmTriggered = true;
} 
//etc

if (alarmTriggered == true) {
   // do something
}

Where you find yourself numbering variables like alarm1, alarm2 etc it is usually a good idea to create arrays and then work through the arrays with a FOR loop. Something like this

for (byte n = 0; n < numAlarms; n++) {
  if ((temperature[n] >= AlarmHIGH[n]) || (temperature[n] <= AlarmLOW[n])) {
     alarmTriggered = true;
  } 
}

...R

You could use something like this:

#define ALARM(v, a, b) ( (v >= a) || (v <= b) )

bool alarm = ALARM(temperature1, AlarmHIGH1, AlarmLOW1) ||
             ALARM(temperature2, AlarmHIGH2, AlarmLOW2) ||
             ALARM(temperature3, AlarmHIGH3, AlarmLOW3) ||
             ALARM(temperature4, AlarmHIGH4, AlarmLOW4);

Otherwise you could declare "temperature", "AlarmHIGH" and "AlarmLOW" as arrays and loop through the values and test for an alarm.

Thanks for the quick replies guys,

Robin,

Having used BASIC before, the for/next loop came to mind, but the code kept producing errors because I didn't have the syntax right - I see you use square brackets, and I was using normal curved ones !

I'll give that a go now I have a good example...

If you use the "for" loop approach, make sure that you exit the loop as soon as a trigger is found, elsehow you would waste compute power on nothing:

for (byte n = 0; (n < numAlarms) && (!alarmTriggered); n++)
  alarmTriggered = (temperature[n] >= AlarmHIGH[n]) || (temperature[n] <= AlarmLOW[n]);

Robin2:
Where you find yourself numbering variables like alarm1, alarm2 etc it is usually a good idea to create arrays and then work through the arrays with a FOR loop. Something like this

for (byte n = 0; n < numAlarms; n++) {

if ((temperature[n] >= AlarmHIGH[n]) || (temperature[n] <= AlarmLOW[n])) {
    alarmTriggered = true;
 }
}






...R

I assume I change the numAlarms for the values I wish to place in the brackets (1, 2, 3 and 4) so I have

for (byte n = 0; n < 4; n++) {
  if ((temperature[n] >= AlarmHIGH[n]) || (temperature[n] <= AlarmLOW[n])) {
       digitalWrite(alarm, HIGH);
  }

However when I try this I get " 'temperature' was not declared in this token" error

Any ideas

(sorry if its a numpty question, but whilst I can figure out some of the errors by sticking brackets or semi-colons - this type gets me stuck)

Malcolm

Danois, just noticed your post - thanks for the tip

It is impossible to help with compiler errors if you do not supply the full code. You would want to declare the arrays as:

#define NUM_TEMPS 4
const int AlarmLOW[NUM_TEMPS] = {LOW1, LOW2, LOW3, LOW4};
const int AlarmHIGH[NUM_TEMPS] = {HIGH1, HIGH2, HIGH3, HIGH4};
int temps[NUM_TEMPS];

...

for (byte i = 0; (i < NUM_TEMPS) && (!triggered); i++) ...

If your temperatures in AlarmLOW and HIGH are in the range -128..127 you could replace data type "int" with "char" (or 0..255 = byte) and save 12 bytes of memory.

OK I see that I need to set up the array first,

int temperatures[6] = {30,30,30,30};

However will that mean I need to change the rest of the code that have stipulated the variable, such as

[code]
if (temperature2 == -127.00)

to something like

if (temperature[2] == -127.00)

[/code]

Danois90:
If you use the "for" loop approach, make sure that you exit the loop as soon as a trigger is found, elsehow you would waste compute power on nothing:

That would not bother me. Keep things simple unless simple fails to work.

...R