How millis() resets itself to 0











up vote
8
down vote

favorite












Looking at the documentation for the millis() function , it says:




Returns the number of milliseconds since the Arduino board began
running the current program. This number will overflow (go back to
zero), after approximately 50 days.




How's this possible? Is Arduino detecting when millis() overflow then resets the value to 0? If so, how does it do it? I just want to know what exactly is going on under the hood with the variable that is being returned by the millis() function.










share|improve this question


























    up vote
    8
    down vote

    favorite












    Looking at the documentation for the millis() function , it says:




    Returns the number of milliseconds since the Arduino board began
    running the current program. This number will overflow (go back to
    zero), after approximately 50 days.




    How's this possible? Is Arduino detecting when millis() overflow then resets the value to 0? If so, how does it do it? I just want to know what exactly is going on under the hood with the variable that is being returned by the millis() function.










    share|improve this question
























      up vote
      8
      down vote

      favorite









      up vote
      8
      down vote

      favorite











      Looking at the documentation for the millis() function , it says:




      Returns the number of milliseconds since the Arduino board began
      running the current program. This number will overflow (go back to
      zero), after approximately 50 days.




      How's this possible? Is Arduino detecting when millis() overflow then resets the value to 0? If so, how does it do it? I just want to know what exactly is going on under the hood with the variable that is being returned by the millis() function.










      share|improve this question













      Looking at the documentation for the millis() function , it says:




      Returns the number of milliseconds since the Arduino board began
      running the current program. This number will overflow (go back to
      zero), after approximately 50 days.




      How's this possible? Is Arduino detecting when millis() overflow then resets the value to 0? If so, how does it do it? I just want to know what exactly is going on under the hood with the variable that is being returned by the millis() function.







      millis






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 at 7:58









      Programmer

      22218




      22218






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          18
          down vote



          accepted










          It is not a property of millis(). Every counter with limited number of digits once returns to zeros. For example a 4 digits tally counter returns to zeros after 9999.



          Under the hood the variable for millis() is of type unsigned long which is 32 bits on the Arduino. Here the 32 bits are the digits of the tally counter. With only two digits of binary 0 and 1. The maximum is 11111111 11111111 11111111 11111111. After then it returns to zeros if adding 1. The 32 ones is 2^32 - 1, or 4294967295 in decimal. The maximum value for the counter is then 4294967295 milliseconds. Converted to days you get approximately 49.71 days.



          And now, you know how computers work. Thousands of binary tally counters.



          enter image description here






          share|improve this answer



















          • 2




            Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
            – Russell Borogove
            Nov 26 at 2:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("schematics", function () {
          StackExchange.schematics.init();
          });
          }, "cicuitlab");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "540"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58164%2fhow-millis-resets-itself-to-0%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          18
          down vote



          accepted










          It is not a property of millis(). Every counter with limited number of digits once returns to zeros. For example a 4 digits tally counter returns to zeros after 9999.



          Under the hood the variable for millis() is of type unsigned long which is 32 bits on the Arduino. Here the 32 bits are the digits of the tally counter. With only two digits of binary 0 and 1. The maximum is 11111111 11111111 11111111 11111111. After then it returns to zeros if adding 1. The 32 ones is 2^32 - 1, or 4294967295 in decimal. The maximum value for the counter is then 4294967295 milliseconds. Converted to days you get approximately 49.71 days.



          And now, you know how computers work. Thousands of binary tally counters.



          enter image description here






          share|improve this answer



















          • 2




            Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
            – Russell Borogove
            Nov 26 at 2:18















          up vote
          18
          down vote



          accepted










          It is not a property of millis(). Every counter with limited number of digits once returns to zeros. For example a 4 digits tally counter returns to zeros after 9999.



          Under the hood the variable for millis() is of type unsigned long which is 32 bits on the Arduino. Here the 32 bits are the digits of the tally counter. With only two digits of binary 0 and 1. The maximum is 11111111 11111111 11111111 11111111. After then it returns to zeros if adding 1. The 32 ones is 2^32 - 1, or 4294967295 in decimal. The maximum value for the counter is then 4294967295 milliseconds. Converted to days you get approximately 49.71 days.



          And now, you know how computers work. Thousands of binary tally counters.



          enter image description here






          share|improve this answer



















          • 2




            Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
            – Russell Borogove
            Nov 26 at 2:18













          up vote
          18
          down vote



          accepted







          up vote
          18
          down vote



          accepted






          It is not a property of millis(). Every counter with limited number of digits once returns to zeros. For example a 4 digits tally counter returns to zeros after 9999.



          Under the hood the variable for millis() is of type unsigned long which is 32 bits on the Arduino. Here the 32 bits are the digits of the tally counter. With only two digits of binary 0 and 1. The maximum is 11111111 11111111 11111111 11111111. After then it returns to zeros if adding 1. The 32 ones is 2^32 - 1, or 4294967295 in decimal. The maximum value for the counter is then 4294967295 milliseconds. Converted to days you get approximately 49.71 days.



          And now, you know how computers work. Thousands of binary tally counters.



          enter image description here






          share|improve this answer














          It is not a property of millis(). Every counter with limited number of digits once returns to zeros. For example a 4 digits tally counter returns to zeros after 9999.



          Under the hood the variable for millis() is of type unsigned long which is 32 bits on the Arduino. Here the 32 bits are the digits of the tally counter. With only two digits of binary 0 and 1. The maximum is 11111111 11111111 11111111 11111111. After then it returns to zeros if adding 1. The 32 ones is 2^32 - 1, or 4294967295 in decimal. The maximum value for the counter is then 4294967295 milliseconds. Converted to days you get approximately 49.71 days.



          And now, you know how computers work. Thousands of binary tally counters.



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 at 11:35

























          answered Nov 25 at 8:27









          Juraj

          6,3702925




          6,3702925








          • 2




            Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
            – Russell Borogove
            Nov 26 at 2:18














          • 2




            Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
            – Russell Borogove
            Nov 26 at 2:18








          2




          2




          Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
          – Russell Borogove
          Nov 26 at 2:18




          Old versions of Windows also maintained a milliseconds count in a 32-bit integer, causing lots of software to misbehave after 49 days uptime.
          – Russell Borogove
          Nov 26 at 2:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Arduino Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58164%2fhow-millis-resets-itself-to-0%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?