Press Duration

Here some clock code from my code junkyard. It takes into account the arduino clock rollover (once millis gets too high it rolls back over to 0.) It will print out the time that has passed to the serial monitor (seconds, minutes, hours,days). You can also hook up 2 LEDs that will blink out the number of minutes and hours.

/* Millis Rollover
 * 
 * Example of counting how many times the millis() function has rolled over
 *  by Rob Faludi http://www.faludi.com
 *
 * for the ATMEGA168 with Arduino 0010 the max value is 34359737 or about 9 hours and 32 mintues
 * for more information see: http://www.arduino.cc/en/Reference/Millis
 *
 */

// The LEDs on pins 13 and 10 will blink out the number of minutes that have passed and the number of hours.  

#define HourLed 13 // light for status output

int days = 0;
int halfday = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;

unsigned long then;  
  
  int counter = 0;
  int hourcounter = 0;

unsigned long secondtag = 0;
  unsigned long minutetag = 0;
  
  int on = 1;
  
void setup() {
  Serial.begin(9600); // start serial output
 pinMode (13, OUTPUT);
 pinMode (10, OUTPUT);
}


void loop() {
  int rollovers = millisRollover(); // get the number of rollovers so far

unsigned long present = millis(); // the time right now

if (present - then >= 1000){
Serial.print("Hours: ");
  Serial.println(hours);
Serial.print("Minutes: ");
  Serial.println(minutes);
  Serial.print("Seconds: ");
  Serial.println(seconds);
//Serial.print("Days: ");
//  Serial.println(days);




//  Serial.print("Rollovers: "); // show the number of rollovers so far
//  Serial.println(rollovers,DEC); // 
  Serial.println();
}





/* MAX millis = 34359736
Which is 9.5443711 hours.  
which is 9 hours and 32.662266 minutes 

9.5 hours is 32400000 milliseconds
*/

if (present - then >= 1000){
  seconds ++;
  then = present;
}


/*
THIS TAKES INTO ACCOUNT THE ROLLOVER, NEED TO ADJUST "THEN" FOR THE ROLLOVER DELAY
//How can 'Then' be larger than 'Now?'  Even though 'Now' has reset back to 0, "Then" will still be 
the previous value, like 34359701.  This will mess up our other if statements, so 
the 'then' variable needs to be reset so that it is less than 'now.'  
*/

// need to change then to a positive number?  

if (then > present){
 then = (then - 34359736) - 1; // 1 is the missing millisecond from the rollover delay????? just a guess

//the new then is a negative number, when this negative number is subtracted in the normal 'seconds' if statement
//it will be added and everything will work out fine.
}

if (seconds >= 60){
  minutes++;
  seconds = 0;
}

if (minutes >= 60){
 hours++;
 minutes = 0;
}

if (hours == 12){
 halfday++;
  hours = 0;
}

if (halfday == 2){
days++;
halfday = 0;
}


// this function blinks the an LED light as many times as requested
// --Rob Faludi http://rob.faludi.com
//

/*
  for (int i=0; i<hours; i++) {
    digitalWrite(13, HIGH);   // sets the LED on
    delay(2000);
    digitalWrite(13, LOW);    // sets the LED off
delay(2000);
  }

*/
  //---------------------------------------------
  //    BLINKING THE MINUTES
  
  if (counter <= minutes){  // blink every half second

   if (present - secondtag > 400){

if (on == 0){
  digitalWrite(13, LOW);
  on = 1;
} else if (on == 1){
      digitalWrite(13, HIGH);
      on = 0;
      counter++;
}
   
 secondtag = present;
 minutetag = present;

 }
   
}


  if (counter > minutes){  // wait 2 seconds
 digitalWrite(13, LOW);
 // Serial.println(counter);
if (present - minutetag > 2000){  
  counter = 0;  
  secondtag = 0;
   
}
}

   
//-------------------------------------------------------------------

//    BLINKING THE HOURS
  
  if (hourcounter <= hours){  // blink every half second

   if (present - secondtag > 400){

if (on == 0){
  digitalWrite(10, LOW);
  on = 1;
} else if (on == 1){
      digitalWrite(10, HIGH);
      on = 0;
      hourcounter++;
}
   
 secondtag = present;
 minutetag = present;

 }
   
}


  if (hourcounter > hours){  // wait 2 seconds
 digitalWrite(10, LOW);
 // Serial.println(counter);
if (present - minutetag > 2000){  
  hourcounter = 0;  
  secondtag = 0;
   
}
}













/*
//Days Array:
DayArray[6] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

Serial.println(DayArray[days]);


Months Array:
MonthArray[11] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}


Serial.println(MonthArray[months]);

*/

/*
// This will turn on 2 servos once every 24 hours
CONTINUOUS MOTOR CODING

if (hours == 0 && minutes == 1 && seconds < 3){ // < 3 works w/ servo?
  servo1control = 2035;
  servo2control = 4035;
  delay(3000);         // is the delay necessary?  check it
  if (seconds >= 3){    // is this necessary?  Check it.
   seconds = seconds - 3; 
  } else ...
}
*/




}

//----------------------------------------------------------------------------------
int millisRollover() {
  // get the current millis() value for how long the microcontroller has been running
  //
  // To avoid any possiblity of missing the rollover, we use a boolean toggle that gets flipped
  //   off any time during the first half of the total millis period and
  //   then on during the second half of the total millis period.
  // This would work even if the function were only run once every 4.5 hours, though typically,
  //   the function should be called as frequently as possible to capture the actual moment of rollover.
  // The rollover counter is good for over 35 years of runtime. --Rob Faludi http://rob.faludi.com
  //
  static int numRollovers=0; // variable that permanently holds the number of rollovers since startup
  static boolean readyToRoll = false; // tracks whether we've made it halfway to rollover
  unsigned long now = millis(); // the time right now
  unsigned long halfwayMillis = 17179868; // this is halfway to the max millis value (17179868)

  if (now > halfwayMillis) { // as long as the value is greater than halfway to the max
    readyToRoll = true; // you are ready to roll over
  }

  if (readyToRoll == true && now < halfwayMillis) {
    // if we've previously made it to halfway
    // and the current millis() value is now _less_ than the halfway mark
    // then we have rolled over
    numRollovers = numRollovers++; // add one to the count the number of rollovers
    readyToRoll = false; // we're no longer past halfway
  } 
  return numRollovers;
}