A Relay Board activated by a few Sensors

Hi All,

I am building a special controller and the system has 6 SENSORS for temperature.

I have a question about the programming aspect about turning my relays on and off…
I have not run on my arduino as yet, as I am putting together the program first.

I would understand I need something added to protect the other sensors from switching the relay if one sensor is already switching a relay.

In the code below, I am controlling a relay based off the resistance of a Thermistor

//RELAY Settings for TEMP1 (CPU)
if (temp > 6500){
   digitalWrite(29,LOW);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}
else if (temp > 8000){
   digitalWrite(29,HIGH);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}
else if (temp > 9000) {
   digitalWrite(29,HIGH);
   digitalWrite(30,HIGH);
   digitalWrite(31,LOW);} 
else if (temp < 10000) {
   digitalWrite(29,HIGH);
   digitalWrite(29,HIGH);
   digitalWrite(31,HIGH);}

Now If I add a 2nd Sensor,
I am worried about Sensor 1 reversing the action of Sensor 2.
or
Sensor 2 reversing Sensor 1

//RELAY Settings for TEMP1 (CPU)
if (temp > 6500){
   digitalWrite(29,LOW);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}
else if (temp > 8000){
   digitalWrite(29,HIGH);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}
else if (temp > 9000) {
   digitalWrite(29,HIGH);
   digitalWrite(30,HIGH);
   digitalWrite(31,LOW);} 
else if (temp < 10000) {
   digitalWrite(29,HIGH);
   digitalWrite(29,HIGH);
   digitalWrite(31,HIGH);}

//RELAY Settings for TEMP2 (CPU)
if (temp2 > 6500){
   digitalWrite(29,LOW);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}
else if (temp2 > 8000){
   digitalWrite(29,HIGH);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}
else if (temp2 > 9000) {
   digitalWrite(29,HIGH);
   digitalWrite(30,HIGH);
   digitalWrite(31,LOW);} 
else if (temp2 < 10000) {
   digitalWrite(29,HIGH);
   digitalWrite(29,HIGH);
   digitalWrite(31,HIGH);}

So temp and temp2 are controlling the same relay board and if temp was set to
if (temp > 6500){
digitalWrite(29,LOW);
digitalWrite(30,LOW);
digitalWrite(31,LOW);}

and temp 2 has
else if (temp2 < 10000) {
digitalWrite(29,HIGH);
digitalWrite(29,HIGH);
digitalWrite(31,HIGH);}

I want to keep the LOW value on the relays which are coming from temp
LOW is the more important value than HIGH

Now here is a hurdle, I need this to be active in no order… , such as Sensor 1-6… if any of them have a LOW value I need that to stick and enforce the LOW value until the sensor cools down.

Can I possibly make it so that all my thermistors temp,temp2,temp3,temp4,temp5,temp6, output as 1 value… the value that I would want would be the lowest of all

this would make my program easier to code instead of making a separate relay code for each temp sensor

I hope this makes sense… Let me know if you need me to elaborate more

Thankyou.
Shaun

Can I possibly make it so that all my thermistors temp,temp2,temp3,temp4,temp5,temp6, output as 1 value.. the value that I would want would be the lowest of all

Test all of the values before taking any action and act on the lowest one found. If you use an array of inputs then it is easy to iterate through them and find the lowest value.

Thanks for using the code tags correctly. That shows you've read some of the forum and understand how to ask your question properly.

Normally, I would ask "What SENSORS?" but maybe that isn't important right now. If you say that you're reading the sensors correctly then I'll believe you.

So, first to correct your logic. Instead of thinking about sensors changing relays, what about looking at the relays first? Relay 1 will have a set of rules, such as "Relay 1 must be off when either temp1 is below 6500 or temp2 is below 1000. Relay 1 must be on when temp1 is above 1000 or temp2 is above 1200 or temp3 is above 6500." Note that these rules give hysteresis so that it doesn't flicker on and off when the temperature measurement is randomly going up or down by 0.1.

Now you can write all of this into a complex boolean expression that is wider than your monitor. Or you can write it as a series of if() else statements that take up several lines. The second option is easier to edit later.

Second, to correct your programming style. This is going to seem like a waste of time now but it will save you literally days of work later when a simple mistake is concealed by your unreadable code.

Almost any time you have numbers in your variable names, you are doing something wrong. Your comment on the very first line already shows this. The name of the variable should remind you want it contains, not the comments. If temp1 contains the CPU temperature then call it tempCPU or something like that.

Now if you really do have 2 (or more) identical sensors all sensing the CPU and it doesn't matter if their wires get mixed up, then you could use numbers in your variable names. The only other exception might be for something that's configurable and you intend to put sensors in different places over the life of the device, then you could use numbered variables without cringing.

For large numbers of identical things - 6 IS a large number - then use an array.

The other thing that stands out is the "magic numbers." What is connected to pins 29, 30 and 31? Why not use their names? If one of then controls the CPU fan, then call it pinCPUFan or something that makes sense to you. The numbers like 6500 might also deserve names, if you use them a lot or they are likely to change.

Hi,

Could I have some assistance with array.
So I have looked up arrays, i believe I need to do something like this.

Would the array below’s min and max give me what I need?

// Find maximum and minimum element in an array.

int main ()
{
  int i;/*loop counter*/
  int tempall[6] = { temp, temp2, temp3, temp4, temp5, temp6 };/*array of ints*/
/*set values for comparison starting from the beginning of the array*/
  int max = tempall[0];
  int min = tempall[0];

/*step through the array*/
  for (i = 0; i < 6; i++)
 {
/*if anything is greater than the value of max, which  started as the int pointed to with a[0]*/
  if (tempall[i] > max)
 {
/*set the value found as the new max*/
  max = tempall[i];
 }
/*if anything is less than the value of min, which started as the int pointed to by a[0]*/
  else if (tempall[i] < min)
 {
/*set the value found as the new min*/
  min = tempall[i];
 }
 }
}


if (min > 6500){
   digitalWrite(29,LOW);
   digitalWrite(30,LOW);
   digitalWrite(31,LOW);}

Source

// Find maximum and minimum element in an arary.
#include<stdio.h>/*printf*/
int main ()
{
  int i;/*loop counter*/
  int a[10] = { 10, 55, 9, 4, 234, 20, 30, 40, 22, 34 };/*array of ints*/
/*set values for comparison starting from the beginning of the array*/
  int max = a[0];
  int min = a[0];

/*step through the array*/
  for (i = 0; i < 10; i++)
 {
/*if anything is greater than the value of max, which  started as the int pointed to with a[0]*/
  if (a[i] > max)
 {
/*set the value found as the new max*/
  max = a[i];
 }
/*if anything is less than the value of min, which started as the int pointed to by a[0]*/
  else if (a[i] < min)
 {
/*set the value found as the new min*/
  min = a[i];
 }
 }
/*after that loop is executed, we should have the minimum and maximum values*/
  printf ("Maximum element in an array : %d\n", max);
  printf ("Minimum element in an array : %d\n", min);

  return 0;
}

Let's start with the basics. What type of sensors are they ?

UKHeliBob:
Let’s start with the basics. What type of sensors are they ?

Hi UKHeliBob

Thank you for replying,

I have such limited skills on coding, as time goes I am learning bits and bobs… my skills remain in PC Software and Hardware diagnosing problems with computers. I can solder and other electrical stuff but the coding is just so frustrating.

I have come along way from my first code which was using ONEWIRE sensors.

But the onewire sensors were really massively oversized and needed to fit sensors in fins of heatsinks and other key locations scattered around my project,

I have a Link to see the build. It’s just a PC with some advanced features is all

To answer your Question, they are NTC type 10K it uses analog input A1-A6

the relay boards use digital outs

The 4 relays, have resistors connected to them, and as each one turns on, it mades the DC-DC PSU up it’s voltage… I have tried using the arduino to make a Powersupply but was way over my head, and ended up trying a MOSFET direct to a fan, tho the fan made too much noise at low RPM so I scrubbed it for the more simple approach.

I am using millis to delay to relays and scan every 5 seconds… to stop from hammering…
the 5 seconds can be altered to anything I like and can iron out the end setup as the system is fully build.

Core i7 8700K,
16GB 3200MHz
5TB HDD
500GB SSD
256GB M.2 CACHE (like a SSHD for both the SSD and HDD if you will) it outputs 3GB/s :slight_smile:
GTX 1080TI AMP Extreme
EVGA 750 P2

this is the system that is being put onto the exterior of the box you see in my photos,
This is version 1.1 a revision and rebuild from my X79 / R9 290X Crossfire I had already

I hope you can help finish this as I am at the end of the project, Just finished making the RED Cables for the system, Next have to make a casing for the M.2 that will replace one of the Crossfired Cards… then I put it back on the wall

Cheers.

Put the sensor pin numbers in an array. Read them by iterating through them using a for loop.

Before the start of the measurement loop set a variable, let’s name it lowValue, to an absurdly high value, say 999

Read each of the sensors and if the value returned is less than the current value of lowValue then set lowValue to that reading. When you have read each sensor then the lowValue variable will hold the lowest value read from the sensors.

Take appropriate action depending on the value.