Go Down

Topic: 6 digit 7 segment stopwatch not accurate.any one can help me? (Read 2152 times) previous topic - next topic

Mar 22, 2014, 06:49 am Last Edit: Mar 31, 2014, 04:19 pm by CrossRoads Reason: 1
Code: [Select]

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);
   }
 }

#1
Mar 22, 2014, 02:44 pm Last Edit: Mar 22, 2014, 03:16 pm by sickparrot Reason: 1
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.


TomGeorge

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: [Select]
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....... :)
Everything runs on smoke, let the smoke out, it stops running......VK3DMK

CrossRoads

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.
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.

TomGeorge

#4
Apr 05, 2014, 10:22 am Last Edit: Apr 05, 2014, 10:29 am by TomGeorge Reason: 1
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.... :)
PS don't forget code #  tags.
Everything runs on smoke, let the smoke out, it stops running......VK3DMK

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

CrossRoads

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.
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.

robtillaart


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

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

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.

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.

CrossRoads

Could be - I haven't looked at that code in ages; my programming has become much better since then.
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.

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.

CrossRoads

I haven't rewritten that sketch, I have nothing new to share at the moment.
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.

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.

CrossRoads

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.
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.

Go Up