Picaxe M2 internal timer

Luftwaffle

New member
I am really new to programming with BASIC for the picaxes and I am having some trouble making a timer. I am using a seven-segment display to display a number that will indicate the number of every ten seconds. I have found that the timer works for the 'main' and 'zeroTimer' but it stops working of anything afterwards. the labels 'zero' and 'one' are just subroutines to configure the seven segment display.

Is there any way I can make the internal timer work during all of the subroutines?

main:
setfreq m4
'enabletime
let w0 = Time + 10

zeroTimer:
gosub zero
do
high c.6
pause 250
low c.6
pause 250
loop until pinC.7=1
low c.6
wait 3


oneTimer:
gosub one
'setfreq m4
'let w0 = Time + 10
do
if Time >= w0 then
w0 = Time + 10
goto zeroTimer
endif
loop until pinC.7=1
wait 1
 

Attachments

AllyCat

Senior Member
Hi,

Welcome to the forum. It's better to keep your code Simple and post any code "in-line" to get many replies. ;)

Also it's not clear what pinC.7 and C.6 are required to do, but I think you do not understand how the "time" variable should be used. However, first I've copied part of your file mainly "commenting out" anything that is not necessary:
Code:
#PICAXE 18M2    ; To help other readers know which PICaxe that you're using
Start:
;   setfreq m8                ; Not necessary
symbol Counter = b0      ; Good idea, but not used in the program (and overlays W0 !)
;    LET time = 0             ; Not required here
LET w0 = time + 10      ; was 20, but KISS with setfreq M4
;    goto Main      ; Not required, Falls through anyway
main:
zeroTimer:
high c.6      ; What is this for ?
do
     gosub zero
     loop until pinC.7=1      ; What is this for ?
     low c.6
;     wait 1    
; oneTimer:
;   gosub one
;     ENABLETIME    ; NOT required  (enabled by default)
;     LET time = 0      ;  *** DON'T reset the time !!
; do
     if time >= w0 then
           gosub one
     endif
;    time = 0      ;  *** DON'T reset the time here !!
;    goto zeroTimer      ; Why jump back here ???
;   endif
;    time = time + 1       ; Increments automatically every second
;  loop until pinC.7=1
;  wait 1
;  LOTS SNIPPED OUT HERE
one:
  low b.0 ; A
  high b.1 ; B
  high b.2 ; C
  low b.3 ; D
  low b.4 ; E
  low b.5 ; F
  low b.6 ; G
return
zero:                ; Will not display "0" but a "blank" (all off)
  low b.0 ; A       ;  Probably High is intended for a "0"
  low b.1 ; B       ;  etc.....
  low b.2 ; C
  low b.3 ; D       ; Probably the only Low required for "0"
  low b.4 ; E
  low b.5 ; F
  low b.6 ; G
return
To count every ten seconds the main program basically needs only:
Code:
ZeroTimer:
   time = 0
   gosub zero
   do  : loop until time > 9
   gosub one
   do : loop until time > 19
   gosub two
;   etc.
   do : loop until time > 99
   goto ZeroTimer
;  subroutines here
Cheers, Alan.
 
Last edited:

Luftwaffle

New member
Sorry about my vagueness in the post. I would like to clear up some things with the code:
- C.6 will be used to control an LED
- C.7 is connected to a button (reasons why it is used is further in the post)
- I snipped out a lot of the code because it was just each timer subroutine (e.g. oneTimer) but repeated
- i am using the goto statements to increment the timer when C.7 is inputted.

I am trying to create a timer for a project. A seven-segment will go up by one when C.7 (a button) is pressed. It will also count down every 10 seconds until it reaches zero. Once it reaches zero, C.6 (a LED) will flash.

Also, how do use in-line code in my posts?

Thanks for the help!
 

AllyCat

Senior Member
Hi,

You can put code "inline" by pasting it between [code ] and [/code] tags (no spaces between [ ] ) or by using the forum's "Insert" icon (....) to the right of the "Smileys".

The time variable auto-increments every second, so I believe the following will do approximately what you require. It should run in the Simulator if the additional subroutines are added. Waiting for the button to be released is an alternative to using a PAUSE.
Code:
#PICAXE 18M2
symbol Counter = b0      ; Decrement every 10 seconds, Increment on button press
symbol LED = C.6             ; Output pin
symbol buton = pinC.7    ; Input pin (Button is a reserved word)
do
   if buton = 1 then
      do : loop until buton = 0             ; wait for release
      Counter = Counter + 1
;     time = 0                   ; Optionally reset time period
   endif
   if time > 9 then
       Counter = Counter MIN 1 - 1            ; Min prevents underflow
       time = 0                               ; start another 10 seconds period
   endif
   on Counter gosub zero , one , two , three , four , five , six , seven , eight , nine
   if Counter = 0 then
       High LED
       Pause 500
       Low LED
   endif
loop
zero:
......
return
Cheers, Alan.
 
Top