Shop OBEX P1 Docs P2 Docs Learn Events
How can I execute two codes at the same time? — Parallax Forums

How can I execute two codes at the same time?

nowhatevenowhateve Posts: 8
edited 2008-12-01 03:57 in BASIC Stamp
I have a "timer" program counting down from 00:30 to 00:00. There is a button to trigger the counting down. When I press the button, I also wanna music to play. How should I write the code. It seems that codes are done in a sequence, I cannot start the other before the first is done.
Below is part of my code:

IF buttons & Cpress THEN
GOTO DISPLAY
(at this point, I wanna my background music program starts two)

DISPLAY:
C1=A1/60
A1=A1-(C1*60)+1
DO UNTIL((A1=0) AND (C1=0))
LCDOUT LCD, LcdCls, [noparse][[/noparse]"0",DEC C1,":",DEC A1]
PAUSE 1000
IF ((A1=0)AND(C1<>0))THEN
A1=60
C1=C1-1
ENDIF
A1=A1-1
LOOP
LCDOUT LCD, LcdCls, [noparse][[/noparse]"00:0"]

STOP

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-01 03:57
    The quick answer is that you can't. The Stamps can only execute one thing at a time.

    The longer answer is that sometimes you can interleave two tasks. For example, servos require a control pulse roughly every 20ms which is at most 2.5ms in length. This leaves 17.5ms every 20ms that can be used for something else like generating an IR pulse and detecting whether or not it can be detected (to look for a wall or other obstacle) plus doing some periodic decision making. Similarly, when a Stamp is generating musical tones, it can't do anything else, but there might be short pauses between tones when other brief things can be done like update one digit on a display for each tone. The length of each tone can be used to keep track of time during the count down.

    Alternatively, you can use external peripheral controllers to offload time critical functions. The Parallax Servo Controller can handle the periodic control pulses for up to 16 servos. The SoundPAL can play short tunes.
Sign In or Register to comment.