//The below code is supposed to establish a base line temperature every time the program is started.
//This is where my problem is. when you print out the baseTmp variable, it read -50.00. This causes the
// very last "else if" statement to read as true.
const int tmpPin = A0; //establish variable tmpPin to = A0 pin.
const int baseSensorVal = analogRead(tmpPin); //reads value from variable tmpPin and stores it in baseSensorVal.
const float baseVoltage = (baseSensorVal/1024.0) *5.0; //calculates baseVoltage.
const float baseTmp = (baseVoltage - 0.5) * 100; // calculates base temperature.
Below is the program in full.
const int tmpPin = A0; //establish variable tmpPin to = A0 pin.
const int baseSensorVal = analogRead(tmpPin); //reads value from variable tmpPin and stores it in baseSensorVal.
const float baseVoltage = (baseSensorVal/1024.0) *5.0; //calculates baseVoltage.
const float baseTmp = (baseVoltage - 0.5) * 100; // calculates base temperature.
void setup(){
Serial.begin(9600); //open a serial port
for (int pinNumber = 2; pinNumber < 5; ++pinNumber){ //Just turns on and off the LED's as to appear to load.
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, HIGH);
delay(250);
digitalWrite(pinNumber, LOW);
}
}
void loop(){
int sensorval = analogRead(tmpPin);
Serial.print("Sensor Value: ");
Serial.print(sensorval);
float voltage = (sensorval/1024.0) *5.0;
Serial.print(" , VOLTS: ");
Serial.print(voltage);
float TempCelsius = (voltage - 0.5) * 100;
Serial.print(" , Temperature C: ");
Serial.print(TempCelsius);
float TempFarhenheit = (TempCelsius * 1.8 ) + 32;
Serial.print(" , Temperature F: ");
Serial.print(TempFarhenheit);
Serial.println();
Serial.print(baseTmp);
if (TempCelsius < baseTmp){
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
}
else if (TempCelsius > baseTmp + 2 && TempCelsius < baseTmp +4){
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
}
else if (TempCelsius > baseTmp +4 && TempCelsius < baseTmp +6){
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
}
else if (TempCelsius > baseTmp +6){
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
}
delay(1000);
}