Go Down

Topic: Project 3 Starter Kit Code Issue  (Read 1 time) previous topic - next topic

Vaibek

Nov 01, 2014, 09:32 pm Last Edit: Nov 01, 2014, 09:57 pm by Vaibek
//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.
Code: [Select]

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.

Code: [Select]


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);
}


blfuentes

Could you post a photo of your wiring? Maybe you have the GND and 5V switched.

Marecdl

Try to check the real value your analog input gives? Another possibility you can use, is to put some of your loops in a "comment" format, just put the /* in those lines.
I also use that for testing, when my projects don't do what I want.

Maybe, since the first reading is performed when the programm is started, you might make a little delay before measuring, so the component you use to make the measurement has time to "start up".



Go Up