Code for relay

Hi. I am fairly new to Arduino. I have a circuit done were I am reading two DS18B20 sensors an displaying them to an LCD. I have recently added a relay to turn it on/off @ a certain temperature and it's working fine. But i want to add to this. My problem is i have a lot of scenarios. My question is how would I write a code for the following scenario.

If Temp1 Is greater than 15.5
Relay should be always off

If Temp1 Is less than 15.5 and greater than 14.5 and Temp 2 is greater than 44
Relay should be off

If Temp1 Is less than 15.5 and greater than 14.5 and Temp 2 is less than 40
Relay should be on

If Temp1 Is less than 14.5 and grater than 13.5 and Temp 2 is greater than 48
Relay should be off

If Temp1 Is less than 14.5 and grater than 13.5 and Temp 2 is less than 44
Relay should be on

If Temp1 Is less than 13.5 and grater than 12.5 and Temp 2 is greater than 52
Relay should be off

If Temp1 Is less than 13.5 and grater than 12.5 and Temp 2 is less than 48
Relay should be on

If Temp1 Is less than 12.5 and grater than 11.5 and Temp 2 is greater than 56
Relay should be off

If Temp1 Is less than 12.5 and grater than 11.5 and Temp 2 is less than 52
Relay should be on

If Temp1 Is less than 11.5 and grater than 10.5 and Temp 2 is greater than 60
Relay should be off

If Temp1 Is less than 11.5 and grater than 10.5 and Temp 2 is less than 56
Relay should be on

If Temp1 Is less than 10.5 and grater than 9.5 and Temp 2 is greater than 64
Relay should be off

If Temp1 Is less than 10.5 and grater than 9.5 and Temp 2 is less than 60
Relay should be on

If Temp1 Is less than 9.5 and grater than 8.5 and Temp 2 is greater than 68
Relay should be off

If Temp1 Is less than 9.5 and grater than 9.5 and Temp 2 is less than 64
Relay should be on

If Temp1 Is less than 8.5 and grater than 7.5 and Temp 2 is greater than 72
Relay should be off

If Temp1 Is less than 8.5 and grater than 7.5 and Temp 2 is less than 68
Relay should be on

If Temp1 Is less than 7.5 and grater than 6.5 and Temp 2 is greater than 76
Relay should be off

If Temp1 Is less than 7.5 and grater than 6.5 and Temp 2 is less than 72
Relay should be on

If Temp1 Is less than 6.5 and grater than 5.5 and Temp 2 is greater than 80
Relay should be off

If Temp1 Is less than 6.5 and grater than 5.5 and Temp 2 is less than 76
Relay should be on

If Temp1 Is less than 5.5 and grater than 4.5 and Temp 2 is greater than 84
Relay should be off

If Temp1 Is less than 5.5 and grater than 4.5 and Temp 2 is less than 80
Relay should be on

If Temp1 Is less than 4.5 and grater than 3.5 and Temp 2 is greater than 88
Relay should be off

If Temp1 Is less than 4.5 and grater than 3.5 and Temp 2 is less than 84
Relay should be on

If Temp1 Is less than 3.5 and grater than 2.5 and Temp 2 is greater than 92
Relay should be off

If Temp1 Is less than 3.5 and grater than 2.5 and Temp 2 is less than 88
Relay should be on

If Temp1 Is less than 2.5 and grater than 1.5 and Temp 2 is greater than 96
Relay should be off

If Temp1 Is less than 2.5 and grater than 1.5 and Temp 2 is less than 92
Relay should be on

If Temp1 Is less than 1.5 and grater than 0.5 and Temp 2 is greater than 100
Relay should be off

If Temp1 Is less than 1.5 and grater than 0.5 and Temp 2 is less than 96
Relay should be on

If Temp1 Is less than 0.5
Relay should be always on

Your English description translates almost exactly to C!

For example:

If Temp1 Is less than 11.5 and grater than 10.5 and Temp 2 is greater than 60
Relay should be off

becomes

if (temp1 < 11.5 && temp1 > 10.5 && temp2 > 60)
  digitalWrite(RELAY, LOW);

The simple way to write it would be simply to turn each of the statements in your requirements above into programming statements, each with a single if/test/action. Next best if/test/action/els/test/action and so on

You could simplify things by starting with your last requirement. testing for that first would prevent most of the code needing to run if the test returned true.

You can also combine several tests into one such as

If Temp1 Is less than 15.5 and greater than 14.5 and Temp 2 is greater than 44
Relay should be off

II Temp1 Is less than 15.5 and greater than 14.5 and Temp 2 is less than 40
Relay should be on

Could become

if (Temp1 Is less than 15.5 and greater than 14.5)
  if (Temp 2 is greater than 44)
    turn relay off
  end if
else if (Temp 2 is less than 40)
  turn relay on
end if

You could also set a variable named say relayState as a result of the tests and use it at the end of loop() to control the relay rather than littering the code with digitalWrite()s

If I understand correctly , another possibility would be to construct a two-dimensional array (table) containing all the setpoint values, like so:

element zero: lowValT1, hiValT1, lowValT2, hiValT2
element one: lowValT1, hiValT1, lowValT2, hiValT2
etc.

The values < .5 and > 15.5 are special cases I would handle separately. That is, if either of the limit values is true set the output as desired and ignore the following loop. If *neither * of the exceptions is true proceed to the loop tests.

Use a [color=blue]for()[/color] loop to step through the array and test the T1 values. When you get a match, use that index to test the corresponding T2 values and set the output accordingly. Include a [color=blue]break;[/color] to avoid checking any remaining values.