// This a test program, no warranties are implied or given. fanControllerR2V3base
// Use, copy any modify this test program any way you want, at your own risk.
// Test, test, test and then test some more..
// It reads a temperature sensor and controls a Mazda PWM fan power module.
// ...Carl... (LS1Tech)
// Modified for use with Dallas 1820 digital sensor, also Celcius for proper understandable values.
// Used with ESP32 S2 "Wemos/Lolin" and a simple bootstrapped p-channel transistor.

#include <OneWire.h>
#include <DallasTemperature.h>

/*----------------------- User adjustable variables and preferences section ---------------------------------*/
float tempForFanStartup = 82.0; // 175.0 target low temp. below this temperature, the fan will be off
float tempForFanOnFull = 105.0; // 210.0 target High temp. above this temperature, the fan will be on at full power
// adjust these two to get the desired range. for example for a transmission, maybe 140 to 160

//float R0 = 10000; // The base resistance of the NTC sensor used. 10K with a 3435 Beta.
//float Beta = 3435; // The Beta of the sensor used. Very commonly available

float voltsForFanStartup = 1.2; // Roughly the signal voltage that triggers the mazda module's slowest speed.
// A higher voltage here will effectively increase the fans lowest speed target.
/*----------------------- end of User adjustable variables and preferences -----------------------------------*/

const int fanPwmOutPin = 12; // Arduino forces this pin to 0 or 5 volts.
const int tempSensorPin = 8; // Pin to read analog voltage from the temp sensor.
int pwmDuty; // The calculated PWM duty is stored here
float pwmMinStartupDuty; // the starting duty is stored here (mazda module starts fans at about 24 % duty)
float currTemperature; // the temperature in F is stored here

OneWire oneWire1 (tempSensorPin);
DallasTemperature sensor1(&oneWire1);

void setup() { /* ++++++++++++++++++ Setup is run once when the arduino boots ++++++++++++++++++++++++++*/
Serial.begin(115200); // set up serial port for 115200 baud (optional)
sensor1.begin();
pinMode(tempSensorPin, INPUT);
//analogReference (EXTERNAL) ; // note, this is using the 3.3 volt supply as the analog reference.
//analogRead (tempSensorPin) ; // a couple of reads to give the A/D time to adjust
//analogRead (tempSensorPin) ; // a couple of reads to give the A/D time to adjust
analogWrite(fanPwmOutPin, 0); // turn the fan off at start

pwmMinStartupDuty = (voltsForFanStartup / 5.0) * 255.0; // convert the Mazda starting voltage to a PWM duty
} // end setup


void loop() { /* ++++++++++++++++++ Main Loop, continuously loops forever ++++++++++++++++++++++++++++*/

readAndTranslateTempSensor();

calculate_and_send_PWM();

//print_to_serial_port(); // un-comment this line for testing and calibration to the laptop.

} // end main loop


void calculate_and_send_PWM() { /* ++++++++ subroutine to calculate and set PWM duty cycle ++++++++++++++*/

if (currTemperature < tempForFanStartup) { // If the temperature is below the lowest setpoint, turn fan off
analogWrite(fanPwmOutPin, 0); // PWM duty = 0 percent
pwmDuty = 0;
return;
}

if (currTemperature > tempForFanOnFull) { // If the temperature is above the highest setpoint, turn fan on full
analogWrite(fanPwmOutPin, 255); // PWM duty = 100 percent
pwmDuty = 255;
return;
}

float tempRange = tempForFanOnFull - tempForFanStartup ; // start calculating duty cycle
float pwmRange = 255.0 - pwmMinStartupDuty ;
float pwmDutyPct = (currTemperature - tempForFanStartup) / tempRange ;

pwmDuty = (int) (pwmMinStartupDuty + (pwmDutyPct * pwmRange) +.5); // actual PWM duty is calculated here

if (pwmDuty > 255) { // make sure duty ended up between 255 and 0
pwmDuty = 255;
}
if (pwmDuty < 0) {
pwmDuty = 0;
}

analogWrite(fanPwmOutPin, pwmDuty); // write PWM duty to PWM output pin

} // end calculate_and_send_PWM


void readAndTranslateTempSensor() { /* ++++++ subroutine to read and translate temp sensor to temp F +++++++++++++*/
//float currTemperature;
sensor1.requestTemperatures();
currTemperature = sensor1.getTempCByIndex(0);
//int tmp = analogRead(tempSensorPin);
//float r = ((1023.0*R0)/(float)tmp)-R0;
//currTemperature = Beta/log(r/0.09919) - 273.15; // for a 10K thermister with a beta of 3435
//currTemperature = (9.0/5.0) * currTemperature +32; // convert to Fahrenheit

} // end readAndTranslateTempSensor


void print_to_serial_port() { /* ++++++++++ optional prints values to laptop usb port for debugging and calibration ++*/
Serial.print(F("currTemperature C: ")); Serial.print(currTemperature);
Serial.print(F(" pwmDuty: ")); Serial.println(pwmDuty);
}