Hola Estoy haciendo un programa que lea dos sensores dht 11, 2 sensores lm35 y 1 fotorresistencia aqui todo va bien pero cuando meto el reloj ds3231 el primer sensor dht11 me da valores 0. y read failed.
Me he bloqueado si me echais una mano por favor. Muchas Gracias
Arduino Leonardo
//-----------------------------------------------Con reloj-----------------------------------
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
// Lectura del sensor DHT11 http://zygzax.com
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 2 //Sensor interior va al pin DIGITAL 2; indica aquí el que tú uses
#define DHT11DOS 4 //Sensor exterior va al pin DIGITAL 8; indica aquí el que tú uses
//Personalizable; Salida digital para el relé , uso la 10
int errorPin=5; //Personalizable; Para activar el LED de error, yo uso el 11 digital
int onPin=12; //Personalizable; Para indicar "en marcha"; pin 12 digital
int tempint; //Variables para guardar los valores y poder comparar
int tempext;
int humint;
int humext;
boolean data_error; //Guardamos el error en esta variable
// .................................................
float temp, temp_med = 0;
float temp1, temp1_med = 0;
float acum = 0, Vout; // variables secundarias
float res = 0.010; // en V/ºC
float Offset = 0.0;
float temp_max, temp_min;
// Muestras.............................
int i,num_muestras=300;
int luzValue = 0;
int humeValue = 0;
int luz;
int humedad;
int regarPin = 11;
int ventiladorPin=6;
int ventilador1Pin=7;
int sensorHumedad = A3;
int sensorLuz = A2;
int tempPin= A0;
int temp1Pin = A1;
int regar;
int ventilar;
int ventilar1;
void setup() {
Wire.begin(); // Inicia el puerto I2C
RTC.begin(); // Inicia la comunicación con el RTC
//RTC.adjust(DateTime(__DATE__, __TIME__)); // Establece la fecha y hora (Comentar una vez establecida la hora)
pinMode(regarPin, OUTPUT);
pinMode(ventiladorPin, OUTPUT);
pinMode(ventilador1Pin, OUTPUT);
pinMode(errorPin, OUTPUT);
pinMode(onPin, OUTPUT);
delay(2000); //Esperamos 2 seg antes de leer los sensores
Serial.begin(9600);
}
void loop() {
data_error=false; //reseteamos el byte de comprobación
int chk = DHT11.read(DHT11PIN);
switch (chk){
case DHTLIB_OK:
//Si es OK, cogemos datos
humint=DHT11.humidity;
tempint=DHT11.temperature;
break;
//case DHTLIB_ERROR_CHECKSUM: //Desactivamos estas opciones, no nos interesan en la práctica en este caso
// display.println("Checksum error");
//display.display();
//break;
//case DHTLIB_ERROR_TIMEOUT:
//display.println("Time out error");
//display.display();
//break;
default: //en caso de cualquier otro resultado (default); error desconocido
data_error=true; //activamos el LED de error
break;
}
chk = DHT11.read(DHT11DOS); //comprobamos sensor externo
chk=DHTLIB_OK;
switch (chk){ //Comprobamos que los datos son válidos
case DHTLIB_OK:
//Si es OK cogemos los datos del externo
humext=DHT11.humidity;
tempext=DHT11.temperature;
break;
default:
//Si no es OK, activamos el LED de error
data_error=true;
break;
}
// Sensores Lm35
acum = 0;
for (i = 0; i < num_muestras; i++) {
Vout = analogRead(tempPin) * 0.00488758553275; // step * V/step = V
temp = ( (Vout - Offset) / res); // V/(V/ºC) = ºC
acum += temp;
temp_med = acum / num_muestras;
delay(10);
}//LM35D
acum = 0;
for (i = 0; i < num_muestras; i++) {
Vout = analogRead(temp1Pin) * 0.00488758553275; // step * V/step = V
temp1 = ( (Vout - Offset) / res); // V/(V/ºC) = ºC
acum += temp1;
temp1_med = acum / num_muestras;
delay(10);
}luzValue = analogRead(sensorLuz);
humeValue = analogRead(sensorHumedad);
// ----------------------------------------------------
// Imprimir valores
// ----------------------------------------------------
luz = (100.0 * luzValue) / 1024;
humedad = (100.0 * humeValue) / 1024;
DateTime now = RTC.now(); // Obtiene la fecha y hora del RTC
// Imprimimos los datos actuales.
Serial.print("DATA, TIME,");// hora en excel 1ª fila
Serial.print(temp_med);
Serial.print(",");
Serial.print(temp1_med);
Serial.print(",");
Serial.print(tempint+1);
Serial.print(",");
Serial.print(tempext);
Serial.print(",");
Serial.print(humint);
Serial.print(",");
Serial.print(humext);
Serial.print(",");
Serial.print(luz);
Serial.print(",");
Serial.print(humedad);
Serial.print(",");
Serial.print(regar);
Serial.print(",");
Serial.print(ventilar);
Serial.print(",");
Serial.print(ventilar1);
//RELOJ
Serial.print(now.year(), DEC); // Año
Serial.print('/');
Serial.print(now.month(), DEC); // Mes
Serial.print('/');
Serial.print(now.day(), DEC); // Dia
Serial.print(' ');
Serial.print(now.hour(), DEC); // Horas
Serial.print(':');
Serial.print(now.minute(), DEC); // Minutos
Serial.print(':');
Serial.print(now.second(), DEC); // Segundos
Serial.println();
if (data_error==true) //al menos uno de los sensores no funciona; mensaje de error y apagado de ventilador y reseteo contadores (como si se reseteara el programa)
{
digitalWrite(errorPin, HIGH);
}
else
{
digitalWrite(errorPin, LOW);
}
// ----------------------------------------------------
// Chequeo si debo regar
// ----------------------------------------------------
if( humedad <= 50 && luz < 70 && tempint < 30) {
digitalWrite(regarPin, HIGH);
regar=1;
}else{
digitalWrite(regarPin, LOW);
regar=0;
}// ----------------------------------------------------
// Chequeo ventilador 0
// ----------------------------------------------------
if( humint >= 53 ) {
digitalWrite(ventiladorPin, HIGH);
ventilar=1;
}else{
digitalWrite(ventiladorPin, LOW);
ventilar=0;
}//------------------------------------------------------
// Chequeo ventilador 1
//---------------------------------------------------------
if( temp >= 35 ) {
digitalWrite(ventilador1Pin, HIGH);
ventilar1=1;
}else{
digitalWrite(ventilador1Pin, LOW);
ventilar1=0;
}
delay(50);
}
moderator: reformatted code using notepad++ and textFX