I am trying to control an exhaust fan in a greenhouse with an h-bridge. I'm not sure on how to keep the fan at full speed when the temperature reaches the maximum in the code. The fan reaches full speed at maximum, but when the temperature rises, the fan starts and stops intermittently. If I raise the maximum temperature in the code, the fan does not exhaust the air fast enough to keep the temperature down.
I have pasted the pertinent code below. I have also played with the map statement a bit (moved the 255 figure to 512, and down to 127). Is there something I am missing in the code? Should I approach this differently? I appreciate the help.
#define R_EN 26 //Fan enable
#define R_PWM 2 //Fan Motor
DHT dht(DHTPIN, DHTTYPE);
#define tempF1 (dht.readTemperature(true))
int tempMin = 75;
int tempMax = 95;
int fanSpeed;
const int freq = 5000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 50;
void setup() {
pinMode(R_EN, OUTPUT);
pinMode(R_PWM, OUTPUT);
digitalWrite(R_EN, HIGH);
// configure LED PWM functionalitites
ledcSetup(pwmChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(R_PWM, pwmChannel);
}
void loop() {
fanOn();
currentTime = millis();
}
void fanOn() {
if (currentTime - tempMillis > tempInterval) {
tempMillis = currentTime;
Serial.print(tempF1); Serial.println(" Degrees");
if (tempF1 < tempMin) {
fanSpeed = 0;
digitalWrite(R_EN, LOW);
digitalWrite(fan_LED, LOW);
Serial.println("fan off");
//Serial.println(tempF1);
} else {
//openDoor();
if (tempF1 >= tempMin) {
fanSpeed = map(tempF1, tempMin, tempMax, 0, 255);
digitalWrite(R_EN, HIGH);
digitalWrite(fan_LED, HIGH);
ledcWrite(pwmChannel, fanSpeed);
}
Serial.print ("Fan Speed: "); Serial.println(fanSpeed);
//Serial.print(tempF1); Serial.println(" Above");
}
}
}