Pages: [1] 2   Go Down
Author Topic: 6 digit 7 segment stopwatch not accurate.any one can help me?  (Read 1799 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int latchPin = 8;  // connected to ST_CP
int clockPin = 12;  // connected to SH_CP
int dataPin  = 11;  // connected to DS
byte trigger = 2;   // pin alias

byte paused = 0;
byte started = 0;

 
 

int LED_SEG_TAB[]={
  0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
// 0    1    2    3    4    5    6    7    8    9
int counts = 0;
int modulo;
byte ten_e5,ten_e4, ten_e3, ten_e2, ten_e1, ten_e0;
byte inpinstat;

void setup()
{
  // Hardware setup:
  Serial.begin (115200);

  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(trigger, INPUT);
  
 
  //First test of display:
  ////  Serial.printIn  ("Test displays routine");
  ///test_display ();
}

void loop()
//Reset Button Here
{
 digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e5]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e4]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e3]); // MSD
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e2]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e1]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e0]); // LSD
      digitalWrite(latchPin, HIGH);
// End of reset_button_state == 0
      
    inpinstat = digitalRead(trigger);
    if (inpinstat == HIGH)
    {
      counts++;
      if (counts > 996999)
      {
        counts = 0;
      }    
      ten_e5 = counts / 100000;
      modulo = counts % 100000;
      ten_e4 = counts / 6000;
      modulo = counts % 6000;
      ten_e3 = modulo / 1000;
      modulo = modulo % 1000;
      ten_e2 = modulo / 100;
      modulo = modulo % 100;
      ten_e1 = modulo / 10;
      modulo = modulo % 10;
      ten_e0 = modulo;
      // "slightly unnec.", but stays w/in paradigm
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e5]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e4]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e3]); // MSD
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e2]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e1]);
      shiftOut(dataPin, clockPin, LSBFIRST, LED_SEG_TAB[ten_e0]); // LSD
      digitalWrite(latchPin, HIGH);
      delayMicroseconds(8000);
    }
  }
« Last Edit: March 31, 2014, 09:19:59 am by CrossRoads » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sure someone will be able to help but they'll probably want to see a circuit diagram, code formatted in 'code' tags to make it easier to see and how inaccurate it is.

For simplicity and accuracy I wouldn't use "delayMicroseconds(8000)" and count loops as you do as it depends on the loop time and is far from elegant. I'd use a call to millis() (see reference page) which returns a 'long' int containing the number of milliseconds since the processor started up and takes 50 days to overflow. Then you can update your display with the current time more simply just when it suits you.

Edit:  'counts' is an int so only goes to 32767 so checking if it's > 996,999 is pointless even if it compiles.
You delay for 8ms each time round the loop which could affect accuracy.

« Last Edit: March 22, 2014, 09:16:52 am by sickparrot » Logged

Victoria, Australia
Offline Offline
Edison Member
*
Karma: 46
Posts: 1559
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, please go back and edit your sketch and use code tags, the #  tag in the menu along the top of the editing window.
This will place  your code in a window like this.

Code:
int ones_seconds = 0;
int tens_seconds = 0;
int ones_minutes = 0;
int tens_minutes = 0;
int tenths = 0;
int hundredths= 0;

Okay, hope to help.
Tom....... smiley
Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27099
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like you have the basics of the program down - I think you can delete the first set of shiftOuts, or move them into setup so the display shows 0's on start up.  Then fixup your trigger reading for the start & stop. I use the internal pullup on a pin to hold it high, and wire the switch to connect the pin to Gnd - then a reading of LOW lets the program know the switch has been pressed:
pinMode(trigger, INPUT);////stat/stop.
change to:
pinMode(trigger, INPUT+PULLUP);////stat/stop.
and
if(inPinstat == LOW)


I would also change this line:
previousmillis  = currentmillis;  // save the time for the next comparison
to
previousmillis  = previousmillis + interval;  // save the time for the next comparison
so the increment amount is constant. I also find for time tracking that micros() provides more consistent results than millis(), you can do some longer term time tracking & see what you get.

Next step is getting your logic squared away - if inPinStat == LOW and started == 0 has been pressed, set started to 1 to show that time is running. Next reading, if inPinStat == LOW and started==1, then set started to 0 and stop the time updates.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Victoria, Australia
Offline Offline
Edison Member
*
Karma: 46
Posts: 1559
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, can you post a CAD or picture of a hand drawn circuit diagram, so we can see how you have your start stop buttons wired to the arduino.
Are you using pull up or pull down resistors on the input pins.

Tom.... smiley
PS don't forget code #  tags.
« Last Edit: April 05, 2014, 03:29:54 am by TomGeorge » Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Re:Stopwatch-6 digit with 8 inch 7 segment/74hc595,
Display-Minit/SS/ms,ms,ms.
Using Arduino UNO.


* button_schem.png (25.75 KB, 528x453 - viewed 36 times.)

* 7SegShiftRegisters2.jpeg (109.24 KB, 1017x768 - viewed 48 times.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27099
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at this code that I did a while, ago, Jan 2011, for a lap timer.
Double click the file named stopwatch, the other files will open as tabs within that file.

* stopwatch_23Jan.zip (5.74 KB - downloaded 27 times.)
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13724
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Check the stopwatch class - http://playground.arduino.cc/Code/StopWatchClass -
adding the 7seg code should not be the problem.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mr CrossRoad,
Yes,I alway look at this sketch.
Warren Reeve and yours.
I'm keep trying to finalise the coding.
Thank for your help.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will use the millis coding, 2 minit accurate only.
micros coding can't control the start and stop button,
Maybe need to use 2 button.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27099
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Could be - I haven't looked at that code in ages; my programming has become much better since then.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

good to hear that new.
i still work on it,
to get the best accurrate coding.
if u wanna share the code,
i'm very much thank u to u.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27099
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't rewritten that sketch, I have nothing new to share at the moment.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hahahar....finaly.i'm done the micros coding and the start/stop button.
work hard to get the best result.
maybe I will publish the picture or video's.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27099
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That would be sweet! Let's see it in action!
Can post here as an attachment (Reply, Additonal Actions, Browse to your locally stored file, and Attach it), or upload to youtube and post the link here.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1] 2   Go Up
Jump to: