#include <LiquidCrystal.h>
//declare variables
//declare variables
const int relayPin1 = 7;
const float fanOffTemp = 22.0;
const float baseTemp = 23.0;
const float fanOnTemp = 25.5;
float tempC = 0; // variable for holding Celcius temp (floating for decimal points precision)
int tempPin = 0; // Declaring the Analog input to be 0 (A0) of Arduino board.
float samples[8]; // array to hold 8 samples for Average temp calculation
float maxi = 0,mini = 100; // max/min temperature variables with initial values. LM35 in simple setup only measures Temp above 0.
int i;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
pinMode(relayPin1,OUTPUT);
digitalWrite(relayPin1, HIGH);// ** USE Normaly Open on relay
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(0, 0); // set LCD cursor position (column, row)
lcd.print("PalkoCodes V2.0"); // print text to LCD
lcd.setCursor(0, 1);
lcd.print("TempSensorRelay");
delay(2000); // wait 2000ms
lcd.clear(); // clear LCD display}
void loop(){
Serial.println(analogRead(tempPin)); // Displays on serial monitor the sampled value before conversion to real Temperature reading
// Start of calculations FOR loop.
for(i = 0;i<=7;i++){ // gets 8 samples of temperature
samples[i] = ( 1.5 * analogRead(tempPin) * 100.0) / 1024.0; // conversion math of LM35 sample to readable temperature and stores result to samples array. 1024 is the Bit depth (quantization) of Arduino.
// 5 is the supply volts of LM35. Change appropriatelly to have correct measurement. My case is 4.4Volts.
// Serial.println(samples[i]);
Serial.print("."); // print a dot for every sample at serial monitor
// Display Current Celcius Temp to LCD
// ( LCD note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0); // set LCD cursor position
lcd.print("Current T: "); // print to LCD
lcd.setCursor(12, 0);
lcd.print(samples[i]); // print current Temp sample to LCD
tempC = tempC + samples[i]; // do the addition for average temperature
delay(800); // wait 800ms
}
// END of FOR loop
Serial.println(""); // Like and CR at serial monitor
Serial.println("");
tempC = tempC/8.0; // calculated the averare of 8 samples in Celcius
if(tempC > maxi) {maxi = tempC;} // set max temperature
if(tempC < mini) {mini = tempC;} // set min temperature
// Send Results to Serial Monitor
Serial.println("New measurement");
Serial.print(" Average Temperature in Celcius is " );
Serial.println(tempC-2);
Serial.print(" MAX Temperature in Celcius is " );
Serial.println(maxi);
Serial.print(" MIN Temperature in Celcius is " );
Serial.println(mini);
// Send results to LCD.
lcd.setCursor(0, 0);
lcd.print("Av.T Max Min");
lcd.setCursor(0, 1);
// print the measured temp average
lcd.print(tempC-2);
lcd.setCursor(6, 1);
// print the maximum temp
lcd.print(maxi);
lcd.setCursor(12, 1);
// print the minimum temp
lcd.print(mini);
delay (1000);
if(tempC-2 <= baseTemp && tempC-2 < fanOnTemp){
digitalWrite(relayPin1, HIGH);
delay(500);
} else if (tempC-2 >= fanOnTemp){
digitalWrite(relayPin1, LOW);
delay (3000);
} else if (tempC-2 <= fanOffTemp){
digitalWrite(relayPin1, HIGH);
delay (500);
delay(3000); // Wait about 3 seconds to display the results to LCD screen befor starting the loop again
tempC = 0; // Set tempC to 0 so calculations can be done again
}}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.