Fan Control


#include 

/*
 * for more information about pulse width modulation, see
 * https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
 */

const int fanPin=9;
const int thermPin=A2;

int _curSpeed=0;
int _curDirection=1;

const int MINTEMP=60;
const int MAXTEMP=85;

#define DHTPIN 8
#define DHTTYPE DHT11
DHT _dht(DHTPIN,DHTTYPE);

// PWM output @ 25 kHz, only on pins 9 and 10.
// Output value should be between 0 and 320, inclusive.
void analogWrite25k(int pin, int value)
{
  if (value>=0 && value<=320)
    switch (pin) {
        case 9:
            OCR1A = value;
            break;
        case 10:
            OCR1B = value;
            break;
        default:
            // no other pin will work
            break;
    }
}

int CalcFanSpeed(int temp)
{
  int tempRange=MAXTEMP-MINTEMP;
  double fanSpeedInc=320/tempRange;

Serial.print(fanSpeedInc); Serial.println(" fanSpeedInc");

  if (temp>MAXTEMP)
    temp=MAXTEMP;
  else if (temp