ciao
vi posto il codice...come da titolo appena avvio si imapasta su tutto e non va piu niente...dalla seriale non butta fuori niente...vai a capire perche...
se ce qualcuno che mi puo aiutare
sembra che il problema sia su ReadDHT(perche quando lo tolgo funziona tutto), quindi penso faccia casino quello.
dimenticavo, uso un arduino mega 2560, ssensori e display sono tutti dfrobot
#include <Wire.h>
#include <RTClib.h>
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
RTC_DS1307 RTC;
#define dht_dpin 18 //dhc11 here. Set equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on digital line 14, aka analog 0
//Other digital lines can be used, e.g. D2
byte bGlobalErr; //dhc11 for passing error code back from complex functions.
byte dht_dat[4]; //dhc11 Array to hold the bytes sent from sensor.
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
InitDHT();//Does what's necessary to prepare for reading DHT
lcd.init();
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!)
lcd.clear();
//lcd.printIn("KEYPAD testing... pressing");
delay(1000);
}
void loop () {
ReadDHT();//This is the "heart" of the program.
//Fills global array dht_dpin[], and bGlobalErr, which
//will hold zero if ReadDHT went okay.
//Must call InitDHT once (in "setup()" is usual) before
//calling ReadDHT.
switch (bGlobalErr){
case 0:
Serial.print("Current humdity = ");
Serial.print(dht_dat[0], DEC);
Serial.print(".");
Serial.print(dht_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht_dat[2], DEC);
Serial.print(".");
Serial.print(dht_dat[3], DEC);
Serial.println("C ");
break;
case 1:
Serial.println("Error 1: DHT start condition 1 not met.");
break;
case 2:
Serial.println("Error 2: DHT start condition 2 not met.");
break;
case 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code encountered.");
break;
}//end "switch"
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
int year=now.year();
int month=now.month();
int day=now.day();
char yearc[5]= " ";
char monthc[3]= " ";
char dayc[3]= " ";
itoa(year, yearc , 10);
itoa(month, monthc , 10);
itoa(day, dayc , 10);
lcd.cursorTo(1, 0); //line=2, x=0
lcd.printIn(yearc);
lcd.printIn("/");
lcd.cursorTo(1, 5); //line=2, x=0
lcd.printIn(monthc);
lcd.printIn("/");
lcd.cursorTo(1, 8); //line=2, x=0
lcd.printIn(dayc);
Serial.println();
delay(1000);
}
void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}//end InitDHT
void ReadDHT(){
/*Uses global variables dht_dat[0-4], and bGlobalErr to pass
"answer" back. bGlobalErr=0 if read went okay.
Depends on global dht_dpin for where to look for sensor.*/
bGlobalErr=0;
byte dht_in;
byte i;
// Send "start read and report" command to sensor....
// First: pull-down I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
delay(23);
/*aosong.com datasheet for DHT22 says pin should be low at least
500us. I infer it can be low longer without any]
penalty apart from making "read sensor" process take
longer. */
//Next line: Brings line high again,
// second step in giving "start read..." command
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. Acknowledges "start read
//and report" command.
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);
delayMicroseconds(40);
dht_in=digitalRead(dht_dpin);
if(dht_in){
bGlobalErr=1;//dht start condition 1 not met
return;
}//end "if..."
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
bGlobalErr=2;//dht start condition 2 not met
return;
}//end "if..."
/*After 80us low, the line should be taken high for 80us by the
sensor. The low following that high is the start of the first
bit of the forty to come. The routine "read_dht_dat()"
expects to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data reception... pick up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: restore pin to output duties
pinMode(dht_dpin,OUTPUT);
//Next: Make data line high again, as output from Arduino
digitalWrite(dht_dpin,HIGH);
//Next see if data received consistent with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*Condition in following "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}//DHT checksum error
};//end ReadDHT()
byte read_dht_dat(){
//Collect 8 bits from datastream, return them interpreted
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code expects the system to have recently entered the
//dataline low condition at the start of every data bit's
//transmission BEFORE this function is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);
//signalling end of start of bit's transmission.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is being sent, respectively.
delayMicroseconds(30);//AFTER pin is high, wait further period, to be
//into the part of the timing diagram where a 0 or a 1 denotes
//the datum being send. The "further period" was 30uS in the software
//that this has been created from. I believe that a higher number
//(45?) might be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));// "add" (not just addition) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the START
//of the NEXT bit's transmission.
while (digitalRead(dht_dpin)==HIGH);
}//end of "for.."
return result;
}//end of "read_dht_dat()"
cmq BUON NATALE!