i am new in programming and i am stuck with a project.
i want to control a relay and it is depending on a temperature,
so i use the 2 ds18b20 digital temperature sensors.
if one of the temperature sensors rise above the other the arduino must set the relay on.
the problem is that i dont know how to get a variable from the code i get from the arduino website:
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int temp1 = sensors.getTempCByIndex(0);
int temp2 = sensors.getTempCByIndex(1);
int relaypin = 3;
void setup(void)
{
Serial.begin(9600); //Begin serial communication
Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
sensors.begin();
pinMode (relaypin, OUTPUT);
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
Serial.print("Temperature1 is: ");
Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print("Temperature2 is: ");
Serial.println(sensors.getTempCByIndex(1));
//Update value every 1 sec.
if (temp1 > temp2)
{digitalWrite (relaypin, HIGH); }
else { digitalWrite (relaypin, LOW);}
delay(1000);
}
int temp1 = sensors.getTempCByIndex(0);
int temp2 = sensors.getTempCByIndex(1);
This will NOT properly initialize these variables.
if (temp1 > temp2)
You have not initialized these variables. You need to assign values to these variables, in loop(), AND print the values in the variables, NOT call the function for the 3rd and 4th time.