Schupp

Datalogger SD

Jan 10th, 2019
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.18 KB | None | 0 0
  1. #include <avr/pgmspace.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <Streaming.h>
  6. #include <EEPROM.h>
  7. #include "RTClib.h"
  8. #include <Time.h>
  9. #include <TimeLib.h>
  10.  
  11. const uint8_t days_in_month [12] PROGMEM = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  12. const int chipSelect = 10;
  13. const char string_0[] PROGMEM = "1=0-Fallend 1-Steigend 2-Beide";
  14. const char string_1[] PROGMEM = "2=Abtast Intervall";
  15. const char string_2[] PROGMEM = "3=0-Datum+Uhrzeit,1-Timestamp,2-Timestamp+Millis,3-Systemmillis,4-Systemmillis+Differenz";
  16. const char string_3[] PROGMEM = "4=Ausgabe 0-Serial 1-SD 2-Beides";
  17. const char string_4[] PROGMEM = "5=Minimale Pulslaenge in Ms";
  18. const char string_5[] PROGMEM = "6=Entprellung in Ms";
  19. const char string_6[] PROGMEM = "7=Channel Aktivieren 1=Ein  0=Aus";
  20. const char string_7[] PROGMEM = "8=Channel wechseln(1-4)";
  21. const char string_8[] PROGMEM = "9=Speichern!";
  22. const char string_9[] PROGMEM = "10=RTC setzen unixtime UTC!";
  23.  
  24. const char* const string_table[] PROGMEM  = {string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8, string_9};
  25. //Beispielconfig
  26. int configs[40] = {1, 5, 2, 0, 50, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  27.  
  28. //RTC&Zeit
  29. RTC_DS3231 RTC;
  30. String filename = "";
  31.  
  32. void setup() {
  33.   loadconfigs();
  34.   Serial.begin(115200);
  35.  
  36.   if (! RTC.begin())
  37.   {}
  38.   setSyncProvider(syncProvider);
  39.   setSyncInterval(1000);
  40.   if (!SD.begin(chipSelect)) {
  41.     Serial.println("Card failed, or not present");
  42.     // don't do anything more:
  43.     return;
  44.   }
  45.   filename += String(now() / 60 / 60);
  46.   filename += ".csv";
  47. }
  48.  
  49. #define SECONDS_FROM_1970_TO_2000 946684800
  50. int pins[4] = {2, 3, 4, 5}; //Pinconfig
  51.  
  52. char buffer2[100]; //Serial Buffer
  53. String inputString = ""; //Serial Buffer
  54. boolean stringComplete = false; //Serial Helper
  55.  
  56. int channel = 1; //Menuhelper
  57. boolean menuactive = false; //Menuhelper
  58. int menuchoice = 0; //Menuhelper
  59. int choicevalue = 0; //Menuhelper
  60.  
  61. int laststate[4] = {HIGH, HIGH, HIGH, HIGH}; //Statehelper
  62. int state[4] = {HIGH, HIGH, HIGH, HIGH}; //Statehelper
  63.  
  64. //Testtrash
  65. //int channelorder;
  66. //int chswitch = 1;
  67. //int chswitchbackup;
  68.  
  69. void loop() {
  70.   if (!menuactive)
  71.     intervallset();
  72.   serialmenu();
  73. }
  74.  
  75. //Call between Loops by Serial Input
  76. void serialEvent() {
  77.   while (Serial.available()) {
  78.     // get the new byte:
  79.     char inChar = (char)Serial.read();
  80.     if (!menuactive)
  81.     {
  82.       inputString += inChar;
  83.       if (inChar == '\n') {
  84.         stringComplete = true;
  85.       }
  86.     } else
  87.     {
  88.       if (isDigit(inChar)) {
  89.         inputString += (char)inChar;
  90.       }
  91.       if (inChar == '\n') {
  92.         stringComplete = true;
  93.       }
  94.     }
  95.   }
  96. }
  97.  
  98.  
  99. //MenuPrintout
  100. void menu() {
  101.   Serial.print(hour());
  102.   Serial.print(":");
  103.   Serial.print(minute());
  104.   Serial.print(":");
  105.   Serial.print(second());
  106.   Serial.print("-");
  107.   Serial.print(day());
  108.   Serial.print(".");
  109.   Serial.print(month());
  110.   Serial.print(".");
  111.   Serial.println(year());
  112.   Serial.print("Aktiver Kanal: ");
  113.   Serial.println(channel);
  114.  
  115.   for (int i = 0; i <= 9; i++)
  116.   { int test = ((channel - 1) * 10) + i;
  117.     int tempwert = configs[((((channel - 1) * 10) + i))];
  118.     if (tempwert != -1)
  119.       Serial.print(tempwert);
  120.     spaces(String(tempwert).length());
  121.     strcpy_P(buffer2, (char*)pgm_read_word(&(string_table[i])));
  122.     Serial.println(buffer2);
  123.     delay( 100 );
  124.   }
  125. }
  126.  
  127. //Menucontrol
  128. void serialmenu() {
  129.   // Serial.println(menuchoice);
  130.   if (stringComplete)
  131.  
  132.     if (!menuactive && inputString == "menu\n")
  133.     {
  134.       menuactive = true;
  135.       stringComplete = false;
  136.       inputString = "";
  137.       //   menuchoice=0;
  138.       menu();
  139.  
  140.     } else if (menuactive && menuchoice == 0)
  141.     {
  142.       menuchoice = inputString.toInt();
  143.       strcpy_P(buffer2, (PGM_P)pgm_read_word(&(string_table[menuchoice - 1])));
  144.       Serial.println(buffer2);
  145.       Serial.println("Neuen Wert eingeben!");
  146.       stringComplete = false;
  147.       inputString = "";
  148.  
  149.     } else if (menuactive && menuchoice == 8)
  150.     {
  151.       choicevalue = inputString.toInt();
  152.       if (choicevalue < 4) {
  153.         channel = choicevalue;
  154.       } else
  155.       {
  156.         Serial.println("Falsche Eingabe");
  157.       }
  158.       stringComplete = false;
  159.       inputString = "";
  160.       menuchoice = 0;
  161.  
  162.       menu();
  163.     } else if (menuactive && menuchoice == 9)
  164.     {
  165.       saveconfigs((channel - 1));
  166.       menuchoice = 0;
  167.       menuactive = false;
  168.       stringComplete = false;
  169.       inputString = "";
  170.       menu();
  171.     } else if (menuactive && menuchoice == 10)
  172.     {
  173.       static time_t tLast;
  174.       time_t t = inputString.toInt() + 3600;
  175.       RTC.adjust(t);
  176.       stringComplete = false;
  177.       inputString = "";
  178.       menuchoice = 0;
  179.       menuactive = false;
  180.       menu();
  181.       //dump any extraneous input
  182.  
  183.  
  184.     }
  185.  
  186.     else if (menuactive && menuchoice != 0)
  187.     {
  188.       choicevalue = inputString.toInt();
  189.       configs[((channel - 1) * 10) + (menuchoice - 1)] = choicevalue;
  190.       stringComplete = false;
  191.       inputString = "";
  192.       menuchoice = 0;
  193.  
  194.       menu();
  195.     }
  196. }
  197.  
  198. //Menuhelper für Zeilen
  199. void spaces(int sizechars) {
  200.   for (int z = 8; z != sizechars; z--)
  201.   {
  202.     Serial.print(" ");
  203.   }
  204.  
  205. }
  206.  
  207. boolean debouncing[4] = {false, false, false, false}; //Entprellt?
  208. boolean mininterval[4] = {true, true, true, true}; //Minimale Impulslänge für Aktion
  209. unsigned long lastintervallmillis[4] = {millis(), millis(), millis(), millis()};
  210. unsigned long changestate[4] = {millis(), millis(), millis(), millis()};
  211. unsigned long lastdebounce[4] = {millis(), millis(), millis(), millis()};
  212. unsigned long lastminpulse[4] = {millis(), millis(), millis(), millis()};
  213.  
  214. //ControlUnit
  215. void intervallset() {
  216.   unsigned long tempmillis = millis();
  217.   for (int i = 0; i <= 3; i++) {
  218.     if (tempmillis >= lastintervallmillis[i] + configs[(i * 10) + 1] && configs[(i * 10) + 6] == 1)
  219.     {
  220.       //  Serial.println("check state");
  221.       lastintervallmillis[i] = tempmillis;
  222.       int tempstate = digitalRead(pins[i]);
  223.  
  224.       if (tempstate != laststate[i])
  225.       {
  226.         debouncing[i] = true;
  227.         lastminpulse[i] = true;
  228.         laststate[i] = tempstate;
  229.         changestate[i] = tempmillis;
  230.         mininterval[i] = true;
  231.         //Serial.println("Statechange");
  232.       }
  233.  
  234.       if (debouncing[i]) {
  235.         // Serial.println("debouncing");
  236.         if (tempmillis >= changestate[i] + configs[((i * 10) + 5)] && debouncing[i])
  237.         { debouncing[i] = false;
  238.           mininterval[i] = false;
  239.         }
  240.       }
  241.       if (tempmillis >= changestate[i] + configs[((i * 10) + 5)] + configs[((i * 10) + 4)] && !debouncing[i] && !mininterval[i])
  242.       {
  243.         //Serial.println("check mininterval");
  244.         state[i] = laststate[i];
  245.         debouncing[i] = false;
  246.         lastminpulse[i] = false;
  247.         mininterval[i] = true;
  248.         int Flanke = configs[(i * 10)];
  249.         if (Flanke == 2)
  250.         {
  251.           Flanke = tempstate;
  252.         }
  253.         if (state[i] == Flanke) {
  254.           if (configs[((i * 10) + 3)] == 0 || configs[((i * 10) + 3)] == 2)
  255.           {
  256.             Serial.println( createlogtext(state[i], i, now(), millis()));
  257.           }
  258.           if (configs[((i * 10) + 3)] == 1 || configs[((i * 10) + 3)] == 2)  {
  259.             File dataFile = SD.open(filename, FILE_WRITE);
  260.             if (dataFile) {
  261.               dataFile.println( createlogtext(state[i], i, now(), millis() % 1000));
  262.               dataFile.close();
  263.             }
  264.  
  265.           }
  266.         }
  267.       }
  268.     }
  269.   }
  270. }
  271.  
  272. //Generate Unixtime for RTC Setup
  273. uint32_t get_unixtime(tmElements_t t)
  274. {
  275.   uint8_t i;
  276.   uint16_t d;
  277.   int16_t y;
  278.   uint32_t rv;
  279.   int tmpYear = t.Year + 1970;
  280.   if (tmpYear >= 2000) {
  281.     y = tmpYear - 2000;
  282.   } else {
  283.     return 0;
  284.   }
  285.  
  286.   d = t.Day - 1;
  287.   for (i = 1; i < (int)t.Month; i++) {
  288.     d += pgm_read_byte(days_in_month + i - 1);
  289.   }
  290.   if ((int)t.Month > 2 && y % 4 == 0) {
  291.     d++;
  292.   }
  293.   // count leap days
  294.   d += (365 * y + (y + 3) / 4);
  295.   rv = ((d * 24UL + (int)t.Hour) * 60 + (int)t.Minute) * 60 + (int)t.Second + SECONDS_FROM_1970_TO_2000;
  296.   return rv;
  297. }
  298.  
  299. //print date and time to Serial
  300. void printDateTime(time_t t)
  301. {
  302.   printDate(t);
  303.   Serial << ' ';
  304.   printTime(t);
  305. }
  306.  
  307. //print time to Serial
  308. void printTime(time_t t)
  309. {
  310.   printI00(hour(t), ':');
  311.   printI00(minute(t), ':');
  312.   printI00(second(t), ' ');
  313. }
  314.  
  315. //print date to Serial
  316. void printDate(time_t t)
  317. {
  318.   printI00(day(t), 0);
  319.   Serial << monthShortStr(month(t)) << _DEC(year(t));
  320. }
  321.  
  322. //Print an integer in "00" format (with leading zero),
  323. //followed by a delimiter character to Serial.
  324. //Input value assumed to be between 0 and 99.
  325. void printI00(int val, char delim)
  326. {
  327.   if (val < 10) Serial << '0';
  328.   Serial << _DEC(val);
  329.   if (delim > 0) Serial << delim;
  330.   return;
  331. }
  332.  
  333. void saveconfigs(int channel) {
  334.   for (int i = channel * 10; i <= channel * 10 + 10; i++)
  335.     eepromWriteInt(i * 2, configs[i]);
  336. }
  337.  
  338.  
  339. void loadconfigs() {
  340.   for (int i = 0; i <= 39; i++)
  341.     configs[i] = eepromReadInt(i * 2);
  342. }
  343. void eepromWriteInt(int adr, int wert) {
  344.   byte low, high;
  345.   low = wert & 0xFF;
  346.   high = (wert >> 8) & 0xFF;
  347.   EEPROM.write(adr, low); // dauert 3,3ms
  348.   EEPROM.write(adr + 1, high);
  349.   return;
  350. } //eepromWriteInt
  351. int eepromReadInt(int adr) {
  352.   byte low, high;
  353.   low = EEPROM.read(adr);
  354.   high = EEPROM.read(adr + 1);
  355.   return low + ((high << 8) & 0xFF00);
  356. } //eepromReadInt
  357. unsigned long laststatemillis1[4];
  358. unsigned long laststatemillis0[4];
  359. String createlogtext(int state, int channel, unsigned long newunixtime, unsigned long newmillis) {
  360.   //"3=0-Datum+Uhrzeit,1-Timestamp,2-TimestampMillis,3-Systemtime";
  361.   String tempstring = "'";
  362.   int checkconfig = configs[(channel * 10) + 2];
  363.  
  364.   switch (checkconfig) {
  365.     case 0:
  366.  
  367.       tempstring += day();
  368.       tempstring += ".";
  369.       tempstring += month();
  370.       tempstring += ".";
  371.       tempstring += year();
  372.       tempstring += "-";
  373.       tempstring += hour();
  374.       tempstring += ":";
  375.       tempstring += minute();
  376.       tempstring += ":";
  377.       tempstring += second();
  378.       tempstring += "'";
  379.       tempstring += ",";
  380.       tempstring += "'";
  381.       tempstring += channel;
  382.       tempstring += "'";
  383.       tempstring += ",";
  384.       tempstring += "'";
  385.       tempstring += state;
  386.       tempstring += "'";
  387.  
  388.       return tempstring;
  389.       break;
  390.     //do something when var equals 1
  391.     case 1:
  392.       tempstring += newunixtime;
  393.       tempstring += "'";
  394.       tempstring += ",";
  395.       tempstring += "'";
  396.       tempstring += channel;
  397.       tempstring += "'";
  398.       tempstring += ",";
  399.       tempstring += "'";
  400.       tempstring += state;
  401.       tempstring += "'";
  402.       return tempstring;
  403.     case 2:
  404.  
  405.       tempstring += newunixtime;
  406.       tempstring += "'";
  407.       tempstring += ",";
  408.       tempstring += "'";
  409.       tempstring += newmillis;
  410.       tempstring += "'";
  411.  
  412.       tempstring += ",";
  413.       tempstring += "'";
  414.       tempstring += channel;
  415.       tempstring += "'";
  416.       tempstring += ",";
  417.       tempstring += "'";
  418.       tempstring += state;
  419.       tempstring += "'";
  420.       return tempstring;
  421.       break;
  422.     case 3:
  423.  
  424.       tempstring += newmillis;
  425.       tempstring += "'";
  426.       tempstring += ",";
  427.       tempstring += "'";
  428.       tempstring += channel;
  429.       tempstring += "'";
  430.       tempstring += ",";
  431.       tempstring += "'";
  432.       tempstring += state;
  433.       tempstring += "'";
  434.       return tempstring;
  435.       break;
  436.     case 4:
  437.  
  438.       tempstring += newmillis;
  439.       tempstring += "'";
  440.       tempstring += ",";
  441.       tempstring += "'";
  442.       tempstring += channel;
  443.       tempstring += "'";
  444.       tempstring += ",";
  445.       tempstring += "'";
  446.       tempstring += state;
  447.       tempstring += "'";
  448.       tempstring += ",";
  449.       tempstring += "'";
  450.       if (state == 0) {
  451.         tempstring += newmillis - laststatemillis0[channel];
  452.         laststatemillis0[channel] = newmillis;
  453.       } else {
  454.         tempstring += newmillis - laststatemillis1[channel];
  455.         laststatemillis1[channel] = newmillis;
  456.       }
  457.       tempstring += "'";
  458.  
  459.       return tempstring;
  460.       break;
  461.   }
  462. }
  463. time_t syncProvider()
  464. {
  465.   return RTC.now().unixtime();
  466. }
Advertisement
Add Comment
Please, Sign In to add comment