Newbie going bonkers!
Hi all - I am a machinist building a tensile testing machine using Arduino MEGA2560, and I want the fan to be controlled by the cabinet temperature.
This worked perfectly well initially, but not after making a few changes to save settings in EEPROM.
The first call in FanControl::update()
to analogWrite(fanPin, 255)
still works fine.
But the second call to analogWrite(fanPin, int(fanSpeed))
does not work.
Even if I replace analogWrite(fanPin, int(fanSpeed))
with analogWrite(fanPin, 255)
nothing happens.
//v19
#include "FanControl.h"
#include <EEPROM.h>
#include <DallasTemperature.h>
// EEPROM addresses for the calibration offsets and temperature bounds
#define FAN_CALIBRATION_OFFSET_ADDR 12
#define FAN_MIN_TEMP_ADDR 16
#define FAN_MAX_TEMP_ADDR 20
FanControl::FanControl(int oneWireBus, int fanPin, float calibrationOffset, int minTemp, int maxTemp):
oneWire(oneWireBus), sensors(&oneWire), fanPin(fanPin), fanStartMode(true), hasPrinted(false), lastUpdateTime(0), startModeTime(0) {
// Read the stored values from EEPROM upon initialization
EEPROM.get(FAN_CALIBRATION_OFFSET_ADDR, this->calibrationOffset);
EEPROM.get(FAN_MIN_TEMP_ADDR, this->fanMinTemp);
EEPROM.get(FAN_MAX_TEMP_ADDR, this->fanMaxTemp);
}
void FanControl::initialize() {
sensors.begin(); // Start communication with the DS18B20 sensor
pinMode(fanPin, OUTPUT); // Set the fan pin as output
startModeTime = millis() + 3000; // Delay fan start by 3 seconds
fanStartMode = true; // Ensure that start mode is activated
}
void FanControl::update() {
unsigned long currentMillis = millis();
sensors.requestTemperatures();
float rawTemperature = sensors.getTempCByIndex(0);
if (rawTemperature == DEVICE_DISCONNECTED_C) {
Serial.println("Error: Sensor disconnected!");
return;
}
// Check if in delayed start period
if (currentMillis < startModeTime) {
return; // In delayed start period, do nothing (return=exit function)
}
if (fanStartMode && (currentMillis - startModeTime < 2000)) {
analogWrite(fanPin, 255); // Run the fan at maximum speed
return;
} else if (fanStartMode) {
fanStartMode = false; // Deactivate start mode, move to temperature control
}
if (!fanStartMode && (currentMillis - lastUpdateTime >= 8000)) { // Update every 3 seconds
lastUpdateTime = currentMillis;
sensors.requestTemperatures();
float rawTemperature = sensors.getTempCByIndex(0); // Read raw temperature
//float rawTemperature = 23.19; // Just for testing
float correctedTemperature = rawTemperature + calibrationOffset; // Apply calibration offset
// Calculate fan speed based on temperaature
Serial.println("========================");
Serial.print("Cabinet temperature: ");Serial.println(correctedTemperature);
Serial.println(fanMinTemp);
Serial.println(fanMaxTemp);
int fanSpeed=0;
if (correctedTemperature < fanMinTemp) {
fanSpeed = 0; //Stop if temperature is below minTemp
} else if (correctedTemperature > fanMinTemp && correctedTemperature < fanMaxTemp) {
// Linear interpolation between fanMinTemp and fanMaxTemp
Serial.println("1");
fanSpeed = map(correctedTemperature, fanMinTemp, fanMaxTemp, 90, 255);
} else if (correctedTemperature >= fanMaxTemp) {
fanSpeed = 255; //Max speed if temperature is above maxTemp
}
Serial.print("fanSpeed : ");Serial.println(fanSpeed);
analogWrite(fanPin, int(fanSpeed));
}
}
void FanControl::checkSerial(String command) {
Serial.println("Received in FanControl: " + command);
if (command.startsWith("calOffset:")) {
float offset = command.substring(10).toFloat();
saveCalibrationOffset(offset);
Serial.print("***** New FanControl calibration offset set to: ");
Serial.println(offset, 2);
} else if (command.startsWith("minTemp:")) {
int minTemp = command.substring(8).toInt();
saveTemperatureBounds(minTemp, this->fanMaxTemp);
Serial.print("****** New FanControl minTemp set to: ");
Serial.println(minTemp,1);
} else if (command.startsWith("maxTemp:")) {
int maxTemp = command.substring(8).toInt();
saveTemperatureBounds(this->fanMinTemp, maxTemp);
Serial.print("****** New FanControl maxTemp set to: ");
Serial.println(maxTemp,1);
}
}
void FanControl::saveCalibrationOffset(float offset) { //Write offset to EEPROM
EEPROM.put(FAN_CALIBRATION_OFFSET_ADDR, offset);
this->calibrationOffset = offset;
}
void FanControl::saveTemperatureBounds(int minTemp, int maxTemp) { //Write bounds to EEPROM
EEPROM.put(FAN_MIN_TEMP_ADDR, minTemp);
EEPROM.put(FAN_MAX_TEMP_ADDR, maxTemp);
this->fanMinTemp = minTemp;
this->fanMaxTemp = maxTemp;
};
In serial monitor I get
========================
Cabinet temperature: 23.30
22
35
1
fanSpeed : 102
-- which seems fine.
What am I missing?