RuiViana

rtc_sd_controle_II.ino

Feb 3rd, 2019
242
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include "RTClib.h"
  3. #include <SPI.h>
  4. #include <SD.h>
  5.  
  6. File myFile;
  7. RTC_DS1307 rtc;
  8. String stringOne;
  9. String Stringyear = "2010";
  10. String Stringmonth = "01";
  11. String Stringday   = "01";
  12. unsigned long atual;
  13. unsigned long anterior = 0;
  14. const long intervalo = 60000;
  15. unsigned long val = 0.00;
  16. unsigned long s;
  17. float media;
  18. //-----------------------------------------------------------------
  19. void setup () {
  20.   Serial.begin(115200);
  21.   if (!SD.begin(15)) {
  22.     return;
  23.   }
  24.   if (! rtc.begin()) {
  25.     Serial.println("Couldn't find RTC");
  26.     while (1);
  27.   }
  28.   if (! rtc.isrunning()) {
  29.     Serial.println("RTC is NOT running!");
  30.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  31.   }
  32. }
  33. //-----------------------------------------------------------------
  34. void loop () {
  35.   DateTime now = rtc.now();
  36.   if ((now.hour() == 17) && (now.minute() == 59) && (now.second() == 0))
  37.   {
  38.     Stringyear = now.year(), DEC;
  39.     Stringmonth = now.month(), DEC;
  40.     Stringday = now.day(), DEC;
  41.   }
  42.   stringOne = Stringyear + "/" + Stringmonth + "/" + Stringday + ".txt";
  43.   if ((now.hour() < 6) || (now.hour() > 18))
  44.   {
  45.     atual = millis();
  46.     if (anterior != atual)
  47.     {
  48.       int valor = analogRead (A0);
  49.       val = val + valor;
  50.       s = s + 1.00;
  51.       if (atual - anterior >= intervalo )
  52.       {
  53.         anterior = atual;
  54.         media = (val * 3.2226) / s;
  55.         myFile = SD.open(stringOne, FILE_WRITE);
  56.         if (myFile) {
  57.           DateTime now = rtc.now();
  58.           myFile.print(now.hour(), DEC);
  59.           myFile.print(':');
  60.           myFile.print(now.minute(), DEC);
  61.           myFile.print(" \t");
  62.           myFile.println(media);
  63.           myFile.close();
  64.           Serial.println('.');
  65.         }
  66.         s = 0.00;
  67.         val = 0.00;
  68.       }
  69.     }
  70.   }
  71.   else
  72.   {
  73.     s = 0.00;
  74.     val = 0.00;
  75.     media = 0.00;
  76.     Serial.print(".");
  77.   }
  78. }
RAW Paste Data