Forum Index > Projects > Clock Kits
 millis() rollover
 |  Printable Version
By: smh (offline) on Monday, January 18 2010 @ 10:18 AM PST (Read 1875 times)  
smh

FWIW, the code in beady1.pde to "correct" for rollover from the millis() function is unnecessary.

You don't need to correct for rollover when you're calculating the difference between unsigned values. If you're not convinced (I wasn't either), try running this simple C program. It uses unsigned char rather than unsigned long to save time, but the principle is the same.

PHP Formatted Code

#include <stdio.h>

int main(int argc, char* argv[]) {

  // Two unsigned values
  unsigned char then = 0;
  unsigned char now = then + 10;
  // Enough iterations for both values to overflow
  int i
  for (i = 0; i < 300; i++) {
    unsigned char dt = now - then;
    printf("Now = %u, Then = %u, Difference = %u\n",now, then,dt);
    if ( dt != 10 ) {
      break;
    }
    ++now;
    ++then;
    if ( now == 0) {
      printf("Now is about to overflow...\n");
    }
    if ( then == 0) {
      printf("Then is about to overflow...\n");
    }
  }
  printf("Done\n");

}
 



Great kit by the way!


Forum Apprentice
Apprentice

Status: offline

Registered: 01/15/10
Posts: 2

Profile Email    
   



 All times are PDT. The time is now 02:21 AM.
Normal Topic Normal Topic
Locked Topic Locked Topic
Sticky Topic Sticky Topic
New Post New Post
Sticky Topic W/ New Post Sticky Topic W/ New Post
Locked Topic W/ New Post Locked Topic W/ New Post
View Anonymous Posts 
Able to Post 
Filtered HTML Allowed 
Censored Content 

Evil Mad Scientist Forum Archives — Read only!

Please visit our new forums for new discussions.


DIY Hardware for Electronic Art


The Original Egg-Bot Kit


Octolively
Interactive LED kits


Meggy Jr RGB
LED matrix game
development kit.


Business-card sized
AVR target boards


Peggy 2
LED Pegboard kits

My Account






Lost your password?