arduino rileva umidità e temperatura 5 minuti poi non più

non riesco a metter il codice lo divido in due:

//Lettura temperatura e umidità
//Paolo prova 6 del 16-02-2013
//This is a re-written DHT11
//DHT stuff in subroutines.
//N.B. "bit" is used in the narrow, computer "1 or 0"
//   sense throughout.
//"DHT" from sensor's names: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.
//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to generally apply to DHT11 as well, except in the
//case of the DHT11, I believe the second and fourth bytes are
//always zero.
//***N.B.****
//The code WORKS... the comments may not yet be EXACTLY right.
//See the web-page cited above for latest news.
//This code works with a DHT11 humidity/ temperature sensing module
//from nuelectronics.com, complied with ver 0018 of the Arduino environment
//Sensor attached to P4 (nuelectonics shield)/ analog 0, aka digital 14.
//That "module", according to the
//nuelectronics site, and visual inspection simply provides for easy
//connection of an Aosong DHT11 unit to the nuelectronics datalogging
//shield. Only 3 wires are involved: Vcc, ground, and a single data
//line. One of the DHT11's 4 pins goes nowhere.
//You should not need to change anything except the next line to use
//the software with the sensor on a different line, or for a DHT22.
//Just "huffing" on the sensor from deeply filled lungs should show
//a near instant rise in humidity
//#define dht_PIN 0      //no ; here. deprecate ADC0...
//even though we are using it as a digital pin.
//Other parts of code restrict us to using
//ADC0-5, aka D14-19
#define dht_dpin 14 //no ; here. Set equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on digital line 14, aka analog 0
//#define LIGHT_SENSOR_PIN 1
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
byte bGlobalErr; //for passing error code back from complex functions.
byte dht_dat[4]; //Array to hold the bytes sent from sensor.
//int light_intensity = 0;
//unsigned int flip = 0;
void setup(){
        //Blink LED to detect hangs
        pinMode(13, OUTPUT);
        // set up the LCD's number of columns and rows:
        lcd.begin(16, 2);              //Setta dysplay 16 su 2 linee
        lcd.print("Realizzato Paolo");  // Scrive su prima riga posizione 0
        lcd.setCursor(0, 1);            //Setta la sriga sotto x la scrittura
        lcd.print("Arduino UNO Test");  //Scrive sulla seconda riga
        delay(5000);                    //Ritardo prima di passare oltre
        lcd.setCursor(0, 0);            //Setta dove scrivere
        lcd.print("                ");  //Scrive tutti spazzi per pulire il display
        lcd.setCursor(0, 1);            //Scrive tutti spazzi per pulire il display
        lcd.print("                ");    //Scrive tutti spazzi per pulire il display
                InitDHT(); //Do what's necessary to prepare for reading DHT
        //Serial.begin(9600);
        delay(300); //Let system settle
        //Serial.println("Humidity and temperature\n\n");
        delay(700); //Wait rest of 1000ms recommended delay before
        //accessing sensor
} //end "setup()"
void loop(){
        // set the cursor to column 0, line 1
        // (note: line 1 is the second row, since counting begins with 0):
        //lcd.setCursor(0, 1);
        // print the number of seconds since reset:
        //lcd.print("100");
        //lcd.print(millis()/1000);
        //if ( flip & 1 )
        {
                digitalWrite(13, HIGH);
       // } else {
                digitalWrite(13, LOW);
        }

        //flip++;

       // light_intensity=analogRead(LIGHT_SENSOR_PIN);
        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.
        //Following: Display what was seen...
        switch (bGlobalErr) {
        case 0:
                lcd.setCursor(0, 0);
                // Serial.print("temperatura e umidita= ");
                lcd.print("Temp =    gradi");
                lcd.setCursor(7, 0);
                lcd.print( dht_dat[2], DEC);
                //Serial.print(dht_dat[0], DEC);
                //Serial.print(".");
                //Serial.print(dht_dat[1], DEC);
                //Serial.print("%  ");
                lcd.setCursor(0, 1);
                //Every 7 out of 15 times we show humidity, rest temp
               // if ((flip % 15) > 7 )
                {
                        lcd.print("Umidita' =   %");
                        lcd.setCursor(11, 1);
                        lcd.print( dht_dat[0], DEC);
                        
                //} else {
                        //lcd.print("Light =         ");
                        //lcd.setCursor(8, 1);
                     //   lcd.print( light_intensity, DEC);
                }
                //Serial.print("temperature = ");
                //Serial.print(dht_dat[2], DEC);
                //Serial.print(".");
                //Serial.print(dht_dat[3], DEC);
                //Serial.println("C  ");
                delay(10000);
                lcd.setCursor(0, 0);            //Setta dove scrivere
        lcd.print("                ");  //Scrive tutti spazzi per pulire il display
        lcd.setCursor(0, 1);            //Scrive tutti spazzi per pulire il display
        lcd.print("                ");    //Scrive tutti spazzi per pulire il display
        lcd.setCursor(0, 0);
        lcd.print("Umidita' =   %");
cd.setCursor(11, 0);
                        lcd.print( dht_dat[0], DEC);
                        lcd.setCursor(0, 1);
                            lcd.print("Temp =    gradi");
                lcd.setCursor(7, 1);
                lcd.print( dht_dat[2], DEC);
                delay(10000);
                lcd.setCursor(0, 0);            //Setta dove scrivere
        lcd.print("                ");  //Scrive tutti spazzi per pulire il display
        lcd.setCursor(0, 1);            //Scrive tutti spazzi per pulire il display
        lcd.print("                ");    //Scrive tutti spazzi per pulire il display
        lcd.setCursor(0, 0);
        lcd.print("Lettura Umidita'");  // Scrive su prima riga posizione 0
        lcd.setCursor(0, 1);            //Setta la sriga sotto x la scrittura
        lcd.print("& Temperatura ");  //Scrive sulla seconda riga
        delay(3000);
                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"
        lcd.setCursor(0, 0);            //Setta dove scrivere
        lcd.print("                ");  //Scrive tutti spazzi per pulire il display
        lcd.setCursor(0, 1);            //Scrive tutti spazzi per pulire il display
        lcd.print("                ");    //Scrive tutti spazzi per pulire il display
        
        delay(200); //Don't try to access too frequently... in theory
        //should be once per two seconds, fastest,
        //but seems to work after 0.8 second.

} // end loop()

/*Below here: Only "black box" elements which can just be plugged unchanged
   unchanged into programs. Provide InitDHT() and ReadDHT(), and a function
   one of them uses.*/

void InitDHT(){
        //DDRC |= _BV(dht_PIN);//set data pin... for now... as output
        //DDRC is data direction register for pins A0-5 are on
        //PORTC |= _BV(dht_PIN);//Set line high
        //PORTC relates to the pins A0-5 are on.
        //Alternative code...
//        if (dht_dpin-14 != dht_PIN){Serial.println("ERROR- dht_dpin must be 14 more than dht_PIN");};//end InitDHT
        pinMode(dht_dpin,OUTPUT); // replaces DDRC... as long as dht_dpin=14->19
        digitalWrite(dht_dpin,HIGH); //Replaces PORTC |= if dht_pin=14->19
} //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_PIN 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 18ms
        digitalWrite(dht_dpin,LOW); //Was: PORTC &= ~_BV(dht_PIN);
        delay(18);
        delayMicroseconds(600);//TKB, frm Quine at Arduino forum
/*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); //Was: PORTC |= _BV(dht_PIN);
        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); //Was: DDRC &= ~_BV(dht_PIN);
        delayMicroseconds(40);

        dht_in=digitalRead(dht_dpin); //Was: dht_in = PINC & _BV(dht_PIN);

        if(dht_in) {
                bGlobalErr=1; //Was: Serial.println("dht11 start condition 1 not met");
                return;
        } //end "if..."
        delayMicroseconds(80);

        dht_in=digitalRead(dht_dpin); //Was: dht_in = PINC & _BV(dht_PIN);

        if(!dht_in) {
                bGlobalErr=2; //Was: Serial.println("dht11 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(70);
//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); //Was: DDRC |= _BV(dht_PIN);
//N.B.: Using DDRC put restrictions on value of dht_pin

//Next: Make data line high again, as output from Arduino
        digitalWrite(dht_dpin,HIGH); //Was: PORTC |= _BV(dht_PIN);
//N.B.: Using PORTC put restrictions on value of dht_pin

//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; } //Was: Serial.println("DHT11 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) ;  //Was: while(!(PINC & _BV(dht_PIN)));
                //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?) would be more appropriate.

                //Next: Wait while pin still high
                if (digitalRead(dht_dpin)==HIGH) //Was: if(PINC & _BV(dht_PIN))
                        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) ;  //Was: while((PINC & _BV(dht_PIN)));
        } //end of "for.."
        return result;
} //end of "read_dht_dat()"

Il codice lo devi racchiudere tra i tag "code" e non "quote", vedrai che diventa anche molto più leggibile. La cosa che mi viene da dirti subito è che le specifiche di Arduino richiedono un'alimentazione minima di 7-7,5V, quindi 6V sono troppo pochi, sicuramente stai sotto-alimentando Arduino.

Prima di tutto ti conviene pubblicare il codice usando il comando Insert Code e non Insert Quote.

A parte questo, l'alimentatore a 6V è insufficiente: meglio un 9V.

Il dropout (differenza tra tensione in ingresso e quella in uscita) del IC stabilizzatore 5V è 1.2V, quindi ottieni un 4.8V, che, con temperature basse, forse potrebbe ridursi ulteriormente.

Tuttavia non credo sia questo il problema. Sei sicuro che il programma funzioni a temperatura ambiente per almeno 24 ore?

cyb, da quando in qua non leggi gli interventi che ti precedono? una volta eri un bravo ragazzo :stuck_out_tongue:

per Michele:

guarda che avevo scritto la cosa prima di te, ma non avevo letto il "warning..." e quindi sei apparso prima tu.

Cosa non si fa per avere qualche Karma in più, vero? XD

cyberhs:
per Michele:

guarda che avevo scritto la cosa prima di te, ma non avevo letto il "warning..." e quindi sei apparso prima tu.

Cosa non si fa per avere qualche Karma in più, vero? XD

ma ti brucia davvero così tanto questa cosa? posso dirti questo, ho notato che gli unici che fanno uso del termine siete tu e Testato, solo che lui annuncia felicemente di averlo dato a qualcuno, mentre tu ti intossichi perché altri lo ricevono. Io penso di averti detto 10 volte che non vado in cerca di nulla, men che meno di primati che perlatro non mi spettano, e sono qui per divertirmi, la vita mi ha dato e mi dà sacrosante soddisfazioni (quelle sì che sono meritate) nel mio mondo lavorativo. Sarà che la tua è una battuta, ma alla decima volta diventa un po' noiosa non ti pare? sul warning non dire sciocchezze, non sfugge, visto che ti esce dopo che tenti di inviare il post e per inviarlo devi dare nuova conferma, fai più bella figura a dire che ti secca cancellare ciò che un altro ha scritto prima di te. baci

Per Michele.

Mi dispiace che una mia battuta sia stata considerata da te un'offesa: ti prego di scusarmi, non era mia intenzione.

Qui siamo per divertirci e per aiutare gli amici in difficoltà (e in più si imparano un sacco di cose), figurati se mi interessa qualcosa dei Karma tuoi o miei! Era solo un "tormentone" che ti davo per farti sorridere, tutto qui.

ok, ok, ma da tormentone stava diventando tormento, tutto qui, quindi discorso chiuso, non c'è alcun bisogno di scuse :wink:

vedo che il traffico di karma di mike va a gonfie vele :stuck_out_tongue_closed_eyes: sei arrivato a quasi a 300 ti devo

ps il mio consiglio e di decommettare il serial print e tenerlo sotto debug 24 ore.

mad, la facilità con sui dispensi i tuoi kinder mi fa pensare che tu appartenga alla famiglia.... :grin:

Magari magari per quanti ne compro dovrebbero farmi socio a vita :grin: :grin: :grin:

eh eh eh Karma, Karma, Karma... ouuummmmm XD

@parapaolo: Prima di tutto ascolterei i suggeriementi che ti hanno datto sull' alimentazione e poi perchè non provi prima ad andare passo passo ?
Prova ad utilizzare una libreria per la famiglia DHT tipo questa Overview | DHT11, DHT22 and AM2302 Sensors | Adafruit Learning System e fare un debug con output su seriale... poi se la cosa fuznona bene per ore a tutte le temperature... :smiley: passi al display e così saprai quello che ha puo avere problemi...
Magari se ci dai anche maggiori informazioni su che tipo di display usi, forse è meglio :wink:

Intanto grazie per le risposte uso un LCD Display Module arduino 16x2 HD44780.
Proverò a portare il voltaggio a 9. Poi proverò a tenere in test su seriale, ma non ho ben capito l'uso delle librerie ossia la struttura delle cartelle per provare la libreria.

Praticamente devi scaricare la libreria, la scompatti, ti crea una cartella DHT, questa cartella la copy nella cartella "libreries" della tua installazione dell' ide.
Fai eseguire l' ide (o lo riavvii) e a questo punto puoi utilizzare l' empio/i che trovo in file/esempi/dht.
Se mastichi un po' di inglese, prova seguire quel link che ti ho dato, così potrai capire un pò di cose su quel sensore.

Scusa sono tonto ma non ho capito come devo costruire la cartella e le sottocartelle :roll_eyes:

Ho scaricato lo zip dal sito, ora scompattando tutto trovo i seguenti file: DHT.h - DHT.cpp - DHTtester.pde

Mi puoi strutturare come mettere il tutto per poter caricare su arduino il file .pde, grazie

Ho risolto, grazie ora provo.

Dimenticavo ho portato l'alimentazione a 9 ma il problema persiste il display ora impazzisce pure, compaiomo anche tutti simboli strani dopo che funziona un pò

Allora motivo in più per fare una prova con il dht11 solo e visualizzare la temp. via seriale...
Poi passiamo al display.

Controlla meglio. Di solito gli zip contenenti le librerie contengono ANCHE i percorsi dei file.

Ok dopo 2 ore sul monitor seriale ancora scorrono i valori senza intoppi e il sensore rileva esattamente le variazioni, adesso come mi muovo per applicarlo ad un display?

Il displsy è uno strumento di visualizzazione alternativo al serial monitor; per funzionare ha bisogno di una libreria specifica (che devi integrare nello sketch), di una dichiarazione dei pin collegati ad Arduino (ti servono al minimo 6 fili per i segnali + l'alimentazione), poi al posto dei comandi normali del Serial Monitor metti quelli specifici per il display.
Visto che hai risolto il problema del sensore ti basta seguire quanto scritto nel PlayGround a proposito del display LCD (va benissimo la versione a 4 bit) e completi rapidamente.