This is my first programming from scratch and I cant seem to get the output to work, i have tried various things but I'm stumped. the goal is to have the relay (eventually will be 12v fans in my car) to switch on when the temperature is over 35 and off when the temperature drops below 25, the values in here are different as its easier to test.
Arduino Nano, 5V relay board and DHT11
Does anyone have any idea what I may have done wrong?
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("DHTxx test!");
pinMode(2,OUTPUT);
digitalWrite(RELAY,LOW);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
{
if (t>=35);{
digitalWrite(RELAY,HIGH);
if (t<=26);
digitalWrite(RELAY,HIGH);
int relayState = digitalRead(RELAY);
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.println(" *F\t");
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
If the code exceeds the 9000 character inline limit then attach it to a post
I've fixed that up and I still cant seem to get it to operate.
currently the DHT is operating correctly however i cant seem to get the output "D2" to operate anything, I've tried different combinations of output devices but none seem to work, I have a feeling it may have something to do with the "t" reference but I cant seem to figure it out.
Connect "2.2k-LED" circuit with DPin-2. Upload the following sketch (your one with little adjustment) and check that LED becomes ON when t >= 35.0 degF and goes OFF when t <=26.0 degF.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4
#define DHTTYPE DHT11
//const float baselineTemp = 26;
DHT dht(DHTPIN, DHTTYPE);
const int RELAY = 2;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("DHTxx test!");
pinMode(2, OUTPUT);
digitalWrite(RELAY, LOW);
dht.begin();
}
void loop()
{
// put your main code here, to run repeatedly:
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
if (t >= 35.0)//;
{
digitalWrite(RELAY, HIGH);
}
if (t <= 26.0)
{
digitalWrite(RELAY, HIGH);
//int relayState = digitalRead(RELAY);
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.println(" *F\t");
}
UKHeliBob:
Are you trying to point out that the code is wrong or suggesting that it is correct ?
to point that the code is wrong :
a. there is nowhere in sketch "digitalWrite(RELAY, LOW)", except in setup.
b. the if condition for <=26 will never be checked
GRuser:
to point that the code is wrong :
a. there is nowhere in sketch "digitalWrite(RELAY, LOW)", except in setup.
b. the if condition for <=26 will never be checked
I know that the code is wrong, but I am not sure if you realised the significance of the semicolons immediately after the if statements and the effect that they have on which code sections are dependant on the tests and which are executed unconditionally
UKHeliBob:
I know that the code is wrong, but I am not sure if you realised the significance of the semicolons immediately after the if statements and the effect that they have on which code sections are dependant on the tests and which are executed unconditionally
Of course you know it . But since the OP said "he fixed that" (after your #1) I thought he changed only the semicolon.
But the missing "digitalWrite(RELAY, LOW)" ia something that has not been discussed.
Anyway, The lack of posting full, revised code is the major factor for mis-leads.