The Museum of HP Calculators

HP Forum Archive 15

[ Return to Index | Top of Index ]

Timer function for repeated delay?
Message #1 Posted by Marcus von Cube, Germany on 20 Jan 2006, 5:59 p.m.

I'm looking for a delay timer function for the HP 41CX or any other HP calculator. I want to create a program that does the following:

 1) Accept a number of rounds
 2) Accept a delay in seconds
 3) Delay execution for the given delay time
 4) Increment round number
 5) If the requested number of rounds is reached
 6)    sound a long beep
 7)    stop
    else
 8)    sound a short beep
 9)    go to (3)

I checked the CX timer functions but couldn't find a match. Can the stopwatch functions be used to implement this?

What is the easiest way to produce different beeps (without synthetics.)?

      
Re: Timer function for repeated delay?
Message #2 Posted by Vassilis Prevelakis on 21 Jan 2006, 1:14 a.m.,
in response to message #1 by Marcus von Cube, Germany

First the easy question:

> What is the easiest way to produce different beeps (without synthetics.)? 

TONE accepts an argument 0 through 9, and it produces different beeps according to the argument, giving you 10 different TONEs. With synthetics you can have around 99 different TONEs.

Now for the difficult question:

If I understand correctly, you essentially want a routine which suspends the execution of a program for n seconds, where n is supplied as a parameter.

In other words something like the Unix sleep n command.

If the stopwatch allowed you to call a function when it crossed zero, this would have been trivial to do. Unfortunately to the best of my knowledge, when the stopwatch timer crosses zero, it just makes some noices until you tell it to go away.

So there are two workarounds:

a) use STOPSW 0 SETSW RUNSW to start the stopwatch and have a loop that repeatedly calls RCLSW and tests it against n. I am not really sure whether this will get you the required resolution though (i.e it may be slow by a second or two).

b) set an alarm in the future. For example

	"^^WAKEUP"
	0
	DATE
	TIME
	increment time by n seconds
	XYZALM
	OFF
	LBL "WAKEUP
	continue execution

This is a simplified algorithm which has a serious bug, namely it does not work near midnight when the date changes during the sleep period. In this case the n + TIME will be greater than 23.59599 and will be rejected by XYZALM.

Adding enough code to ensure that this works in the general case, will increase execution time by many seconds, affecting resolution.

**vp

Edited: 21 Jan 2006, 1:40 a.m.

            
Re: Timer function for repeated delay?
Message #3 Posted by Geir Isene on 21 Jan 2006, 4:01 a.m.,
in response to message #2 by Vassilis Prevelakis

Quote:
This is a simplified algorithm which has a serious bug, namely it does not work near midnight when the date changes during the sleep period. In this case the n + TIME will be greater than 23.59599 and will be rejected by XYZALM.

Not really a big problem (see the Ultimate Alarm Clock how this was solved).

      
Re: Timer function for repeated delay?
Message #4 Posted by Garth Wilson on 21 Jan 2006, 1:52 a.m.,
in response to message #1 by Marcus von Cube, Germany

XYZALM allows you to put in the repeat interval, and starting the string in alpha with ^^ makes it a control alarm that will begin execution at the label whose name follows the ^^ in alpha. That program could do your incrementing, comparing, and beeping, and if it has reached the last one you wanted, clear the alarm to stop the repetition. If there is no label name after the ^^ in alpha when you set the alarm, then execution will resume where it had stopped the last time. The 41 does not need to be off for the alarm to work and take control. The first alarm time may need to be calculated. After that they will keep coming at the interval you specified until you (or your program) clear the alarm. This interval will not depend on execution speed, but you will want the execution to be completed before the alarm comes due the next time. IOW, if you want every 30 seconds, and the program takes 3 seconds to execute every time, and then you add more lines and it takes 8 seconds every time, it doesn't matter. The alarm will still come up every 30 seconds as specified, regardless.

User beeps are numbers 0-9 and are all the same length. Synthetic beeps allow different lengths and pitches, and range from 10-129. I might suggest TONE 7 and TONE 87, which are the same pitch but 7 is about three times as long as 87.

            
Re: Timer function for repeated delay?
Message #5 Posted by Vassilis Prevelakis on 21 Jan 2006, 3:43 a.m.,
in response to message #4 by Garth Wilson

Garth Wilson wrote:
> XYZALM allows you to put in the repeat interval ...

You are right. For some reason I thought Marcus required different timeouts depending on the round. Silly of me.

So, Marcus, if you want the reminder to be every n seconds, do as Garth suggested.

Note, however, that my warning regarding the date is still valid (i.e. make sure that TIME + n <= 23.59599). Luckily this is required only for the first alarm. Repeat alarms will be handled by the Timer Module.

Another issue is how to stop the alarms once the required number of iterations has been reached. On a non-CX with a TIME module, you have to clear the alarm manually. On a 41CX you can use the CLALMA command to clear the alarm.

Finally be careful that the execution time of the program is shorter than the specified time interval. Otherwise the alarm processing routine will be interupted by the next alarm and so on, till you get in a total mess.

**vp

So here is an example program

; REPTMR -- Repeat Timer
;        X iterations (i)
;        Y sleep interval in seconds (ss)

01 LBL "REPTMR 02 STO 00 ; store i in R00 03 RDN ; bring ss to X register 04 1 E4 ; change ss to 0.00ss 05 / 06 "^^RING ; unconditional alarm function 07 0 ; use 0 to indicate that the alarm is due today 08 TIME 09 RCL Z ; bring 0.00ss into X-Register 10 + ; X := TIME + ss seconds ; here you are supposed to check if X > 23.59599 and do smth about it 11 XYZALM ; set the alarm 12 OFF ; wait

13 LBL "RING 14 DSE 00 15 GTO 01

16 BEEP ; final beep 17 CLALMA ; clear alarm 18 OFF ; terminate

19 LBL 01 ; repeat beep 20 TONE 3 21 TONE 3 22 OFF ; wait

                  
Re: Timer function for repeated delay?
Message #6 Posted by Marcus von Cube, Germany on 21 Jan 2006, 4:03 a.m.,
in response to message #5 by Vassilis Prevelakis

Thanks to both of you!

There is an additional difficulty: I want to be able to suspend execution after each beep with a key. Why? The timer is intended to monitor an IQ test with a given number of seconds for each question. Now what if my telephone rings?

I'll probably go for a simpler aproach that requires to hit a key to advance to the next round. This requires to reprogram the timer for each round but is much more flexible for the user. I'll keep you informed - about both the program and my IQ ;-)

Marcus

                        
Re: Timer function for repeated delay?
Message #7 Posted by Vassilis Prevelakis on 21 Jan 2006, 6:08 a.m.,
in response to message #6 by Marcus von Cube, Germany

Marcus von Cube wrote:
> There is an additional difficulty: I want to be able to suspend
> execution after each beep with a key. 

Why didn't you say so from the beginning??????

It changes the whole program design!

In this case I'd use the GETKEYX command (HP-41CX only) which takes the number of seconds to wait for the key as argument.

So you can use this program

; REPTMR -- Repeat Timer
;        X iterations (i)
;        Y sleep interval in seconds (ss)

01 LBL "REPTMR 02 STO 00 ; store i in R00 03 RDN ; bring ss to X register 04 LBL 00 05 VIEW 00 ; (optional) display countdown value 06 TONE 3 ; beep to let user know start of interval 07 GETKEYX ; wait for X seconds 08 X<>0? ; if nothing pressed, skip STOP 09 STOP ; freeze program 10 LASTx ; recover timeout value 11 DSE 00 ; decrement count 12 GTO 00 13 BEEP 14 END

If you press a key while it waits, it will stop (line 09). Pressing R/S will cause it to continue

So as friend used to say tell me what you want to do, not how you think you should do it :-)

**vp

                              
Re: Timer function for repeated delay?
Message #8 Posted by Marcus von Cube, Germany on 21 Jan 2006, 12:57 p.m.,
in response to message #7 by Vassilis Prevelakis

Hi Vassilis,

I was asking for a delay function in the first place. GETKEYX seems to be what I was asking for. I'll modify your solution slightly because I need to restart the round if it's interrupted. I like this aproach because it leaves the control to the program, not to an external timer.

Thanks again to all who replied.

Marcus


[ Return to Index | Top of Index ]

Go back to the main exhibit hall