 # using a minimum pwm value for thermal fan

hi all,

i hope to get some help with 2 line of code

fanSpeed = map(temp, fantempMin, fantempMax, 32, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fan

the above is the speed mapping for a fan.
i would love to make the fans run at a minimum speed of xxx pwm duty cycle and speedup from that value

fantempmin and max are actually resistance values of the of the thermistor.
this gave me more steps of variance…

full code

``````float fantemp;
int fantempPin = A1; //arduino pin used for temperature sensor
int fantempMin = 10000; // the temperature to start the buzzer
int fantempMax = 6500;
int fan = 11; // the pin where fan is connected default 6
int fanSpeed = 0;

#define FIVEMIN (1000UL * 5) // actually changed to 5 sec.
unsigned long rolltime = millis() + FIVEMIN;

// Thermistor code below

#define THERMISTORPIN A0 // which analog pin to connect
#define THERMISTORNOMINAL 10000 // resistance at 25 degrees C
#define TEMPERATURENOMINAL 25   // temp. for nominal resistance (almost always 25 C)
#define NUMSAMPLES 2// how many samples to take and average, more takes longer // but is more 'smooth'
#define BCOEFFICIENT 3950// The beta coefficient of the thermistor (usually 3000-4000)
#define SERIESRESISTOR 10000// the value of the 'other' resistor

/*
2480 60c
2985 55c
3601 50c
4541 45c
5324 40c
6530 35c
8056 30c
10000 25c
12500 20c
15700 15c
19900 10c
*/
#define COLDTEMP 6500.00 //HOT VALUE !resistance values... better resolution
#define HOTTEMP  10000.00 //COLD VALUE! refer to ntc 10k temp chart

#define RELAY1  31 // Relay on digital pin 4 **********

int samples[NUMSAMPLES];
//int redPin = 10; //now led
//int greenPin = 10;
//int bluePin = 9; //now led2
/*

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9;           // the pin that the LED is attached to
int led2 = 10;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int brightness2 = 255;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int fadeAmount2 = 5;    // how many points to fade the LED by
int fadeAmount3 = 5;    // how many points to fade the LED by
int fadeAmount4 = 5;    // how many points to fade the LED by
int brightnessA1 = 180;  // how bright the LED is
int fadeAmountA1 = 2;    // how many points to fade the LED by
int brightnessA2 = 60;   // how bright the LED is
int fadeAmountA2 = 2;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);

void Setup() {

//fanstuff
pinMode(fan, OUTPUT);
pinMode(fantempPin, INPUT);
Serial.begin(9600);

//endfanstuff

void loop() {

pinMode(RELAY1, OUTPUT); // ADDED FOR RELAY PIN4 **********
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
delay(2);
}

// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
//Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
//   Serial.print("Thermistor resistance ");
// Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
steinhart = log(steinhart);                  // ln(R/Ro)
steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart;                 // Invert
steinhart -= 273.15;                         // convert to C

// Serial.print("Temperature ");
// Serial.print(steinhart);
// Serial.println(" *C");
// Serial.println(average);

uint8_t red, blue;
// float temp = steinhart;  //make temp real degree c
float temp = average; //make temp read resistance
if (temp < COLDTEMP) temp = COLDTEMP;
if (temp > HOTTEMP) temp = HOTTEMP;
// map temperature to red/blue color

// hotter temp -> more red
red = map(temp, COLDTEMP, HOTTEMP, 255, 0);  // reverse 0, 255 for resistance use
// hotter temp -> less blue
blue = map(temp, COLDTEMP, HOTTEMP, 0, 255);
setColor(red, 0, blue);
delay(2);

//fan stuff
fantemp = (fantemp *5.0*100.0)/1024.0; //calculate the temperature in Celsius
//Serial.println(fantemp);
//delay(1000); // delay in between reads for stability
if(temp < fantempMax) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
//digitalWrite(fan, LOW);
analogWrite(11, 255);
}
if((temp >= fantempMax) && (temp <= fantempMin)) //if temperature is higher than the minimum range
{
fanSpeed = map(temp, fantempMin, fantempMax, 32, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
//analogWrite(11, 255);
}

// signed comparison for proper handling of timer rollover
if((long)(millis() - rolltime) >= 0) {

//SECTION CONTROL RELAY SWITCH ****************
if (temp > 8000) { // also in resistance! //must float new value with  =steinhart; FOR TEMP.
digitalWrite(31,LOW);
} else if (temp < 9000) {
digitalWrite(31,HIGH);}
rolltime += FIVEMIN;}
// delay(50);
}

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif

analogWrite(led2, red);
//  analogWrite(greenPin, green); // no longer using green
analogWrite(led, blue);
}
/*
// set the brightness of pin 9:
analogWrite(led, brightness);
analogWrite(led2, brightness);
// change the brightness for next time through the loop:
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
}
delay(30);

*/
/*
for (int i = 0; i <= 100; i++) {
// set the brightness of pin 9:
analogWrite(led, brightness);
analogWrite(led2, brightness);
// change the brightness for next time through the loop:
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255){
delay(30);}

*/
//}
``````

thanks
shaun

Have you tried:

``````fanSpeed = constrain(fanSpeed, MINspeed, MAXspeed);
``````

Hi

Can anyone explain everything happening in this line

fanSpeed = map(temp, fantempMin, fantempMax, 32, 255);

outsider: Have you tried:

``````fanSpeed = constrain(fanSpeed, MINspeed, MAXspeed);
``````

Can you tell me if this should be used at the line above my line..

Minspeed and Maxspeed should be values of pwm? Such as

fanSpeed = constrain(fanSpeed, 80, 255);

Is this how I'd do it?