fan controlled by temperature

Hi,

I am creating a project where a fan will start at a certain temperature. if the temperature is below the min temp i want a green led to light up and fan to not run, then between the min temp and max temp i want a orange led to light up and the fan to start and reach 50% fan speed. when max temp is reached i want a red led to light up and 100% fan speed to be reached.

when i download my code the fan starts automatically even though the temp is below temp min and the red led lights up, not the green led which is supposed to be alight.

I am very new to arduino and only know the basics so I think its just something simple which is causing this to happen but i have tried to resolve it many times but still cant work it out.

Here is my code, if you can see why this would not work that would be a great help.

Thank you

//code from scratch

int pos = 0;                            
int sensorPin = A1;                      //temp sensor
int reading = 0;                         //read temp
int fan = 11;                            //fan
int ledgreen = 8;                        //green led
int ledorange = 7;                       //orange led
int ledred = 5;                          //red led
int tempMin = 10;                        //temp fan starts
int tempMax = 35;                        //temp fan reaches max speed
int fanSpeed;

float voltage = 0;                        //convert temp
float temperatureC = 0;                   //convert temp
float temperatureF = 0;                   //convert temp

void setup()  {
  pinMode(sensorPin, INPUT);
  pinMode(fan, OUTPUT); 
  pinMode(ledgreen, OUTPUT);
  pinMode(ledorange, OUTPUT);
  pinMode(ledred, OUTPUT);
  Serial.begin(9600);
}


void loop()  {

  if(temperatureC<tempMin){                           //if temp is below temp min turn everything off but green led
    digitalWrite(fan, LOW);
    digitalWrite(ledgreen, HIGH);                     
    digitalWrite(ledorange, LOW);
    digitalWrite(ledred, LOW);
    
  fanSpeed = 0;}
  
  if((temperatureC>=tempMin) &&  (temperatureC<tempMax)){     //if temp is the same or above temp min and under temp max then turn on fan(50% speed), servo and orange led
    digitalWrite(fan, HIGH);
    digitalWrite(ledgreen, LOW);
    digitalWrite(ledorange, HIGH);
    digitalWrite(ledred, LOW);

  fanSpeed = 50;}
    
   if(temperatureC>tempMax){                    //if temp is more than temp max then turn on fan(100% speed), servo and red led
    digitalWrite(fan,HIGH);
    digitalWrite(ledgreen, LOW);
    digitalWrite(ledorange, LOW); 
    digitalWrite(ledred, HIGH);
 
  fanSpeed = 100;}
   
   
      
}
  int readTemp(){                                             //to convert temperature's
reading = analogRead(sensorPin);
voltage = ( reading * 5.0) / 1024.0;
temperatureC = (voltage - 0.5) * 100;
temperatureF = temperatureC * 9/5 + 32;
  

  Serial.print(temperatureC);                               //print text of temp and fan speed on serial monitor
delay(1000);
Serial.println(fanSpeed);
  delay(1000);
  }

temp fan fritzing.pdf (191 KB)

hjktimmy:
Hi,

I am creating a project where a fan will start at a certain temperature. if the temperature is below the min temp i want a green led to light up and fan to not run, then between the min temp and max temp i want a orange led to light up and the fan to start and reach 50% fan speed. when max temp is reached i want a red led to light up and 100% fan speed to be reached.

when i download my code the fan starts automatically even though the temp is below temp min and the red led lights up, not the green led which is supposed to be alight.

I am very new to arduino and only know the basics so I think its just something simple which is causing this to happen but i have tried to resolve it many times but still cant work it out.

Here is my code and fritzing image, if you can see why this would not work that would be a great help.

Thank you

//code from scratch

int pos = 0;                            
int sensorPin = A1;                      //temp sensor
int reading = 0;                         //read temp
int fan = 11;                            //fan
int ledgreen = 8;                        //green led
int ledorange = 7;                       //orange led
int ledred = 5;                          //red led
int tempMin = 10;                        //temp fan starts
int tempMax = 35;                        //temp fan reaches max speed
int fanSpeed;

float voltage = 0;                        //convert temp
float temperatureC = 0;                   //convert temp
float temperatureF = 0;                   //convert temp

void setup()  {
 pinMode(sensorPin, INPUT);
 pinMode(fan, OUTPUT);
 pinMode(ledgreen, OUTPUT);
 pinMode(ledorange, OUTPUT);
 pinMode(ledred, OUTPUT);
 Serial.begin(9600);
}

void loop()  {

if(temperatureC<tempMin){                           //if temp is below temp min turn everything off but green led
   digitalWrite(fan, LOW);
   digitalWrite(ledgreen, HIGH);                    
   digitalWrite(ledorange, LOW);
   digitalWrite(ledred, LOW);
   
 fanSpeed = 0;}
 
 if((temperatureC>=tempMin) &&  (temperatureC<tempMax)){     //if temp is the same or above temp min and under temp max then turn on fan(50% speed), servo and orange led
   digitalWrite(fan, HIGH);
   digitalWrite(ledgreen, LOW);
   digitalWrite(ledorange, HIGH);
   digitalWrite(ledred, LOW);

fanSpeed = 50;}
   
  if(temperatureC>tempMax){                    //if temp is more than temp max then turn on fan(100% speed), servo and red led
   digitalWrite(fan,HIGH);
   digitalWrite(ledgreen, LOW);
   digitalWrite(ledorange, LOW);
   digitalWrite(ledred, HIGH);

fanSpeed = 100;}
 
 
     
}
 int readTemp(){                                             //to convert temperature's
reading = analogRead(sensorPin);
voltage = ( reading * 5.0) / 1024.0;
temperatureC = (voltage - 0.5) * 100;
temperatureF = temperatureC * 9/5 + 32;

Serial.print(temperatureC);                               //print text of temp and fan speed on serial monitor
delay(1000);
Serial.println(fanSpeed);
 delay(1000);
 }

It looks like you never call your readTemp() function. You're not seeing anything on the serial monitor, right?

You need to put readTemp(); at the beginning of your main loop before any if-statements.

Since you are not returning a value from your function, you can define your function as:

(void) readTemp(){...

(But, don't include the (void) when you call the function from your main loop.)

P.S.
A couple more comments & suggestions -

You don't really need a function since you are only going to call the function once, from the same place, every time the program loops. You could put the code to read the temperature and convert to C & F in-line inside the loop. However, a function is "cleaner" and it generally makes your code easier to debug. And eventually, you should know how to write & use functions because that's how C is written.

For testing purposes, you can also "hard code" values such as reading = 200 or temperatureC = 10, etc. If you have trouble, you can hard-code those variables in your main loop instead of inside the function (then, don't call the function from the main loop).