Hi all. This is my first real project after playing around with the Arduino for some time.
I want to implement a semi-passive fan control for fans in a PC, where I read the PWM duty cyle as an analog voltage and then switch the +12V line with a high-side mosfet switch.
Reading the duty cycle, converting and switching the fan on or off below or above a defined value works fine. But I want to have some kind of hysteresis.
Now my problem: I can't figure out how to switch off below a level of 15% and then back on above 35%, but only if the fan was off before.
My current code just switches off below 15%, turns the fan on and off repeatedly between 15-35% and then off at any higher value.
I'm stuck at this problem and would really appreciate any help and suggestions.
const int inputPin = A0; // PWM input
const int mosfetPin = 9; // Fan output
const int lowLimit =15; // Lower PWM limit
const int highLimit = 35; // Upper PWM limit
int pwmValue; // Hold input value
boolean mosfetOn; // Turn mosfet on? "true" or "false"
void setup()
{
mosfetOn = true; // Start with mosfet on
pinMode(mosfetPin, OUTPUT); // Define as output
Serial.begin(9600);
}
void loop()
{
pwmValue = analogRead(inputPin); // Read value
int pwmLevel = pwmValue * (100 / 1023.0); // Convert to percent
Serial.println(pwmLevel); // Print value
if (pwmLevel < lowLimit) //Is pwmLevel lower than lower limit?
{
mosfetOn = false;
}
else if (mosfetOn == true && pwmLevel < highLimit) //Is mosfet HIGH and pwmlevel lower than upper limit?
{
mosfetOn = false;
}
else
{
mosfetOn = true;
}
if (mosfetOn == true) //Turn mosfet on or off
{
digitalWrite(mosfetPin, HIGH);
}
else
{
digitalWrite(mosfetPin, LOW);
}
delay(100);
}
fan_control.ino.ino (1.29 KB)