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);
}
}
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.
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.
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.......
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.
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.
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)
Check the stopwatch class - Arduino Playground - StopWatchClass -
adding the 7seg code should not be the problem.
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.
Could be - I haven't looked at that code in ages; my programming has become much better since then.
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.
I haven't rewritten that sketch, I have nothing new to share at the moment.
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.
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.
This is what I'm doing this far.
VID-20140504-WA0001.mp4 (1.55 MB)
This is what I'm doing this far.
VID-20140503-WA0001.mp4 (530 KB)
Hi zaine, can you go back to your last post and pop your code in # tags mate, makes reading them a lot easier.
Tom.....
The last post is the best result.
do you developed the same timing system?
I save the coding file at my office pc,the pc got virus.
I will post to again later.