Stop watch

userjyjyuserjyjy Posts: 30
edited 2018-11-13 - 01:20:32 in BASIC Stamp
This is the code that I have for a stop watch and this is what I'm trying to accomplish

A “Start/Stop”button is pressed once, the chronograph will start counting in seconds from 00 to 59, then goes back to 00 and counts again unless the button is pressed

.c.If the Start/Stop button is pressed again, the counter will stop and will display the time between start and stop.If the button is pressed again, the timer will continue to count upward. Please make the stopwatch as accurate as possible.

d.If the Resetbutton is pressed, it will reset the chronograph to zero.e.If the Start/Stop is pressedagain, the counterwill start counting again.

f.The chronograph will “sleep” and the LED lights will turn off to save energy if the stopwatch is left idle(any time that is not counting, whether on 00 or holding a number) for 20 seconds.

g.To wake up the stopwatch, you will need to press the Reset button. The number 00 will be displayed and the above process is repeated.

I cant seem to get display one to count while display 2 displays 0

'{$PBASIC 2.5}

countdown VAR NIB 'create a variable to hold if stopwatch is being using or not
disp_val VAR NIB 'hold value that seven segment displays show
mili_seconds VAR WORD 'hold the value of calculated 1 second accurate with 1 mili second
disp_1 VAR NIB 'hold value that display on first seven segment display
disp_2 VAR NIB 'hold value that display on second seven segment display

OUTH = %00000000 'clear seven segment display
DIRH = %11111111
'set pins 8-15 to outputs
Setup: 'setup function
countdown = 0 'set countdown to 0
disp_val = 0 'set display value to 0
mili_seconds = 0 'set mili seconds to 0
disp_1 = 0 'set the value of first seven segment to 0
disp_2 = 0 'set the value of second seven segment to 0

Main: 'main function
DO 'create a endless loop
IF(((mili_seconds - (mili_seconds // 100))/100) = 0) THEN 'check two buttons every 100ms
IF(IN0 = 1) THEN 'if start/stop button pressed, then
PAUSE 100 'wait 100ms
mili_seconds = mili_seconds + 100 'add 100ms to mili seconds
IF(countdown = 1) THEN 'if stopwatch is ticking, then
countdown = 0 'stop ticking
ELSE 'if not,
countdown = 1 'start ticking

IF(IN1 = 1) THEN 'if reset button pressed, then
countdown = 0 'stop ticking
disp_val = 0 'set display to 00

'prepair valued for digit 1 and digit 2 based on disp_val
disp_1 = (disp_val - (disp_val // 10) / 10)
disp_2 = disp_val - (disp_1 * 10)

HIGH 3 'turn off second digit
LOW 2 'turn on first digit
LOOKUP disp_1,[ %11100111, %01000100 , %11010011 , 'display 0-9 based on the value of disp_1
%11010110 , %01111100 , %10110110,
%10110111, %11000100 , %11110111,
%11110110 ], OUTH
PAUSE 5 'wait 5ms

HIGH 2 'turn off first digit
LOW 3 'turn on second digit
LOOKUP disp_2, [ %11100111, %01000100 , %11010011 , 'display 0-9 based on the value of disp_2
%11010110 , %01111100 , %10110110,
%10110111, %11000100 , %11110111,
%11110110], OUTH
PAUSE 5 'wait 5ms

mili_seconds = mili_seconds + 10 'add 10ms to mili_seconds

IF(mili_seconds >= 1000) THEN 'if reached 1000ms (1 second), then
mili_seconds = 0 'reset mili second variable to 0
IF(countdown = 1) THEN 'if stopwatch still ticking, then
disp_val = disp_val + 1 'add 1 second to display
IF(disp_val >= 60) THEN 'if display reached to 60s, then
disp_val = 0 'set it to 00



  • You wrote all that code and tested it once?

    Where are you in this project?

    Just starting?
  • userjyjy,

    Do you have a schematic or a wiring that shows which pins are going to which segments?
    Also, are you using a transistor to turn each display on an off through P2 and P3 and which one is connected to which display?
    Are your pushbuttons on P0 and P1 wired as active-high or active-low?
    Are you using a HomeWork board or a Board of Education?

Sign In or Register to comment.