7 segment disp count up program

Knightryder

New Member
Hi there

I am new to picaxe but have created a 7 segment display circuit. I have sucessfully written a program to make it count up using 08M setup(see below)
My aim is to try and replicate a stop watch.

main: let b1 = 9 'give variable b1 the value 9
gosub clock 'call sub-procedure
pause 1000 'wait 1 second
goto main 'loop

clock:pulsout 1,10 'reset display to 0
if b1 = 0 then endclk 'if b1 = 0 then return
for b3 = 0 to b1 'start a for next loop
pulsout 0,10 'pulse clock line
pause 1000 'pause
next b3 'next loop
goto clock 'return from sub-procedure

endclk:return

I would now like to program in a routine that makes it stop counting when i press the 'push to make' switch and for it to reset when i press and hold the ptm switch.

any help would be greatly appreciated

Many thanks
 

hippy

Technical Support
Staff member
Welcome to the PICAXE Forum.

The first thing to do is to work out what your stopwatch will be doing ...

1) Reset the display
2) Wait for 'start' button push
3) Start counting and incrementing display
4) If 'stop' button not pushed keep counting and incrementing display
5) Wait for 'reset' button pushed
6) Start again from 1.

Your design may be a little different to that but once you've decided the design it's a matter of writing the code to implement the design required. That may be modifying an existing program but it's often best to take the knowledge gained from previous programs then write a new program for the new design.

You've got an excellent program which demonstrates a clock which counts up and you are now confident you can control the display itself, reseting it and incrementing it. It may not however be the most appropriate code to modify for what you now need to do.

"Starting again" can seem like a lot of effort and waste but you will likely end up with a better program for that, and it will often actually be quicker than trying to coax another program into doing what you want. Some existing programs can be very difficult to coax into doing what you later want and eventually one gives up and starts again anyway.

For detecting button pushes you need to know which pin you have connected your push button to and then it's a case of using -

If pinX = 0 Then ...
If pinX = 1 Then ...

where X is the pin number you're using, and the 0 or 1 will indicate if your button is pushed or not. So much like your existing "If b0=0 Then ..." but rather than acting on a variable's value you are acting on an input pin's value.
 

Knightryder

New Member
Many thanks for your help.
I have succesfully managed to get my program functioning in a similar manor to a stopwatch by using if pin3 = 1 in the appropiate places. I have also managed to incorperate audio feedback aswel with the use of a piezo.

However i have a seeminly silly question...i have used the picaxe manuals for the generic 7 segment display program and i am using varibles b1 and b3. But i cannot work out what the varible b3 is. The varible b1 is the number displayed on the 7 seg display.

I wonder if you might be able to let me know what you think?

Many thanks

this is part of the program...

'I/O lines (0-4):
'0 - not used
'1 - 7 segment display
'2 - piezo sounder
'3 - push button switch input to initalise or reset
'4 - not used

'variables:
'b1= Number on the 7 segment display
'b3= ?

initial:
let dirs = %00000111

main_1:
let b3 = 0 'give variable b3 the value 0
pulsout 1,10 'reset display to 0
if pin3 = 1 then goto start 'if switch is pressed goto start
goto main_1 'loop

main_2:
let b1 = 9 'give variable b1 the value 9
gosub clock 'call sub-procedure
pause 1000 'wait 1 second
goto main_2 'loop

start:

'piezo "counts down" then 7 segment display counts up in seconds

sound 2,(100,50) 'sound
pause 250 'pause
sound 2,(100,50) 'sound
pause 250 'pause
sound 2,(110,100) 'sound
goto main_2 'goto main_2

clock:
pulsout 1,10 'reset display to 0
if b1 = 0 then endclk 'if b1 = 0 then return
for b3 = 0 to b1 'start a for next loop
pulsout 0,10 'pulse clock line
if pin3 = 1 then goto stop_sound
pause 1000 'pause for 1 second
next b3 'next loop
goto clock 'return from sub-procedure

endclk:
return
 

westaust55

Moderator
The orginal code from PICAXE manual 3 page 21 is:
Code:
This code example uses sub-procedure ‘clock’ to display the digit ‘4’, which is stored in
the variable b1.

[B]‘This is the sub-procedure[/B]
clock: pulsout 1,10 ‘ reset display to 0
if b1 = 0 then endclk ‘ if b1 = 0 then return
[B]for[/B] b3 = 1 to b1 ‘ start a for...next loop
pulsout 0,10 ‘ pulse clock line
[B]next[/B] b3 ‘ next loop
endclk: return ‘ return from sub-procedure

[B]'This is the main code[/B]
main: let b1 = 4 ‘ give variable b1 the value 4
gosub clock ‘ call sub-procedure
pause 1000 ‘ wait 1 second
goto main ‘ loop

In that portion, b3 is (as within your own program) being used as a counter variable for the FOR...NEXT loop.

That sub procedure by the FOR...NEXT loop pulses the 4026 clock pin the number of time equal to the value in b1 which is to be displayed.

Have a read of the FOR...NEXT command structure/syntax in PICAXE manual 2 page 53
basically a variable is needed to keep count of the number of times the program has been through the program loop


As a suggestion, can you please use the [code] and [/code] markers around your program code so that it appears in a smaller sub window thin your post.
It is recommended practice as descripbed in the sticky Read Me First post at the start of the active form area:
http://www.picaxeforum.co.uk/showthread.php?t=7679

Here you have in fact posted in the Programming Editor section which is predominantly for discusions, questions and bug reports specific to the Programming Editor program itself as opposed to questions about programming - For some, I guess the distinction is a fine line.

Best to post general quations here: http://www.picaxeforum.co.uk/forumdisplay.php?f=2
 
Last edited:

hippy

Technical Support
Staff member
endclk:
return
What is the point of this sub? Wouldn't it be easier to just write return when needed, instead of calling endclk?
This is really a left-over from before the PICAXE supported block structured commands. More modern code would show the operation more clearly ...

Code:
Clock:
  PulsOut 1, 10           ' Reset display to 0
  If b1 > 0 then          ' If need to increment the display ...
    For b3 = 1 To b1      '   Number of increments
      PulsOut 0, 10       '   Increment display
    Next
  End If
  Return                  ' Done
 
Top