I also add Fan. But now temperature change every time. Not normal change, but weird change. For example, now temperature is 34, but it change until 40 then return to 34 back and it always like that. Sometime it increases, but still return to 34. This is my code.
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int fan = 9;
int tempPin = A2;
int rled = 8;
int bled = 7;
int temp;
void setup()
{
lcd.begin(16,2);
pinMode(tempPin, INPUT);
pinMode(fan, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(bled,OUTPUT);
}
void loop()
{
// Formula to change from volts to celcius
int reading = analogRead(tempPin);
float voltage = reading * 5.0;
voltage = voltage/1024.0;
int temp = voltage * 100;
if(temp >= 50)
{
analogWrite(fan, 255); // Fan speed at full speed
setColor(HIGH, LOW, LOW); // red
}
else
{
analogWrite(fan, 127); // Fan speed at half of fan speed
setColor(LOW, LOW, HIGH); // blue
}
// Display the temperature
lcd.setCursor(0,0);
lcd.print("TEMP:");
//lcd.setCursor(6,0);
lcd.print(temp);
lcd.print((char)223);
lcd.print("C");
delay(1000);
}
void setColor(int red, int green, int blue)
{
digitalWrite(rled, red);
digitalWrite(bled, blue);
}