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?
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;
}
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 ;
}
The function on the bottom, has the Variable "pin" does this need to be declared in the top? or should it be sensorPin?
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
}
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.)