You can try this:
Code:
#include "DHT.h"
#define output1 4
#define input1 36
int Temperature = 0;
int Humidity = 0;
#define DHTPIN 13 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long SwitchReadloop = millis();
long SwitchReadloopint = 20; //you can change the time duration as you need loop time (in millisec)
unsigned long Temploop = millis();
long Temploopint = 2000; //you can change the time duration as you need loop time (in millisec)
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(output1,OUTPUT);
pinMode(input1,INPUT);
pinMode(DHTPIN, INPUT);
}
void loop() {
unsigned long CurrentTime = millis();
if(CurrentTime - SwitchReadloop > SwitchReadloopint){
SwitchReadloop = CurrentTime;
if (digitalRead(input1)==LOW && digitalRead(output1)==LOW)
{
digitalWrite(output1,HIGH);
}
else if(digitalRead(input1)==HIGH && digitalRead(output1)==HIGH)
{
digitalWrite(output1,LOW);
}
}
/*********************humidity***************************/
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very s0 sensor)
if(CurrentTime - Temploop > Temploopint){
Temploop = CurrentTime;
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
else{
Humidity = h;
Temperature = t; // data in *Celcius
}
Serial.print("Humidity: ");
Serial.println(Humidity);
Serial.print("Temperature: ");
Serial.println(Temperature);
}
}// end loop()
(03-13-2023, 11:19 PM)Joseph.o Wrote: i'm using a dht22 and input 1 and output1.
i made a simple code where i'm showing the dht22 value in the serial monitoring and when the intput1 is colsed i made the output1 in the ON state, and if the input1 is open the ouptput1 is OFF.
in the simulation everting work fine and the value of the dht22 sonsor is correct.
but when i add an lamp in the output1 and turn it on and off the value of the dht22 becomes null.
i tried to modify the code like add a delay... but no result. dht22 becomes null.
here is my simple source code:
Code:#define output1 4
#define input1 36
#include "DHT.h"
#define DHTPIN 13 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int temperatureSonde=0;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(output1,OUTPUT);
pinMode(input1,INPUT);
}
int loading=0;
void loop() {
if (digitalRead(input1)==0 && digitalRead(output1)==0){digitalWrite(output1,1);
//delay(100);
}
else if(digitalRead(input1)==1 && digitalRead(output1)==1){digitalWrite(output1,0);
//delay(100);
}
/*********************humidity***************************/
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very s0 sensor)
float h = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(h) ) {
Serial.println("Failed to read from DHT sensor!");
}
Serial.print(F("Humidity: "));
Serial.println(int(dht.readHumidity()));
/********************************************************/
delay(100);
}// end loop()