Advertisement
ektoras2012

papadimos

Jan 11th, 2021
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include "DHT.h"
  2.  
  3. #define DHTPIN 2
  4.  
  5.  
  6. //#define DHTTYPE DHT11   // DHT 11
  7. #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  8. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  9.  
  10. DHT dht(DHTPIN, DHTTYPE);
  11. int TimeToMeasure(int intIncrements);
  12. float GetMeasurement(boolean blnIsFirstMeasurement=false);
  13.  
  14. boolean blnIsFirstMeasure=false;
  15. unsigned long ulFirstMeasurementPreviewMillis=0;
  16. unsigned long ulMeasurementPreviewMillis=0;
  17. float flMeasurements[10];
  18. int intNumberOfSamples=10;
  19. byte btNowMeasure=0;
  20. float flAverage=-200;
  21.  
  22. void setup() {
  23.   Serial.begin(9600);
  24.   Serial.println(F("DHTxx test!"));
  25.  
  26.   dht.begin();
  27.  
  28.   GetMeasurement(true);
  29. }
  30.  
  31. void loop() {
  32.  
  33.    float flAVR=0;
  34.  
  35.    flAVR=GetMeasurement();
  36.    if(flAVR!=float(-200))
  37.      {
  38.         Serial.println(flAVR);
  39.      }
  40. }
  41.  
  42.  
  43. float GetMeasurement(boolean blnIsFirstMeasurement=false)
  44. {
  45.  
  46.    unsigned long ulTime=millis()-ulMeasurementPreviewMillis;
  47.    byte btCount=-1;
  48.    float flSum=0;
  49.    boolean blnGiveTheMeasure=false;
  50.    
  51.    if(blnIsFirstMeasurement==true)
  52.      {
  53.         ulFirstMeasurementPreviewMillis=0;
  54.         blnIsFirstMeasure=true;
  55.      }
  56.  
  57.  
  58.    if (blnIsFirstMeasure==true)
  59.       {
  60.          if(millis()>2000)
  61.            {
  62.               blnIsFirstMeasure=false;  
  63.            }
  64.       }
  65.       else
  66.       {
  67.            if(ulTime<0){ulMeasurementPreviewMillis=millis();}
  68.            if(ulTime>=TimeToMeasure(intNumberOfSamples))
  69.              {
  70.                btNowMeasure++;
  71.                flMeasurements[btNowMeasure-1]=dht.readTemperature();
  72.                if (isnan(flMeasurements[btNowMeasure-1]))
  73.                   {
  74.                     do
  75.                     {
  76.                       flMeasurements[btNowMeasure-1]=dht.readTemperature();
  77.                     }while(isnan(flMeasurements[btNowMeasure-1]==false));
  78.                   }
  79.                if(btNowMeasure==byte(intNumberOfSamples))
  80.                  {
  81.                     ulMeasurementPreviewMillis=millis();
  82.                    
  83.                     for(btCount=0;btCount<byte(intNumberOfSamples);btCount++)
  84.                        {
  85.                           flSum=flSum+flMeasurements[btCount];
  86.                           flAverage=flSum/intNumberOfSamples;
  87.                        }
  88.                     btNowMeasure=0;
  89.                     btCount=0;
  90.                  }
  91.              ulMeasurementPreviewMillis=millis();
  92.              }
  93.       }
  94.       if(flAverage!=-200 && btCount==0){}else{flAverage=-200;}
  95.       return flAverage;
  96. }
  97.  
  98. int TimeToMeasure(int intIncrements)
  99. {
  100.   int intTime=0;
  101.  
  102.   if(intIncrements==10){intTime=6000;}
  103.   if(intIncrements==9){intTime=6666;}
  104.   if(intIncrements==8){intTime=7500;}
  105.   if(intIncrements==7){intTime=8571;}
  106.   if(intIncrements==6){intTime=10000;}
  107.  
  108.   return intTime;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement