Hi everyone. i was trying to create a temprature alarm system using DS18B20 sensor. the problem im having is i can read temprature value in serial monito with the code which ill mention further, but i want to convert that value to an integer so i can take some actions like alarm or turning on led on certain temprature. i dont know how to convert the "Serial.print(sensors.getTempCByIndex(0));" to a value so i would have seprate temprature integer with which i can assign funtion to it. for example
" if (Temprature > 40) {
digitalWrite(Led5, HIGH);
}
else{
digitalWrite(Led5,LOW)}
here is the code: #include <OneWire.h> #include <DallasTemperature.h>
// Data wire is plugged into digital pin 2 on the Arduino #define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup(void)
{
sensors.begin(); // Start up the library
Serial.begin(9600);
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
//print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
//print the temperature in Fahrenheit
Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
Serial.print((char)176);//shows degrees character
Serial.println("F");
Since you are currently just printing them, you just have to change the line to this:
Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0,0);
But what you need to do is capture the sensor value into a variable. You can declare it as global in the beginning of your sketch if you want to do something with it the next time through or local. If only local, you would change it to this:
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
//print the temperature in Celsius
int temp = sensors.getTempCByIndex(0).toInt();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
//print the temperature in Fahrenheit
Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
Serial.print((char)176);//shows degrees character
Serial.println("F");
if(temp....WHATEVER YOU WANT HERE)
delay(500);
}
But you don't have to get it to an integer to compare it to 40. You could leave it in it's native "float" type and not worry about converting to an integer.
thankyou guys for the help. one more question, is that possible if i can add two more temprature sensors to this program, basically im working on a power supply and i have to measure main MOSFET temprature and two SHOTTKEY diodes temprature.
sorry i didnt get it. how would arduino know which sensor reading what data and how is it assigned to led and eventually turning the fan on.
so basically whats happening is im going to assign 3 led to 1 mosfet and two diodes respectively. so i would know which thing is running hotter. which will eventually turn the fan on