Go Down

Topic: If and else if range gap (Read 625 times) previous topic - next topic

gfvalvo

#15
Feb 13, 2018, 03:51 pm Last Edit: Feb 13, 2018, 04:01 pm by gfvalvo
First thing I'd do is change the name of your 'min' variable. I assume that's it's the current temperature reading (in resistance units) since you're trying to control the fans with it. So, it makes no sense to call it 'min' as most people would think that stands for 'minimum'. Also, since you haven't yet posted your complete code, I have no idea how you're actually reading the temperature. So, I'm gonna guess.

Here's an ugly, bute force outline of a way to do it. There are many optimization possible to make it less ugly and more elegant. But, baby steps ….

Code: [Select]
const unsigned int fan4SetPoint = 5900;
const unsigned int fan3SetPoint = 5300;
const unsigned int fan2SetPoint = 4800;
const unsigned int fan1SetPoint = 4500;

const unsigned int detlaT = 50;

void setup() {
// Setup Code Goes Here
}

void loop() {
  unsigned int currentTemp;

  currentTemp = GET_NEW_TEMPERATURE_READING();   // You never showed how you do this
  
  if (currentTemp < fan1SetPoint-deltaT) {
    Turn On Fan1;
  }
  if (currentTemp < fan2SetPoint-deltaT) {
    Turn On Fan2;
  }
  if (currentTemp < fan3SetPoint-deltaT) {
    Turn On Fan3;
  }
  if (currentTemp < fan4SetPoint-deltaT) {
    Turn On Fan4;
  }

  if (currentTemp > fan1SetPoint+deltaT) {
    Turn Off Fan1;
  }
  if (currentTemp > fan2SetPoint+deltaT) {
    Turn Off Fan2;
  }
  if (currentTemp > fan3SetPoint+deltaT) {
    Turn Off Fan3;
  }
  if (currentTemp > fan4SetPoint+deltaT) {
    Turn Off Fan4;
  }
}

toxsickcity

SECTION CONTROL RELAY SWITCH
/*
2480 60c | 2985 55c | 3601 50c | 4541 45c
5324 40c | 6530 35c | 8056 30c | 10000 25c
12500 20c | 15700 15c | 19900 10c

So we're basically talking 500ohms for 5 degree C
So 100ohm would be maybe 1 degree but it's enough.
Plus once I understand how to create this so called deadzone. I can change this to better suit.. should I need 2 or 3 degrees of deadzone (200-300ohms)

I am using ohms as I didn't like it when degree C was being calculated. I still calculate it for serial print so I can refer to it for diagnosis etc.. resistance seems to have alot more integers than degrees C.. 1 degree or 1.0degree vs 100ohms..

. It's not linear as you can see 5degrees can be 2000ohms difference.. I don't care as I can make my dead zones as large as I want.. as it's all just numbers in the end.

UKHeliBob

Let's try this in English rather than in C

Suppose that you want to keep the temperature at about 35 degrees

If you did,
when the temperature is above 35 turn off the heater and when the temperature is below 35 turn on the heater then there could come a point when the heater is being turned on and off in quite rapid succession which will probably be quite bad for the relay and the heater and could be annoying.

However, if you did,
when the temperature is above 38 turn off the heater and when the temperature is below 33 turn on the heater then the relay and heater would turn on and off less frequently and there would be less wear and tear on the system.  You would have introduced 5 degrees of hysteresis.

Of course, you don't have to use the actual temperature you can use any values that represent the temperature such as the output from a sensor.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

toxsickcity

So 'min' does stand for minimum.. it's the reading taken from 6 sensors in an array. I found a code to extract Max and min and I use min as the minimum of 6 sensors..
So the hottest component.

I will work that code as a replacement to how I have it setup and will trial it tomorrow.. it's 2.30am in Australia and I'm buggered..

Can I ask this tho..

if (currentTemp < fan2SetPoint-deltaT) {
   Turn On Fan2;
 }

At 4802ohm and 4798 sorry.. I forgot Delta...

4749 and 4751 ohms..
Would this make the relay turn on and off? If sensor fluctuate between the two values?

As I don't think the code cares that we are using -50? I could be wrong but 4800-50 is 4750.. unless -50 works differently than simple maths which is take 50 from my set point...

The code I currently use sort of works this way..
If I had set < 4750 it will hammer at 4751 and 4749. I added a delay for 30sec to stop this but don't like millis for this as a solution..

I don't see how this is different to saying the code like this

If (min < 4750){
Turn fan on}

My code was set as above and when sensor fluctuate between + or - few ohms from 4750 the relay is on off on off

I am only asking because my current code which is posted above about 9 to 10 posts back for my relays..
It shows the relays code I use.. with some explainations

Note: posted code has a range of resistance.. not a set single resistance..
It was single resistance.. eg.. if (min < 5000) but thought min < 5000 && > 6000) would work for me...

Good

wildbill

There is no difference between using delta and calculating the raw numbers beforehand. Using delta helps you to add hysteresis using this pattern:

Code: [Select]

if (currentTemp < fan2SetPoint-deltaT)
{
   Turn On Fan2;
 }
if (currentTemp > fan2SetPoint+deltaT) {
   Turn Off Fan2;
 }


It's just a convenience, you can do the calc yourself and put the literals in place instead if desired.

toxsickcity

To also mention..

It's a single connection of fans..
Eg. 3 large blowers but wired as 1 fan

So I have fan speeds! Not turning on separate fans...

So the 4 relays when on is like a volume know on a power supply at full voltage.. and all relays off is 4 volts...
Each relay adds 2v so when all 4 relays on the output is 12..
I couldn't work out making a Arduino controlled powersupply so I wired resistors to the relays outputs and that is like my make shift variable resistor for my power supply that power the fans...


toxsickcity

Windbill thanks for your time and replies..

I will try that and let you know the results..

It could be because all my if statements are all <
None of them are >

I am also using if, else if.. where your showing to use only if statements

Anyways will try tomorrow..

Cheers..

gfvalvo

To also mention..

It's a single connection of fans..
Eg. 3 large blowers but wired as 1 fan

So I have fan speeds! Not turning on separate fans...

So the 4 relays when on is like a volume know on a power supply at full voltage.. and all relays off is 4 volts...
Each relay adds 2v so when all 4 relays on the output is 12..
I couldn't work out making a Arduino controlled powersupply so I wired resistors to the relays outputs and that is like my make shift variable resistor for my power supply that power the fans...
None of that matters. It's just noise. The frameworks shown in Reply #15 and Reply #14 are exactly what you need to follow. You could be controlling multiple fans, a single fan with multiple speeds, an air conditioner, etc. It just doesn't matter. The control algorithm is exactly the same. Just fill in the details for your application.

toxsickcity

Hi all

It would appear the new code seems to do the trick.

I have not seen it playup anymore..
I'd like to run further tests bit it all seems good so far.

Go Up