Hello, I am new to arduino and c+ but was hoping someone could guide me how to make my uno have a high output once a certain temperature is reached with the tmp102 sensor. Ive got a couple books from sparksfun on the way but til then Im on my own. This program came from sparkfun and would like to make a relay trigger when the temp goes above, say 80. Any help would be greatly appreciated.
#include <Wire.h>
int tmp102Address = 0x48;
void setup(){
Serial.begin(9600);
Wire.begin();
}
void loop(){
float celsius = getTemperature();
Serial.print("Celsius: ");
Serial.println(celsius);
float fahrenheit = (1.8 * celsius) + 32;
Serial.print("Fahrenheit: ");
Serial.println(fahrenheit);
delay(200); //just here to slow down the output. You can remove this
}
float getTemperature(){
Wire.requestFrom(tmp102Address,2);
byte MSB = Wire.read();
byte LSB = Wire.read();
//it's a 12bit int, using two's compliment for negative
int TemperatureSum = ((MSB << 8) | LSB) >> 4;
float celsius = TemperatureSum*0.0625;
return celsius;
}
[code]
Welcome. Please read the "how to post to this forum". Specifically, the section about posting code inside code tags, then go back and edit your original post so that the code is inside code tags.
Thank you for responding. Obviously I need to learn c+ because Im not sure where the if goes. I had something of that nature on a code earlier but it didnt work correct. I assumed I didnt identify the variable correctly. But I did have a if and a else statement.
fahrenheit > 80
digitalWrite (pin#, HIGH)
I apologize again for not knowing anything, just bored and wanted to get my new arduino board doing something.
The "return" keyword returns from a subroutine. No more lines of the subroutine will be executed.
Also, logically, you really should put your "if" in the main loop, instead of in a function called "getTemperature()".
Get the value using getTemperature, and when it returns, do the "if" using the value that it returned. Try to keep subroutines only doing the functions that the name of the subroutines say they will do.
you don't really need the "else if" as you compared the same number. else {digitalwrite.........} would work
Nothing wrong with doing what you did because In the future you will probably have to change the "else if" to a different number to stop the relay being turned on and off as the temp hovers around the set point.
you will also need to check your compares ( < ) as your relay is going to be active when the temp is lower than 30