COunt down timer

Jaden

Senior Member
Count down timer

Hi Folks,
Newby here. I am constructing a countdown timer (from 35) and need a little help interigating the program. I will later add buzzer at the end and interupt switches.

It is bits from the manual,

main: let b1=35
gosub clock
pause 1000
b1 = b1 - 1
goto main

clock: pulsout 1,10
if b1=0 then endclk
for b3 = 1 to b1
pulsout 0,10
next b3
endclk: return

The 7 segment will display '35' but when it pauses 1 second it goes back to 35! and what is b3 = 1 to b1 doing? b3 is just a label and it is saying 1 to 35 until it reaches that value then falls out?

Thanks for your help.
 
Last edited:

marks

Senior Member
Quite clever I like your program!( just move the first bit like this)
thats why its stuck on 35
b3 is just looping to give 35 pulses out or whatever b1 equals.
Code:
let b1=35
main: 
 gosub clock
 pause 1000
 b1 = b1 - 1
 goto main
 
clock: pulsout 1,10
 if b1=0 then endclk
 for b3 = 1 to b1
 pulsout 0,10
 next b3
 endclk: return
 

westaust55

Moderator
Welcome to the PICAXE forum.

The for b3 =1 to b1
:
:
next

is a looping structure

b3 is a index/counter variable to keep track of how many times the loop has been executed
"1" is the start value for the couter
b1 is used as the final value for the counter

as the looping structure increments each time it reaches the next command the internat part executes the correct number of times but when the program rollsout of the loop the b3 value will be 1 greater than the final value.

here is a further tweaked (untested) version
Code:
Init:
	let b1=35
Main: 
 	gosub clock
 	pause 1000
 	b1 = b1 - 1
 	goto Main
 
clock: 
	pulsout 1,10
 	if b1>0 then 
 		for b3 = 1 to b1
 			pulsout 0,10
 		next b3
 	endif
	return
note how the lines have been indented for each level of program structure/looping. This can make the program easier to follow and good practice to get into )along will inclusion of some comemnts.
 

Jaden

Senior Member
Thanks WA guys.

Trying to rack my brains and get that basic programming back! Works great now.

The next issue with it is that the 7 segment now flashes very quickly back to 0 (pulsout 1,10) . is there a way at all to not get the display to return to 0 and simply display only the number intended? What is the need to send the 7 segment back to 0? As I got snippets of this code from p21 of the 'Interfacing Circuits' manual and it was intended just to display a single number, it was intended to count the 4026 from 0 to 4 and display only 4.

If anyone later wants to construct this circuit you must run pin 0 to the second 4026B and pin5 of the second 4026B to pin1 of the first chip to get the numbers the correct way around!
 

westaust55

Moderator
the program line:
pause 1000​
only pauses for 1 second = 1000 msec
so try increasing that time
pause 10000 ; pause for 10 seconds​
then the desired number will be displayed for 10 seconds before you count down to the next value
 

vttom

Senior Member
Thought I'd mention the fact that the test for b1>0 in the "clock" routine is unnecessary since the "b3 = 1 to b1" block won't even get entered if b1<1. Come to think of it, the gosub is entirely unnecessary; just put the clock code in-line. Lastly, simplify things by using the "step -1" argument of the "for" to count down...

Code:
Main:	for b1 = 35 to 0 step -1

		pulsout 1,10
		for b3 = 1 to b1
			pulsout 0,10
		next b3

	 	pause 1000

	next b1
Furthermore... What is this program supposed to do when b1 gets to 0? The original version posted above will "wrap around" to 255 and will keep on counting. Is that really what you want to do? Or did you intend for it to stop when it reaches 0? (My version above stops when it gets to 0.)

The next issue with it is that the 7 segment now flashes very quickly back to 0 (pulsout 1,10) . is there a way at all to not get the display to return to 0 and simply display only the number intended? What is the need to send the 7 segment back to 0? As I got snippets of this code from p21 of the 'Interfacing Circuits' manual and it was intended just to display a single number, it was intended to count the 4026 from 0 to 4 and display only 4.
The reason for returning to 0 is the fact that the 7-seg display counts up by default. You're trying to count down. So what the program is doing each time through is reset the display to 0 and then quickly count back up to one less than the previous value.

What you could do is use an additional output from the PICAXE to control the DISPLAY ENABLE pin of the 4026 (pin3). Set it low when counting, and set it high during the pause (this program assumes pin2 of the PICAXE is connected to DISPLAY ENABLE):


Code:
Main:	for b1 = 35 to 0 step -1

		low 2 ' Disable display while value is changing

		pulsout 1,10
		for b3 = 1 to b1
			pulsout 0,10
		next b3

		high 2 ' Enable display during pause
	 	pause 1000

	next b1
 

Jaden

Senior Member
Thanks vttom, I thought that there seemed to be a lot of unnecessary code there. I am still new and would like to thoroughly understand what the program is doing.

My end goal is to make a 35 second (which will be settable via dip switches) shot clock as used in water polo/basketball. I will have two interrupt buttons, start/pause and reset (although I know eventually I could do it all with one button). When the countdown gets to 0 it will sound a siren and then the switch operator will reset the display to 35 and start the countdown again. Or they can stop and reset it anywhere in the count down.

Thanks for all your valuable help.
 

Jaden

Senior Member
No good sending pin 3 high as it turns the seven segment off! I would suspect that as it is labeld 'Display enable in' (p21 of the picaxe interfacing circuits) holds it high to enable it to turn on the display.

"The enable display input should be high (+Vs) for normal operation. When low it makes outputs a-g low, giving a blank display. The enable out follows this input but with a brief delay."

I will have a look fro some other IC.

It may be a simple solve as one 7 segment is OK but the first digit has a worse flashing of the count up numbers! May go away when I hook up my large LED seven segment display rather than these small displays! Will let you know.


Also the countdown will stop a 1! Should stop at 0, any reasons for this?

Thanks
 
Last edited:

westaust55

Moderator
A simple alternative is to use two (2) CMOS 4511 chips as covered in PICAXE manual 3 page 21. Each requires 4 bits of data - we don't know which PICAXE you are using.

With the 4511 you can convert tha value 35 down to 0 into a BCD number and put that BCD number onto 8 output pins in a single instruction (OUTPINS = byte variable).
That way no need to clear to 0 before counting up to a new value, just output the new value as a BCD number.

UIf you have an X1 or X2 part then the BINTOBCD command is available to you. Otheriwse you will need to use a few more math steps to achieve the same result.
 

vttom

Senior Member
Or, you could get rid of the interface IC and use a PICAXE with enough output pins to drive the 7-seg display directly. You would have to work out some kind of table so you know which segments to turn on for each of the 10 numeric possibilities (0-9).
 

Jaden

Senior Member
I was trying to get away with the 8M but now ralise that if I want to drive a few more devices I will have to go the bigger picaxe, no problem as space is not an issue. I will keep you up to date with what chip I decided to go with.

I will sus out the 4511, I was thinking last night that it would be better to get the IC only to display the value I want and not have to count to it. This IC also has a 'lamp test input' which would be very handy for at the end of the countdown when I sound a buzzer to be able to flash the display as well.

Thanks for the help so far, I have learnt a lot and apreciate that.
 
Top