🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    [Solved]using built-in RTC on ArduinoIDE?

    Scheduled Pinned Locked Moved Cores
    5 Posts 2 Posters 11.8k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      akita11
      last edited by m5-docs

      In MicroPython, we can use built-in RTC as clock, such as NTP clock.
      Is there any way (library) to handle built-in RTC as clock on ArduinoIDE?

      1 Reply Last reply Reply Quote 0
      • K Offline
        kat
        last edited by

        Hi,
        I have found this, it's maybe what you want:

        #include <M5Stack.h>
        #define TFT_GREY 0x5AEB

        uint32_t targetTime = 0; // for next 1 second timeout
        static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x
        uint8_t hh = conv2d(TIME), mm = conv2d(TIME + 3), ss = conv2d(TIME + 6); // Get H, M, S from compile time

        byte omm = 99, oss = 99;
        byte xcolon = 0, xsecs = 0;
        unsigned int colour = 0;

        void setup(void) {
        //Serial.begin(115200);
        M5.begin();
        // M5.Lcd.setRotation(1);
        M5.Lcd.fillScreen(TFT_BLACK);

        M5.Lcd.setTextSize(1);
        M5.Lcd.setTextColor(TFT_YELLOW, TFT_BLACK);

        targetTime = millis() + 1000;
        }

        void loop() {
        if (targetTime < millis()) {
        // Set next update for 1 second later
        targetTime = millis() + 1000;

        // Adjust the time values by adding 1 second
        ss++;              // Advance second
        if (ss == 60) {    // Check for roll-over
          ss = 0;          // Reset seconds to zero
          omm = mm;        // Save last minute time for display update
          mm++;            // Advance minute
          if (mm > 59) {   // Check for roll-over
            mm = 0;
            hh++;          // Advance hour
            if (hh > 23) { // Check for 24hr roll-over (could roll-over on 13)
              hh = 0;      // 0 for 24 hour clock, set to 1 for 12 hour clock
            }
          }
        }
        
        
        // Update digital time
        int xpos = 0;
        int ypos = 85; // Top left corner ot clock text, about half way down
        int ysecs = ypos + 24;
        
        if (omm != mm) { // Redraw hours and minutes time every minute
          omm = mm;
          // Draw hours and minutes
          if (hh < 10) xpos += M5.Lcd.drawChar('0', xpos, ypos, 8); // Add hours leading zero for 24 hr clock
          xpos += M5.Lcd.drawNumber(hh, xpos, ypos, 8);             // Draw hours
          xcolon = xpos; // Save colon coord for later to flash on/off later
          xpos += M5.Lcd.drawChar(':', xpos, ypos - 8, 8);
          if (mm < 10) xpos += M5.Lcd.drawChar('0', xpos, ypos, 8); // Add minutes leading zero
          xpos += M5.Lcd.drawNumber(mm, xpos, ypos, 8);             // Draw minutes
          xsecs = xpos; // Sae seconds 'x' position for later display updates
        }
        if (oss != ss) { // Redraw seconds time every second
          oss = ss;
          xpos = xsecs;
        
          if (ss % 2) { // Flash the colons on/off
            M5.Lcd.setTextColor(0x39C4, TFT_BLACK);        // Set colour to grey to dim colon
            M5.Lcd.drawChar(':', xcolon, ypos - 8, 8);     // Hour:minute colon
            xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 6); // Seconds colon
            M5.Lcd.setTextColor(TFT_YELLOW, TFT_BLACK);    // Set colour back to yellow
          }
          else {
            M5.Lcd.drawChar(':', xcolon, ypos - 8, 8);     // Hour:minute colon
            xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 6); // Seconds colon
          }
        
          //Draw seconds
          if (ss < 10) xpos += M5.Lcd.drawChar('0', xpos, ysecs, 6); // Add leading zero
          M5.Lcd.drawNumber(ss, xpos, ysecs, 6);                     // Draw seconds
         }
        

        }
        }

        static uint8_t conv2d(const char* p) {
        uint8_t v = 0;
        if ('0' <= *p && *p <= '9')
        v = *p - '0';
        return 10 * v + *++p - '0';
        }

        A 1 Reply Last reply Reply Quote 0
        • A Offline
          akita11 @kat
          last edited by

          @kat Thanks, this seems to be software-counting clock in main loop. I'm looking for background-counting clock...

          1 Reply Last reply Reply Quote 0
          • A Offline
            akita11
            last edited by

            No, my question is NOT solved yet...

            1 Reply Last reply Reply Quote 0
            • K Offline
              kat
              last edited by

              @akita11 said in [Solved]using built-in RTC on ArduinoIDE?:

              No, my question is NOT solved yet...
              ah ok sorry, i don't know this...

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • First post
                Last post