Creating a digital watch with stopwatch function.

Hey, im trying to create a digital watch on arduino for an exam project and are really struggling with how to create a stopwatch that can run simultanious with the original clock. So far i managed to use time.h in order to have a normal timer on the lcd display, but i want to create a timer with start/stop function that start at 00:00:00 and can be restartet on command like a normal digital watch. However after spending several day trying to figure this problem out i havnt been able to find any good examples on how to do so.

Can you help me out with some idea's, examples etc. on how to solve my problem? =)

#include <Time.h> //Inkuderer time bibloteket  http://playground.arduino.cc/code/time
#include <IRremote.h> //Inkluderer IRremote bibloteket http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html
#include <LiquidCrystal.h> //Inkluderer LiquidCrystal bibloteket http://arduino.cc/en/Tutorial/LiquidCrystal

#define prev 0xFF22DD //Define er for å definere de forskjellige knappene på en måte som gjør det mer oversiktig og enklere å programere etter ønske
#define next 0xFF02FD
#define vol1 0xFFE01F
#define vol2 0xFFA857
#define play 0xFFC23D
#define CH1 0xFFA25D
#define CH2 0xFF629D
#define CH3 0xFFE21D

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Setter lcd input til 12,11,5,4,3,2.
const int IR_RECIEVER = 10; //Setter IR input mottak til 10.
IRrecv irrecv(IR_RECIEVER);
decode_results results;



void setup()
{
  lcd.begin(16, 2); 
  irrecv.enableIRIn(); //Starter mottakeren.
}

void loop()
{
  boolean r = 0;
  int p = 0;
  irDecode(&p,&r);
  while(p==0)
  {
    clockDisplay(); //Viser klokken.
    irDecode(&p,&r); //adressen til og r blir sendt som argument/input
  }
  lcd.clear(); //Brukes for å cleare displayet sånn at man ikke sitter igjen med gamle verdier fra forrige whilie løkke.
  while(p==1)
  {
    timer(); //Viser stoppeklokke.
    irDecode(&p,&r);
  }
  lcd.clear();
  while(p==2)
  {
    setAlarm(); //Viser skjerm for mulighet til å stille alarm.
    irDecode(&p,&r);
  }
  lcd.clear();
}

void clockDisplay()
{
  
  lcd.setCursor(0,1); //Kolonne, rad.
  lcd.print(hour());
  lcd.print(":");
  lcd.print(minute());
  lcd.print(":");
  lcd.print(second());
  lcd.print(" ");
  lcd.setCursor(0,0);
  lcd.print(day());
  lcd.print(" ");
  lcd.print(month());
  lcd.print(" ");
  lcd.print(year());

}

void timer()
{
  time_t t = now(); //Lagrer nåværende verdi.
  lcd.setCursor(0,0);
  lcd.print("Start/Stop");
  lcd.setCursor(0,1); 
  lcd.print(hour(t));
  lcd.print(minute(t));
  lcd.print(second(t));

}

void setAlarm()
{
  lcd.setCursor(0,0);
  lcd.print("Alarm");
  lcd.setCursor(0,1);
  lcd.print("instilling");

}

void irDecode(int *p, boolean *r) //Decoding av input fra fjernkontroll.
{ 
  int nextCase = *p;//  verdien til nextCase setter til verdien som befinner seg i adressen til p
  if (irrecv.decode(&results))
  {
    irrecv.resume(); //Mottar neste verdi.
    switch (results.value) //Mottar verdien fra fjernkontrollen (HEX) som man lager case ut av.
    {
      case next:
        switch (nextCase)
        {
          case 0: *p = 1; break; //Lager løkke for å få knappen til å velge mellom vilken funksjon den skal vise på skjermen.
          case 1: *p = 2; break;
          default: *p=0; break;
        }
        break; //Case next.
        case play:
        *r = !*r;
    }
  }
}

This is my code so far, sorry that the comments are in norwegian =)

check my stopwatch class - Arduino Playground - StopWatchClass -

Thank you, i look though your library code and modyfied it and added seconds, minutes and hours. Was pretty easy going from there =) Thumbs up

welcome, please post your final result for future reference !

robtillaart:
welcome, please post your final result for future reference !

Are you looking for him to give his exam result for using your class? Did you want to know how you did in his exam? :wink:

LOL,

yes I might get a raise from my boss :slight_smile:

Good luck with that! :wink: :wink: :wink: :wink: :wink: :wink:

have finished the code :smiley: only 3 minor bugs left. I will add the complete code with StopWatch.h & .cpp changes sometime during the christmas, i want to change the norwegian comments to english before i post the final result. The program is a digital watch that can be used as a regular clock where you are able to change the time, a stopwatch function with play/pause and reset function, a timed alarm with the setting for the timer for the alarm. all displayed on a lcd screen remotely controlled by a IR controller.

But im having one last issue with the time setting, and that is that i cannot change months and year using the time.h library without changing the day, hour, min and sec setting.

  while(*s == 4)//Month.
  {
    if (irrecv.decode(&results))
    {
      switch (results.value) //Mottar verdien fra fjernkontrollen (HEX) som man lager case ut av.
      {
        case CH3: adjustTime(2592000); break; irrecv.resume();
        case CH1: adjustTime(-2592000); break; irrecv.resume();
      }  
      clockDisplay();
      lcd.setCursor(13,1);
      lcd.print("Mth");
      break;
    }   
  }
  while(*s == 5)//Year.
  { 
    if (irrecv.decode(&results))
    {
      switch (results.value) //Mottar verdien fra fjernkontrollen (HEX) som man lager case ut av.
      {
        case CH3: adjustTime(31536000); break; irrecv.resume();
        case CH1: adjustTime(-31536000); break; irrecv.resume();
      }
      clockDisplay();
      lcd.setCursor(13,1);
      lcd.print("Yrs");
      break;
    }
  }

I know the problem is that i use seconds to adjust the time which get very inacurate while dealing with months and years, but is there a way to change the month and year in a simular manner?

(update):
I've seen a way to change the time using setTime(hr,min,sec,day,month,yr); function, but that means remodeling that entire section of the code, and i'd rather not if there is an alternative =)