Advertisement
SallatielFernandes

RTC-set-echo

Jul 1st, 2022
784
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.37 KB | None
  1. /*
  2.  
  3. Sets the time from input and prints back time stamps for 5 seconds
  4.  
  5. Based on DS3231_set.pde
  6. by Eric Ayars
  7. 4/11
  8.  
  9. Added printing back of time stamps and increased baud rate
  10. (to better synchronize computer and RTC)
  11. Andy Wickert
  12. 5/15/2011
  13.  
  14. Clean for SAMD arch, add explanation, respect code-style and
  15. fix interpretation of Serial input if used more than once
  16. Olivier Staquet
  17. 4/26/2020
  18.  
  19. */
  20.  
  21. #include <DS3231.h>
  22. #include <Wire.h>
  23.  
  24. DS3231 clockk;
  25.  
  26. byte year;
  27. byte month;
  28. byte date;
  29. byte dow;
  30. byte hour;
  31. byte minute;
  32. byte second;
  33.  
  34. bool century = false;
  35. bool h12Flag;
  36. bool pmFlag;
  37.  
  38. /*****************************************************************************************************
  39.  * Setup
  40.  *  - Open Serial and Wire connection
  41.  *  - Explain to the user how to use the program
  42.  *****************************************************************************************************/
  43. void setup() {
  44.   // Start the serial port
  45.   Serial.begin(57600);
  46.  
  47.   // Start the I2C interface
  48.   Wire.begin();
  49.  
  50.   // Request the time correction on the Serial
  51.   delay(4000);
  52.   Serial.println("Format YYMMDDwhhmmssx");
  53.   Serial.println("Where YY = Year (ex. 20 for 2020)");
  54.   Serial.println("      MM = Month (ex. 04 for April)");
  55.   Serial.println("      DD = Day of month (ex. 09 for 9th)");
  56.   Serial.println("      w  = Day of week from 1 to 7, 1 = Sunday (ex. 5 for Thursday)");
  57.   Serial.println("      hh = hours in 24h format (ex. 09 for 9AM or 21 for 9PM)");
  58.   Serial.println("      mm = minutes (ex. 02)");
  59.   Serial.println("      ss = seconds (ex. 42)");
  60.   Serial.println("Example for input : 2004095090242x");
  61.   Serial.println("-----------------------------------------------------------------------------");
  62.   Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
  63. }
  64.  
  65. /*****************************************************************************************************
  66.  * Loop
  67.  *  - Each time we receive the time correction on the Serial
  68.  *  - Set the clockk of the DS3231
  69.  *  - Echo the value from the DS3231 during 5 seconds
  70.  *****************************************************************************************************/
  71. void loop() {
  72.   // If something is coming in on the serial line, it's
  73.   // a time correction so set the clockk accordingly.
  74.   if (Serial.available()) {
  75.     inputDateFromSerial();
  76.  
  77.     clockk.setClockMode(false);  // set to 24h
  78.  
  79.     clockk.setYear(year);
  80.     clockk.setMonth(month);
  81.     clockk.setDate(date);
  82.     clockk.setDoW(dow);
  83.     clockk.setHour(hour);
  84.     clockk.setMinute(minute);
  85.     clockk.setSecond(second);
  86.    
  87.     // Give time at next five seconds
  88.     for (uint8_t i = 0; i < 5; i++){
  89.         delay(1000);
  90.         Serial.print(clockk.getYear(), DEC);
  91.         Serial.print("-");
  92.         Serial.print(clockk.getMonth(century), DEC);
  93.         Serial.print("-");
  94.         Serial.print(clockk.getDate(), DEC);
  95.         Serial.print(" ");
  96.         Serial.print(clockk.getHour(h12Flag, pmFlag), DEC); //24-hr
  97.         Serial.print(":");
  98.         Serial.print(clockk.getMinute(), DEC);
  99.         Serial.print(":");
  100.         Serial.println(clockk.getSecond(), DEC);
  101.     }
  102.  
  103.     // Notify that we are ready for the next input
  104.     Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
  105.   }
  106.   delay(1000);
  107. }
  108.  
  109. /*****************************************************************************************************
  110.  * inputDateFromSerial
  111.  *  - Read and interpret the data from the Serial input
  112.  *  - Store the data in global variables
  113.  *****************************************************************************************************/
  114. void inputDateFromSerial() {
  115.     // Call this if you notice something coming in on
  116.     // the serial port. The stuff coming in should be in
  117.     // the order YYMMDDwHHMMSS, with an 'x' at the end.
  118.     boolean isStrComplete = false;
  119.     char inputChar;
  120.     byte temp1, temp2;
  121.     char inputStr[20];
  122.  
  123.     uint8_t currentPos = 0;
  124.     while (!isStrComplete) {
  125.         if (Serial.available()) {
  126.             inputChar = Serial.read();
  127.             inputStr[currentPos] = inputChar;
  128.             currentPos += 1;
  129.  
  130.       // Check if string complete (end with "x")
  131.             if (inputChar == 'x') {
  132.                 isStrComplete = true;
  133.             }
  134.         }
  135.     }
  136.     Serial.println(inputStr);
  137.  
  138.   // Find the end of char "x"
  139.   int posX = -1;
  140.   for(uint8_t i = 0; i < 20; i++) {
  141.     if(inputStr[i] == 'x') {
  142.       posX = i;
  143.       break;
  144.     }
  145.   }
  146.  
  147.   // Consider 0 character in ASCII
  148.   uint8_t zeroAscii = '0';
  149.  
  150.     // Read Year first
  151.     temp1 = (byte)inputStr[posX - 13] - zeroAscii;
  152.     temp2 = (byte)inputStr[posX - 12] - zeroAscii;
  153.     year = temp1 * 10 + temp2;
  154.  
  155.     // now month
  156.     temp1 = (byte)inputStr[posX - 11] - zeroAscii;
  157.     temp2 = (byte)inputStr[posX - 10] - zeroAscii;
  158.     month = temp1 * 10 + temp2;
  159.    
  160.     // now date
  161.     temp1 = (byte)inputStr[posX - 9] - zeroAscii;
  162.     temp2 = (byte)inputStr[posX - 8] - zeroAscii;
  163.     date = temp1 * 10 + temp2;
  164.    
  165.     // now Day of Week
  166.     dow = (byte)inputStr[posX - 7] - zeroAscii;    
  167.    
  168.     // now Hour
  169.     temp1 = (byte)inputStr[posX - 6] - zeroAscii;
  170.     temp2 = (byte)inputStr[posX - 5] - zeroAscii;
  171.     hour = temp1 * 10 + temp2;
  172.    
  173.     // now Minute
  174.     temp1 = (byte)inputStr[posX - 4] - zeroAscii;
  175.     temp2 = (byte)inputStr[posX - 3] - zeroAscii;
  176.     minute = temp1 * 10 + temp2;
  177.    
  178.     // now Second
  179.     temp1 = (byte)inputStr[posX - 2] - zeroAscii;
  180.     temp2 = (byte)inputStr[posX - 1] - zeroAscii;
  181.     second = temp1 * 10 + temp2;
  182. }
Advertisement
RAW Paste Data Copied
Advertisement