value question in a loop

New and first time posting so go easy!
This is a typical tmp36 program for the arduino starter kit, which I have modifying and trying to learn a couple of things along the way.
My question is this: in the loop I want to grab the value of tempF the first time thru so I can use this as the base line temperature. At first I made it equal to another variable, only to watch it change every time the tempF value changes. How can I hold it constant for the first time if reads the sensor?

Thanks


//TMP36
int sensorPin = A0;

void setup()
{
Serial.begin(9600);

pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);

}
void loop()
{

int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
Serial.print(voltage); Serial.println(" volts");
float tempC = (voltage - 0.5) * 100 ;
Serial.print(tempC); Serial.println(" degrees C");
float tempF = (tempC * 9.0 / 5.0) + 32.0;
Serial.print(tempF); Serial.println(" degrees F");
delay(1000);

if (tempF<=72){
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else if(tempF >= 73+2 && tempF < 73+4){
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else if (tempF >= 73+4 && tempF < 73+6){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
else if(tempF > 73+6){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
delay(1);

}


wrap it in a function, and set a base value in setup()

did some refactor in the if then else part, to prevent unneeded tests

//TMP36
int sensorPin = A0;

float baseTemperature = 0;

void setup()
{
  Serial.begin(9600);

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);

  baseTemperature  = readTemperatureC(sensorPin);
}


void loop()
{
  delay(1000);
  float tempC = readTemperatureC(sensorPin);

  Serial.print(tempC); 
  Serial.println(" degrees C");
  float tempF = (tempC * 9.0 / 5.0) + 32.0;
  Serial.print(tempF); 
  Serial.println(" degrees F");

  if (tempF <= baseTemperature  )
  {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }
  else if ( tempF < baseTemperature  + 4)
  {
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }   
  else if (tempF < baseTemperature  + 6)
  {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
  }
  else 
  {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
  }
}


float readTemperatureC(int pin)
{
  int reading = analogRead(pin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  // Serial.print(voltage); Serial.println(" volts");
  return (voltage - 0.5) * 100 ; 
}

You could read the value in your setup function. That way it only gets done once - that's what the setup is for, reading baselines and stuff. Or, you could do it this way in the main loop - but this hurts performance because you're doing a branch where you don't need to.

bool baseTempRead;
if (!baseTempRead) {
   ReadBaseTemp(); //store the value using the function
  baseTempRead = true;
}

that's great, I will paly around with this and use this same logic for other applications.

THANKS for the totally quick response.

Now I know I did the right think by buying the real Arduino starter kit instead of sparkfun kit!!!!!

casharp1:
Now I know I did the right think by buying the real Arduino starter kit instead of sparkfun kit!!!!!

Are you talking about this?

It's not really the same thing as this:

But, you can learn the same things, the programming language is the same, the hardware is solid.

Yes, I read reviews before I bought the Arduino and there were people who questions the Open source between the the two and the fact that dollars might be better off with Arduino to help fund the community.

Anyway

I do have a question on the code now that I looked it over in detail.

float readTemperatureC(int pin)
{
int reading = analogRead(pin);
float voltage = reading * 5.0;
voltage /= 1024.0;
// Serial.print(voltage); Serial.println(" volts");
return (voltage - 0.5) * 100 ;
}

  1. The function on the bottom, has the Variable "pin" does this need to be declared in the top? or should it be sensorPin?

casharp1:

  1. The function on the bottom, has the Variable "pin" does this need to be declared in the top? or should it be sensorPin?

When you define a function with a parameter like that, the variable is "declared" in the function signature, and the scope is "local" to the function itself. When you call the function, you must provide a value for that parameter. Naming is up to you, but I'm a fan of long descriptive names using CamelCase - but that's because I was mostly brought up in the Microsoft world and that's their naming standard.

The different places where you can declare a variable can affect the scope of the variable - scope is "where is the variable name valid" and if you're careful about scope, you can re-use names. If you declare a variable "at the top" (or really, anywhere outside of a function) - then the scope of that variable is "global" meaning it's accessible to every line of code in the whole program.

If you declare a variable inside a function or as part of the function declaration, the scope is "local" and only accessible within that function. A variable can also be local to a block of code. Examples:

int x = 23; //GLOBAL - accessible anywhere

void setup() {
  x = 0;
  int SetupX = 25;
}

void loop() {
  int Y = 2; //local to the "loop" function only
  x = 45; // can be set here because it's global.
  SetupX = 45; //ERROR - does not exist in this scope
}

void myFunction(int Y) {
  if (Y != 0) {  //LOCAL Y, not the same as the one inside "loop()" function!
    int k = 1;
  }
  k = 2; //ERROR - k is local to the "IF" block
}

O BTW, here's the documentation about "scope" of variables.

http://arduino.cc/en/Reference/scope

And yes, I agree, Arduino is a worthwhile place to spend your money. You should know however, that Sparkfun also supports the community, and buying from them supports the larger "Maker" community, and a really great company with really great people, who are local to me, so I buy from them quite often. Just letting you know, you're still supporting the community if you buy from Sparkfun.

Wanted to add a small correction to Robtillaart's post:

The "if statements" which are used to turn the LEDs on or off incorrectly compare the baseline temperature (which is measured in Celsius) to the changing temperature converted to Fahrenheit.

This results in all 3 of your LEDs turning on from the moment the sketch is uploaded.

The simple correction is to change tempF to tempC for each if condition.....

*BTW, thanks for this thread....was looking all over the internet on how to do this very thing (capture and store ambient temp.)