Hi to all!
I’m a newbie and i have a problem with my little project… i try to switch on a relay by an if condition!
I think that i have connected my relay shield correctly because i’ve tested the Blink example to try the switching power on and off.
This little program read temperature with a dallas sensor and if the temperature is high than 40°C the switch must be off otherwise is must be on.
The problem is that my pin 5 never go in HIGH condition…the if condition i think that works well because if the temperature is under 40°C my serial monitor print “a” (so the procedure is well)
I’ve also noticed a strange thing: if i join the else conditon (switch off the power) the shield work like blink example so i’ve deduced that my shield works well an the if condition is good too!
Why the state HIGH and LOW doesn’t work if they are separated?? any idea?
Thanks to all
i post the code
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
pinMode(5, OUTPUT);
// start serial port
Serial.begin(9600);
sensors.begin();
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temperature for the device 1 is: ");
Serial.println(sensors.getTempCByIndex(0));
//my if condition
if(sensors.getTempCByIndex(0)<=40)
{
digitalWrite(5, HIGH);
Serial.println(“a”);
}
else
{
digitalWrite(5, LOW);
Serial.println("b ");
}
}