My code doesn't work well. I need to visualize data dht11 all the time and when I push button turn on water pump. But it doesn't work well. Are wrong my code?
[code]
#include <DHT11.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTTYPE DHT11
int boton = 11;
int led = 13;
int pump_on = 7;
boolean estadoAnterior = LOW;
boolean estadoActual = LOW;
boolean ledOn = false;
int cont=0;
const int DHTPin = 5; // what digital pin we're connected to
DHT dht(DHTPin, DHTTYPE);
void setup()
{
pinMode(boton, INPUT);
pinMode(led, OUTPUT);
pinMode(pump_on, OUTPUT);
Serial.begin(9600);
dht.begin();
readSensor();
}
void loop()
{
readCmd();
readSensor();
}
void readSensor(void)
{
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.println();
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
}
void readCmd(void)
{
estadoActual = rebote(estadoAnterior);
Serial.print(estadoActual);
if (estadoAnterior == LOW && estadoActual == HIGH)
{
ledOn = !ledOn;
cont++;
Serial.println(cont);
}
estadoAnterior = estadoActual;
digitalWrite(led, ledOn);
digitalWrite(pump_on, !ledOn);
}
boolean rebote(boolean eAnterior)
{
boolean eActual = digitalRead(boton);
if (eAnterior != eActual)
{
delay(5);
eActual = digitalRead(boton);
}
return eActual;
}
[/code]