Hello all.
Im trying to make Fan control depending on temperature.
I have 4 sensors and 3 is just for viewing ambient temperature in the room etc. (MCP9700 sensors)
but all readings im displaying on my LCD flickers at about 0.5. for instanse its now showing 24.2 degrees C but its flickering between 24.2 and 24.7 and sometimes down to 23.7.
Any ideas as to how this is ? is it because the input into my index is raw sensor readings ?
Do i need to convert the data into degree C before putting it into the index ? and if so how do i do that ?
#include <LiquidCrystal.h>
#include <Encoder.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int numReadings1 = 20;
const int numReadings2 = 20;
const int numReadings3 = 20;
const int numReadings4 = 20;
int readings1[numReadings1]; // the readings from the analog input
int readings2[numReadings2];
int readings3[numReadings3];
int readings4[numReadings4];
int index1 = 0; // the index of the current reading
int index2 = 0;
int index3 = 0;
int index4 = 0;
int total1 = 0; // the running total
int total2 = 0;
int total3 = 0;
int total4 = 0;
int average1 = 0; // the average
int average2 = 0;
int average3 = 0;
int average4 = 0;
float temp1; //float for calculating temperature
float temp2;
float temp3;
float temp4;
int inputPin1 = A0; //sensor 1
int inputPin2 = A1; //sensor 2
int inputPin3 = A2; //sensor 3
int inputPin4 = A3; //sensor 4
const int LED = 13; //warning light for sensor 1
const int FAN = 3; // cooling fan PWM controlled
const int potPin = A4; //input to adjust the light level PWM controlled
int tempMin = 40;
int tempMax = 70;
int fanSpeed;
int fanLCD;
const int analogInPin = A4; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 5; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int lightLCD = 0; // PWM output for LED light
void setup()
{
Serial.begin(9600);
lcd.begin(20, 4);
pinMode(LED, OUTPUT);
pinMode(FAN, OUTPUT);
pinMode(5, OUTPUT);
// initialize all the readings to 0:
for (int thisReading1 = 0; thisReading1 < numReadings1; thisReading1++)
readings1[thisReading1] = 0;
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++)
readings2[thisReading2] = 0;
for (int thisReading3 = 0; thisReading3 < numReadings3; thisReading3++)
readings3[thisReading3] = 0;
for (int thisReading4 = 0; thisReading4 < numReadings4; thisReading4++)
readings4[thisReading4] = 0;
}
void loop() {
//1 temperatur sensor 1
total1= total1 - readings1[index1];
readings1[index1] = analogRead(inputPin1);
total1= total1 + readings1[index1];
index1 = index1 + 1;
if (index1 >= numReadings1)
index1 = 0;
average1 = total1 / numReadings1;
temp1 = (average1 * 5.0 / 1024.0) - 0.5;
temp1 = temp1 / 0.01;
delay(1); // delay in between reads for stability
//2 temperatur sensor 2
total2= total2 - readings2[index2];
readings2[index2] = analogRead(inputPin2);
total2= total2 + readings2[index2];
index2 = index2 + 1;
if (index2 >= numReadings2)
index2 = 0;
average2 = total2 / numReadings2;
temp2 = (average2 * 5.0 / 1024.0) - 0.5;
temp2 = temp2 / 0.01;
delay(1);
//3 temperatur sensor 3
total3= total3 - readings3[index3];
readings3[index3] = analogRead(inputPin3);
total3= total3 + readings3[index3];
index3 = index3 + 1;
if (index3 >= numReadings3)
index3 = 0;
average3 = total3 / numReadings3;
temp3 = (average3 * 5.0 / 1024.0) - 0.5;
temp3 = temp3 / 0.01;
delay(1);
//4 temperatur sensor 4
total4= total4 - readings4[index4];
readings4[index4] = analogRead(inputPin4);
total4= total4 + readings4[index4];
index4 = index4 + 1;
if (index4 >= numReadings4)
index4 = 0;
average4 = total4 / numReadings4;
temp4 = (average4 * 5.0 / 1024.0) - 0.5;
temp4 = temp4 / 0.01;
delay(1);
// read the analog in value: For light control.
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
lightLCD = map(outputValue, 0, 255, 0, 100);
analogWrite(analogOutPin, outputValue);
lcd.setCursor(0, 0);
lcd.print("INDE ");
lcd.print(temp1);
lcd.setCursor(9, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("UDE ");
lcd.print(temp2);
lcd.setCursor(9, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("KOLE ");
lcd.print(temp3);
lcd.setCursor(9, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("FAN ");
lcd.print(temp4);
lcd.setCursor(9, 3);
lcd.print(" ");
// Fan control according to temperature sensor 1
if (temp1 < tempMin) {
fanSpeed = 0;
digitalWrite(FAN, LOW);
}
if((temp1 >= tempMin) && (temp1 <= tempMax)) {
fanSpeed = map(temp1, tempMin, tempMax, 32, 255);
fanLCD = map(temp1, tempMin, tempMax, 0, 100);
analogWrite(FAN, fanSpeed);
}
// Warning light for temperature sensor 1
if(temp1 > tempMax) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
lcd.setCursor(11, 0);
lcd.print("FAN SPEED");
lcd.setCursor(11, 1);
lcd.print(fanLCD);
lcd.print("%");
lcd.setCursor(11, 2);
lcd.print("LYS");
lcd.setCursor(11, 3);
lcd.print(lightLCD);
lcd.print("%");
}