Go Down

Topic: Stopwatch with Pause/Lap  6 digit 7 seg LED. HELP! (Read 127827 times) previous topic - next topic

Coding Badly


I suspect you'll get a quick response if you...

  • Take a well focused low-resolution picture of the board focusing on the area near the processor
  • Post the picture in the Hardware / Troubleshooting forum
  • Politely ask for someone to identify the clock as either a crystal or a resonator


CrossRoads

This has certainly been a perplexing one Warren.
Robert
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.

CrossRoads

#47
Dec 30, 2010, 06:10 am Last Edit: Dec 30, 2010, 06:11 am by CrossRoads Reason: 1
Warren,
I've adjusted it to use micros() instead millis() (ignore the names) and have it printing the time on screen once a second.
I downloaded a clock that CNET had a link to, to have something to watch that the PC updated once a second.
The screen update seems to update with no discernible change in rate, while the PC clock seems to catch up every once on a while - yet the two times are still in agreement after 37 minutes. Has not gained or lost any time.
I am wondering what I have that has more accurate time - maybe my Garmin Nuvi GPS? Have to see if the battery is charged and compare to that.

Have you decided if you have crystal, such as pictured here next to the USB connector,

or a resonator, which may look like the 3-lead blue part here

or this yellow part


Code: [Select]

unsigned long currentmillis = 0;
unsigned long previousmillis = 0;
unsigned long interval = 10000;
unsigned long elapsedmillis = 0;

byte latchpin = 2; // connect to pin 12 on the 74HC595
byte clockpin = 3; // connect to pin 11 on the 74HC595
byte datapin = 4; // connect to pin 14 on the 74HC595
byte testpin5 = 5;

byte ones_seconds = 0;
byte tens_seconds = 0;
byte ones_minutes = 0;
byte tens_minutes = 0;
byte tenths = 0;
byte hundredths= 0;


int segdisp[10] = {
 63,6,91,79,102,109,125,7,127,111 }; //segment references using 74HC595 Shift Registers
//The above numbers light up different segments of a digit

byte time_update = 0;// added new flag
void setup()
{
 pinMode(latchpin, OUTPUT);
 pinMode(clockpin, OUTPUT);
 pinMode(datapin, OUTPUT);
 pinMode(testpin5, OUTPUT);
 Serial.begin(57600);
}

void loop()
{
 currentmillis = micros();  // read the time.
 elapsedmillis = currentmillis - previousmillis;

 if (elapsedmillis >= interval) // 10 milliseconds have gone by
 {
   previousmillis  = previousmillis + elapsedmillis;  // save the time for the next comparison
   time_update = 1;
  // if (elapsedmillis>10000){  
  //   Serial.println (ones_seconds,DEC);
  //   Serial.println (elapsedmillis,DEC);  I notice this prints out 10004 a lot, with an occasional 10008
  //}
 
 }
 

 if (time_update == 1){  // no updating if not at 10ms interval, skip this whole section
   // increment the counters, roll as needed, shift the digits out

   time_update = 0; // reset for next pass thru

   hundredths = hundredths +1;

   if (hundredths == 10){
     hundredths = 0;
     tenths = tenths +1;
   }

   if (tenths == 10){
     tenths = 0;
     ones_seconds = ones_seconds +1;
     Serial.print ( tens_minutes,DEC);
     Serial.print (ones_minutes,DEC);
     Serial.print (tens_seconds,DEC);
     Serial.println(ones_seconds,DEC);
   }

   if (ones_seconds == 10){
     ones_seconds = 0;
     tens_seconds = tens_seconds +1;
   }

   if (tens_seconds == 6){
     tens_seconds = 0;
     ones_minutes = ones_minutes +1;
   }

   if (ones_minutes == 10){
     ones_minutes = 0;
     tens_minutes = tens_minutes +1;
   }
   if (tens_minutes == 6){
     tens_minutes = 0;
   }
   // counters are all updated now, just do the shiftout one time here:
   digitalWrite(latchpin, LOW); // send the digits down to the shift registers!
   shiftOut(datapin, clockpin, MSBFIRST, segdisp[hundredths]); // print the % first "hundredths" digit
   shiftOut(datapin, clockpin, MSBFIRST, segdisp[tenths]); // print the tens of hundredths digit
   shiftOut(datapin, clockpin, MSBFIRST, segdisp[ones_seconds]); // print the % first "seconds" digit
   shiftOut(datapin, clockpin, MSBFIRST, segdisp[tens_seconds]); // print the tens of seconds digit
   shiftOut(datapin, clockpin, MSBFIRST, segdisp[ones_minutes]); // print the % first "minute" digit
   shiftOut(datapin, clockpin, MSBFIRST, segdisp[tens_minutes]); // print the tens of minutes digit
   digitalWrite(latchpin, HIGH);

 } // end if time to be updated

} // end void loop
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.

CrossRoads

Okay, am running compared to official us gov't time!
http://www.time.gov/timezone.cgi?Eastern/d/-5/java
Lets see how that matches up.
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.

CrossRoads

Well, its been 2 hrs, 25 minutes, and my duemilanova seems to have gained about a second on the gov't time.
Will let it continue to run overnight & see what happens.
Got a feeling tho that my laptop will go into sleep mode or something, and reset the serial interface and lose track of where it is.
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.

Coding Badly


(2 / (((2 * 60) + 25) * 60)) * 1000000 = 229.885057 ppm  (I think I have that correct)
...seems a bit high for a crystal.

Quote
Got a feeling tho that my laptop will go into sleep mode or something, and reset the serial interface and lose track of where it is.

I have a "Screen Saver Tool" that will help.  When the screen saver is disabled, the tool also prevents the computer from going to sleep or hibernating.  Essentially, it fools the operating system into believing a human is at the keyboard.  If you're interested, send me a PM and we'll figure out a way to get you a copy.

CrossRoads

Timer stayed alive overnight & serial port didn't reset!
Time comparison didn't change much - after running for nearly 8 hours (started at 1:00 AM with a 3 second time difference due a delay in opening the serial port and when the time actually appears on screen), the difference is still small, just 2 seconds.


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.

Warren Reeve

Hi Robert and Codey,
I must be using the crystal clock going by the pictures you put up.. mine is the exact same as the first pic.
I have put your code in and tested it again and although better still runs slow.
I am testing again using another stopwatch and it is still showing a defecate of around 2 secs per 5 or 6 mins.  :-/

Coding Badly


@Warren Reeve & @CrossRoads:

The clock source is immediately above the processor next to the reset button...

(with thanks to westfw)
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285541663
http://www.flickr.com/photos/58843278@N00/5027065281/

Warren Reeve

Yip.. that's the exact 1 I have.  :)
So what does this mean?? I assume I have a crystal.. does this mean it can't/won't be accurate with it's clock?

CrossRoads

Warren,
Did you try with the change to micros() instead of millis() ?
I let it run over night, was just 2-3 seconds off after 11 hours when I finally stopped.
Try checking it against the gov't time like I did, see how it looks.
Robert
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.

Warren Reeve

Hi Robert, yes I did.. posted the result up;
Quote
I am testing again using another stopwatch and it is still showing a defecate of around 2 secs per 5 or 6 mins.

Using the micros() instead of millis(). I tried it over approx 1 hour and it was (I think) 11 seconds out?? That was also on another stopwatch. I have used 4 different stopwatches up to now which all run the same. For some reason my Uno & code run slower?? However, when I add the 7 hundredths each loop it appears accurate to approx 1 sec per hour.

Coding Badly

Quote
Yip.. that's the exact 1 I have.

Just so there is no confusion, you have this exact Arduino...
http://www.flickr.com/photos/58843278@N00/5027065281/

Quote
So what does this mean??

If you float your mouse over the picture, you'll notice that there are labels.  westfw believes that board has a RESONATOR, "Layout permits either resonator or crystal.  Here we have a resonator (I think.)"

Quote
I assume I have a crystal.. does this mean it can't/won't be accurate with it's clock?

If your Arduion does have a resonator then that is very likely the reason why your Arduino's clock is "off".


But, all is not lost.  You have some choices available...

1. You can make adjustments in software like you have been doing.  I believe resonators are sensitive to temperature.  If they really are then you will probably have to vary the adjustment a bit depending on the ambient temperature.  In other words, you may have to calibrate your Arduino right before using it as a lap timer.

2. You may be able to carefully remove the resonator and replace it with a crystal.  I have no idea if anyone has done this successfully or how difficult it may be.

3. You can use a "real time clock" (RTC).  There are shields available that include an RTC.  You could use something like this...

http://macetech.com/store/index.php?main_page=product_info&cPath=5&products_id=8&zenid=17fb9dd4ef706ec6d279ba8eca01ee70

...or buy an RTC integrated circuit in a DIP package.

4. I believe one of the timers can be driven from an external crystal.  You could use a "watch crystal" (and capacitors) to make the timer very accurate.

5. You could live with the inaccuracy.  You said earlier this may cause some conflict so it's probably not the best choice.   ;)


CrossRoads

That photo shows a crystal tho. I have the same on my duemilanove.  I have not seen any resonators in metal cans.

You are definitely running code that adds elapsedmillis(), or interval, back into previousmillis()?

If so,
Coding, could there be a bad batch of crystals out there?

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.

Coding Badly

#59
Dec 30, 2010, 09:33 pm Last Edit: Dec 30, 2010, 09:34 pm by bcook Reason: 1
Quote
If so, Coding, could there be a bad batch of crystals out there?

It seems unlikely but I really have no idea.

Have you been testing with a Uno?  Does it look just like the one in the picture?

Go Up