Interlocking Relay Code

@ttommttomm -

How far do the readings from ...

  • sensor1 range from below 20 to above 100
  • sensor2 range below 30 and above 90
  • sensor3 range below 45 and above 70

What will the relays look like in these "Value123" conditions:

          REL1   REL2   REL3
1 above   .      .      .    // sensor 1 above ValueHigh1
1 between .      .      .    // sensor 1 between ValueLow1 and ValueHigh1
1 below   OFF    OFF    OFF  // sensor 1 below ValueLow1 - all relays off

2 above   .      .      .
2 between .      .      .
2 below   .      .      .

3 above   .      .      .
3 between .      .      .
3 below   .      .      .

If I am correct in my understanding of what you want in my reply #19 then something like the following will work:
Note the following suggestion is based on blocking code, something I am usually at pains to avoid. Also I am using a tablet to type, so I can't test anything and there might be errors.

void setup () exactly as you have it in reply #9, but note my comment about using unsigned int instead of float.

Loop:

void loop() {
  // put your main code here, to run repeatedly:

  //Read sensor values
     SensorReading1 = analogRead(sensor1);
     SensorReading2 = analogRead(sensor2);
     SensorReading3 = analogRead(sensor3);


if ( SensorReading1 > ValueHigh) { 
    digitalWrite(R1, HIGH); //on
    while (SensorReading1 >ValueLow) {
         // This is the blocking part, having started pump 1
         // The pump runs until sensor 1 reports the temperature has dropped sufficiently 
         // The other 2 sensors are not read, there is no point if you don't want their pumps to run
    }  // End of while loop
  digitalWrite(R1,LOW); //off 
  }


if ( SensorReading2 > ValueHigh) { 
    digitalWrite(R2, HIGH); //on
    while (SensorReading2 >ValueLow) {
         // This is the blocking part, having started pump 2
         // The pump runs until sensor 2 reports the temperature has dropped sufficiently 
         // The other 2 sensors are not read, there is no point if you don't want their pumps to run
    }  // End of while loop
  digitalWrite(R2,LOW); //off 
  }


if ( SensorReading3 > ValueHigh) { 
    digitalWrite(R3, HIGH); //on
    while (SensorReading3 >ValueLow) {
         // This is the blocking part, having started pump 3
         // The pump runs until sensor 1reports the temperature has dropped sufficiently 
         // The other 2 sensors are not read, there is no point if you don't want their pumps to run
    }  // End of while loop
  digitalWrite(R3,LOW); //off 
  }

}

You don't need to pinMode() analog input pins, the analogRead() function does that.

1 Like

Thank you @bitherder_57 I have tried that too and gave me the same result.

I noticed that you have not made A0, A1, and A2 inputs. I know they default to inputs on reset but it is good programming practice to define them explicitly.

Thank you for the tip much appreciated :pray:

Thank you @xfpd this will definitely save me some relays and perhaps even boards :sweat_smile:

This will be helpful for future projects as well!

Thank you for the suggestion @gilshultz however the relays im using are SPST as they are the generic relays boards for the arduino from amazon.

@xfpd I altered my code now to only use a single ValueHigh and ValueLow to simplify things. All sensors are on a single roof so they will actually all experience the same heat, hence it makes more sense to use a single ValueHigh and ValueLow for all. But thanks for trying to make the previous code work :pray:

Thank you @PerryBebbington I will correct the float to int as you recommended and try this code. I also wanted to avoid using a while loop as I like to constantly run through the entire program rather than potentially getting stuck in an infinite loop, but its a good start and if its the correct solution so be it. Thanks again :pray:

I agree with not using while loops as you say. I am on holiday using a tablet so I don't want to try writing something better without having access to my computer and the IDE. I can certainly write something better without the while loop, but it will have to wait, sorry. Try that, let me know if it does what you want. A proper solution would need a state machine.

I only pointed it out as a good programming practice. I knew it would not change anything.

However my previous post should solve your problem:

The digital read of an output will return the actual pin state.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.