Go Down

### Topic: need timer to count and display time from zero to 30 days (day-hour:min:sec) (Read 2528 times)previous topic - next topic

#### dalai

I need a counter ,for time elapsed from zero point (reset)  but this timer must count for days.
it will show to display "day 2 :00:01:03"  and thiw will mean that its elapsed 2 days and 1 min 3 sec from the time I pressed reset button.

My only question is how can I start this timer on mega8

Thank you

The IDE supports the Mega8.
Use micros( ) to count seconds, when that rolls over increment tens of seconds, when that rolls over increments the minutes, when that rolls over increment the tens of minutes, when that rolls over increment the hours, when that rolls from from 24 to 25 reset it to 0 and increment the days.
Use MAX7219 to drive the digits, has a register for each digit.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### teddyz

My Arduino has a clock frequency slightly off, so each day I loose a minute or two. I added a RTC from eBay (less than \$2.50) that change the time everyday.

For you I guess it has the advantage that you could make your device restart the countdown so it finish when you want if you happen to loose power, or something else make the Arduino reboot.

A minute or two?  Wow!
My crystal based duemilanove only drifts a second a day.
I use micros( ) to track the seconds, how is your code doing it?

Code: [Select]
`void loop(){currentMicros = micros();if ((currentMicros - previousMicros)>=1000000){ // check for 1 second passed  previousMicros = previousMicros + 1000000; //  time_update = 1;seconds = seconds +1;if (seconds == 10){   seconds = 0;   tens_seconds = tens_seconds +1;    if (tens_seconds == 6){    tens_seconds = 0;    etc. for minutes, hours, days    }  }}  // done with with counter updates// update display if neededif (time_udpate == 1){time_update = 0;// send data to the MAX7219 ...  }// do other stuff your code might do} // end ofvoid loop`
Battery backed RTC could keep you going during power loss, especially if the sketch start time is stored in the RTC  SRAM
(DS1307 has 56 bytes for example) and the sketch does the math to show the elapsed time from the stored startup and the current time.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Jimmy60

Use the Time.h library. Set the time to 0 on start up. now() will return the elapsed seconds.

Code: [Select]
`#include <Time.h>time_t seconds = 0;void setup(){  setTime(seconds);     Serial.begin(115200);     // or whatever rate you prefer}void loop(){  seconds = now();  Serial.println(seconds);}`

This works on an UNO. Making it look nice should be easy.

http://playground.arduino.cc/Code/time

Library - not need here, and it doesn't give the individual digits for display if that's what is needed.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### dalai

This is what I made 1 year ago .But to succeeded  I changed time  libraries  . Now with a fresh version of arduino ,this cod doesn't run . I lost my changed time library and I cant remember the changes that I have made
In this program a counter starts from zero. you can add hours and mins  by pressing pins or you can erase timer.
Also there is a backup ,every hour, in case of power loss.
Code: [Select]
`#include <DateTime.h>#include <LiquidCrystal.h>#include <EEPROM.h>/* LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3)./. */      int resetPin = 10; // pin 10 resets the time    int addHourPin = 9;     int addMinPin = 8;     long restoreTime = 0;    int  restoreDay = 0;    int eepromTime = 0;    int eepromDay = 0;    LiquidCrystal lcd(7, 6, 5, 4, 3, 2);//create object to control an LCD void setup(){         lcd.begin(16, 4);  // set up the LCD's number of columns and rows        digitalWrite(resetPin,HIGH);  // this line enables pull-up       digitalWrite(addHourPin,HIGH);// this line enables pull-up       digitalWrite(addMinPin,HIGH);// this line enables pull-up       pinMode(13,OUTPUT);          // we flash the LED each second       pinMode(resetPin, INPUT);    // a button on this pin resets the time       pinMode(addHourPin, INPUT);//add hours to counter       pinMode(addMinPin, INPUT);//add hours to counter              DateTime.sync( 0 );             // set time to zero /*Restoring Time  after every power failure*/              restoreDay = EEPROM.read(10); //Read days passed       restoreTime = EEPROM.read(0); //read hours passed       if (restoreDay !=0) {         restoreTime = (restoreTime*3600+(restoreDay-1)*86400); //add days and hours to datetima counter       DateTime.sync(restoreTime);       } delay (30); }void loop(){  /* --------- Prints time in hours, minutes and seconds ----------- */ DateTime.available(); // needed to refresh the clock time        /* GENERAL reset pin. Everithing resets to zero when presed*/        if(digitalRead(resetPin) == HIGH){                      for (int u = 0; u < 20; u++)            EEPROM.write(u, 0);            // write a 0 to all 512 bytes of the EEPROM           DateTime.sync( 0 );            // reset time to zero if button pressed             } time_t timeNow = DateTime.now(); time_t customTime = timeNow;       if(digitalRead(addHourPin) == HIGH){         customTime += (86400 * 1); //add 3600 sec  for 1 time(for test,adds a day)         DateTime.sync(customTime);   // increase hour by one (1 day)             }         if(digitalRead(addMinPin) == HIGH){         customTime += (60 * 1);         delay (100);         DateTime.sync(customTime);      // increase min by one        }             //... BACKUP TIME EVERY 1 HOUR...//            if ((DateTime.Minute) == 1 && (DateTime.Second)==10 ) {          eepromTime  = DateTime.Hour;          EEPROM.write(10,eepromDay);          eepromDay = DateTime.Day ;          EEPROM.write(0,eepromTime);         } // lcd  PRINT lcd.setCursor(0,0); lcd.print("Hr:min:sec"); lcd.setCursor(12,0); lcd.print(DateTime.Hour,DEC);    // prints total hours lcd.print(":"); lcd.print(DateTime.Minute,DEC);  // and minutes since reset lcd.print(":"); lcd.print(DateTime.Second,DEC);  // and sec since reset lcd.print(" ");                  // clears second digit every minute lcd.setCursor(0,1); lcd.print("Project Day"); lcd.setCursor(13,1); lcd.print(DateTime.Day,DEC);    // prints total days lcd.print(" "); /*test perposes lines*/ lcd.setCursor(0,3); lcd.print("epromDay "); lcd.print(eepromDay); lcd.print(" time "); lcd.print( eepromTime );    lcd.print("  ");  digitalWrite(13, LOW);           //light the LED every second delay (10); digitalWrite(13, HIGH);                 //} `

#### Jimmy60

Individual digits are easy with the library. I was leaving that as an exercise for the OP and being lazy. All my approach really does is start a clock at midnight January 1970. So the time of day it returns is in line with the count up. The reset button can start it over but an extra button would be more instant. It does start with the day at 1 if it was preferred to start at 0 just subtract one from hour().

Here's individual digits.

Code: [Select]
`#include <Time.h>time_t secondCount = 0;time_t lastSecond = 0;void setup(){  setTime(secondCount);     Serial.begin(115200);     // or whatever rate you prefer}void loop(){  secondCount = now();  if (secondCount != lastSecond)  {    Serial.print(day(secondCount));    Serial.print(":");    if (hour(secondCount) < 10) Serial.print("0");      Serial.print(hour(secondCount));    Serial.print(":");      if (minute(secondCount) < 10) Serial.print("0");      Serial.print(minute(secondCount));    Serial.print(":");      if (second(secondCount) < 10) Serial.print("0");      Serial.print(second(secondCount));    Serial.println(" ");    lastSecond = secondCount;  }}`

Go Up

Please enter a valid email to subscribe