I'm just trying to get back into the hang of using arduinos and this is my first project.
Why isn't the fan turning off below the tempMin value?
Managed to get the temp reading value correct etc, just not the fan control function
Any help would be much appreciated
float temp;
int tempPin = A0; //arduino pin used for temperature sensor
int tempMin = 25; // the temperature to start the buzzer
int tempMax = 70;
int fan = 6; // the pin where fan is connected
int fanSpeed = 0;
void setup() {
pinMode(fan, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin)*5/1024.0;
temp = temp - 0.5;
temp = temp / 0.01;
Serial.print ("Temperature: ");
Serial.println(temp);
delay(1000); // delay in between reads for stability
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) //if temperature is higher than the minimum range
{
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
}
Double check your wiring/circuit, specifically the fan side.
I'm not quite sure what circuit you're trying to implement there, but whatever it is, it's not correct. You are essentially just plugging the fan into a power source.
b) you've got a three state system and you're only providing conditional functions for two of those states, never a good idea.
c) to avoid confusion, re-examine your tests. In your first test, temp < tempMin then your second test immediately if temp >= tempMin. Well if the first fails, the second must be true. So,
if(temp < tempMin)
{
blah, blah
}
else
{
if (temp <= tempMax)
{
blah, blah, blah
}
else
{
blah, blah, blah, blah // the third and final conditional test ensuring all possibilities are covered.
}
}
By structuring your tests like this, you can guarantee that only one of you conditional statement blocks is ever executed.
Last thing, and I'm sure you got some compiler warnings, you're testing a float against an integer, also not a very good idea.
You need hysteresis, otherwise the fan will be constantly switching on and then off again which is
annoying and can wear things out:
#define OFF_TEMP 24.0
#define MIN_TEMP 26.0 // a few degrees of hysteresis to prevent hunting on and off
#define MAX_TEMP 55.0
....
float temp = get_temperature() ;
if (temp <= OFF_TEMP)
switch_fan_off () ; // use lots of short well-named functions for everything
else if (temp > MAX_TEMP)
switch_fan_on () ;
else if (temp > MIN_TEMP)
set_fan_speed (map (temp, MIN_TEMP, MAX_TEMP, 32, 255)) ;
// no need for any delays, hysteresis will do the job.
....
Just make sure you disconnect the red wire from the battery before making the changes.
Once the new wiring arrangement is okay, re-connect the red wire.
I am making the assumption that the NPN and temperature sensor are correctly wired.
I am also making the assumption that the stripe on the diode is closes to the red wires.
The wire marked with blue crosses should be removed.