// 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...
#include <OneWire.h>
#include <DallasTemperature.h>
/*----------------------- User adjustable variables and preferences section ---------------------------------*/
float tempForFanStartup = 82.0; // 175 target low temp. below this temperature, the fan will be off
float tempForFanOnFull = 105.0; // 210 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 voltsForFanStartup = 2.0; // 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 = 7; // 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
float fanSpeedPct;

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

void setup() { /* ++++++++++++++++++ Setup is run once when the arduino boots ++++++++++++++++++++++++++*/
Serial.begin(115200); // set up serial port for 115200 baud (optional)
sensorClt1.begin();

sensorClt1.requestTemperatures();
currTemperature = sensorClt1.getTempCByIndex(0);

//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.

print_to_signal_generator_softserial();

} // 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

fanSpeedPct = map(pwmDuty, 0, 255, 0, 100);

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 +++++++++++++*/

sensorClt1.requestTemperatures();
currTemperature = sensorClt1.getTempCByIndex(0);
} // 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);
Serial.print(F("Percentage: ")); Serial.println((int)fanSpeedPct);
}

void print_to_signal_generator_softserial() {
  // Check the length of fanSpeedPct
  if (fanSpeedPct < 10) {
    Serial.print(F("D00")); // Add "D00" prefix for single-digit fan speed
  } else if (fanSpeedPct < 100) {
    Serial.print(F("D0")); // Add "D0" prefix for double-digit fan speed
  } else {
    Serial.print(F("D")); // Add "D" prefix for triple-digit fan speed
  }
  
  Serial.print((int)fanSpeedPct); // Cast fanSpeedPct to int to remove decimals
}