help with incrementing code

Hi ardunio fans,

just need a little coding help with my project to finish off my home ventilation controller, here is some variables to hopefully help you understand the code and what im trying to do

ttemp = 24 (target temperature set in another function)
fspeed = 210 (set in setup)
userfspeed = manual set fan speed in another function low/mid high etc
the fan max speed is fspeed == 0
fan off is fspeed == 255
fan slowest setting just before stall fspeed == 220 so half speed is 110 etc

now if temp2 is <= ttemp i want the motor to just tickle over but not off thats set in the function below

but if temp2 is > ttemp i want the fan to speed up but in increments if temp2 is 5 degrees hotter turn the fan speed up 20 %, 10 degrees hotter 40%, 15 degrees hotter 60% 20 degrees hotter 80% bla bla

{
  
  digitalWrite(9, LOW);
  if (fanonoff == 0) {
    analogWrite(motorPin, 255);
      }
   if (fanonoff ==1) {
     analogWrite(motorPin , fspeed);
   }
  if (fanonoff == 2{
     analogWrite(motorPin, userfspeed); 
  }
  float temp1 = sensors.getTempC(insideThermometer);
  float temp2 = sensors.getTempC(outsideThermometer);
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    if (temp2 <= ttemp){ 
      analogWrite(motorPin, 220); 
{
  if (ttemp is x amount greater than temp2 then decrease fspeed value to speed fan up
}{

if you need more information i will try the best i can to explain thanks in advance

Tiaki

adrunio newbee

  float temp1 = sensors.getTempC(insideThermometer);
  float temp2 = sensors.getTempC(outsideThermometer);
  float tempC = sensors.getTempC(deviceAddress);

insideThermometer and outsideThermometer are great names. deviceAddress is not. Nor are temp1, temp2, and tempC.

What kind of thermometers are you using? Are you really interested in fractional temperature values?

but if temp2 is > ttemp i want the fan to speed up but in increments if temp2 is 5 degrees hotter turn the fan speed up 20 %, 10 degrees hotter 40%, 15 degrees hotter 60% 20 degrees hotter 80% bla bla

Why? If the temperature is a little over the set point, run the fan slowly. If it is way over, run the fan full speed.

Define a range of temperature differences, from 0 to some upper limit, like 25 degrees. Then, simply map() the difference from 0 to 25 to 0 to 255. Then constrain() the value to be in the range 0 to 255.

This will do it:

if (temp2 <= ttemp)
  { 
  analogWrite(motorPin, 220); 
  }
else
  {
  int FanPercent = ((temp2-ttemp)/5+1)*10;
  FanPercent=constrain(FanPercent,0,100);
  analogWrite(motorPin, 220-FanPercent*220/100);
  }

But having written it, I realized that this does the same thing:

int FanPercent = ((temp2-ttemp)/5+1)*10;
FanPercent=constrain(FanPercent,0,100);
analogWrite(motorPin, 220-FanPercent*220/100);

not compiled, not tested

insideThermometer and outsideThermometer are great names. deviceAddress is not. Nor are temp1, temp2, and tempC.

What kind of thermometers are you using? Are you really interested in fractional temperature values?

i am using the DS18B20 1 wire temp probes deviceAddress is juts whats is used in the 1 wire temp example and is called in the temp fuction void printTemperature(DeviceAddress deviceAddress) which both DS18B20 are delared at the top of the code

OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
//The thermometers are identified by their unique identifier
DeviceAddress insideThermometer = { 0x28, 0x2B, 0x84, 0x3F, 0x03, 0x00, 0x00, 0xB2 };
DeviceAddress outsideThermometer  = { 0x28, 0x9A, 0x79, 0x3F, 0x03, 0x00, 0x00, 0xC8 };

but if temp2 is > ttemp i want the fan to speed up but in increments if temp2 is 5 degrees hotter turn the fan speed up 20 %, 10 degrees hotter 40%, 15 degrees hotter 60% 20 degrees hotter 80% bla bla Why? If the temperature is a little over the set point, run the fan slowly. If it is way over, run the fan full speed.

its a powerful 200mm commercial centrifugal inline fan there is a massive difference from slow speed to full speed, if the temp is high enough to run the fan on full speed it will draw away enough hot air it will lower the temp and then cycle to slow speed then to fast speed when temp rasies again, and in my house there is vents in the all rooms it will be very annoying to have it cycle like this so i want a consistent speed at different temps above my set target temperature for comfort.

i am using the DS18B20 1 wire temp probes deviceAddress is juts whats is used in the 1 wire temp example

Which you are free to extend/alter/modify, as you did. There is no reason to stick with meaningless names, when you know better names.

its a powerful 200mm commercial centrifugal inline fan there is a massive difference from slow speed to full speed, if the temp is high enough to run the fan on full speed it will draw away enough hot air it will lower the temp and then cycle to slow speed then to fast speed when temp rasies again, and in my house there is vents in the all rooms it will be very annoying to have it cycle like this so i want a consistent speed at different temps above my set target temperature for comfort.

The idea, though, is that a small rise about set point should cause the fan to run slowly. As the temperature rises higher above the set point, the fan should speed up, in small steps, not in big jumps. The temperature in the space will be more consistent with a smooth increase in speed vs. big jumps.

Correct that was the original Queston on how to code it, but what you explained in your last post is exactly what I want the fan to do, I am not worried about my code names yet when I have it going correctly I can strip allot of meaningless code out I am a programming newbee.

Might I suggest -

FanSpeed = int(( CurrentTemp - SetTemp)/5);

case(Fanspeed){ switch(FanSpeed) case 1: case 2: FanDrive = 0; break; // less than 15 degrees case 3: FanDrive = 15; // 15 - 25 degrees break; case 4: FanDrive = 25; break; case 5: etc...

Basicaly scale your temperature difference by some constant ans then use that constant to set the fan speed.

I think I need to learn how to map values, and define max min values then I can make my fan speed variable through the whole analog write range

I think I need to learn how to map values, and define max min values then I can make my fan speed variable through the whole analog write range

You need to decide how far above the target temperature the fan should run full speed. I'd recommend making that a #define so you can easily change it.

define MAXDIFF 20

Then, something like this should work:

int deltaT = outsideTemp - targetTemp;
if(deltaT > 0)
{
  int fanSpeed = map(deltaT, 0, MAXDIFF, 0, 255);
  fanSpeed = constrain(fanSpeed, 0, 255);
  analogWrite(fanPin, fanSpeed);
}

PaulS

your little code working brilliant for the auto fan speed It just works reverse fan speeds up as temp goes down i just need to change the map values thanks heaps

void printTemperature(DeviceAddress deviceAddress)

{

  float temp1 = sensors.getTempC(insideThermometer);
  float temp2 = sensors.getTempC(outsideThermometer);
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    //
      int deltaT = temp2 - ttemp;
   
    digitalWrite(9, LOW);
  if (fanonoff == 0) {
    analogWrite(motorPin, 255);
      }
   if (fanonoff == 1) {
     int fanSpeed =map(deltaT, 0, MAXDIFF, 0 , 255);
    fanSpeed = constrain(fanSpeed, 0, 255);
     analogWrite(motorPin , fanSpeed);
   }
  if (fanonoff == 2){
     analogWrite(motorPin, userfspeed); 
  }
  }
}

your little code working brilliant for the auto fan speed It just works reverse fan speeds up as temp goes down i just need to change the map values thanks heaps

      int deltaT = temp2 - ttemp;

Just swap the two values, subtract the actual temp from the target temperature.

Hi PaulS

i reversed the map values so in stead of int fanSpeed =map(deltaT, 0, MAXDIFF, 0 , 255);

i put int fanSpeed =map(deltaT, 0, MAXDIFF, 255 , 0);

i reversed the map values

That works, too. A little less intuitively obvious, later, but if you comment the code well, that doesn't really matter.

i try to comment my code as might as i can i get lost in my own sketch its up to 15474 bytes but there is still a bit of junk in it that i will take out