Page 174 - The Unofficial Guide to Lego Mindstorms Robots
P. 174

163


          Immediate and Delayed Gratification

          The functions in Spirit.ocx can be imm ediate, delayed, or both. An immediate function executes as soon as you call it. Delayed
          functions can be placed inside a program and execute d later. For example, Onis both an immediate and delayed function. You
          can call it to turn on some outputs immediately, or you  can call it to add it to a program that will be executed later.

          The following subroutine plays a little song using the  PlayTone function in its immediate mode:

              Sub Charge()
                  With DummySpiritForm.Spirit1
                      .InitComm
                      .PlayTone 392,    10
                      .PlayTone 5  23,  10
                      .PlayTone 659, 1   0
                      .PlayTone 784   , 2 0
                      .PlayTone 659, 10
                      .PlayTone 784, 20
                      .CloseComm
                  End With
              End Sub

          Instead of executing things on the RC X immediately, you can store them for later. The following subroutine redefines Program
          5 on the RCX to play the same so ng:

              Sub ChargeProgram()
                  With Dummy  SpiritForm.Spirit1
                      .I nitComm
                      .SelectPrgm 4
                      .BeginOfTask 0
                           .PlayTon e 392, 10
                           .PlayTone 523, 10
                           .PlayTone 659, 10
                           .PlayTone 784, 20
                           .PlayTone 659, 10
                           .PlayTone 784, 20
                      .EndOfTask
                      Pause 1
                      .CloseComm
                  End With
              End Sub

              Sub Pause(ByVal duration As Integer)
                  Start = Timer
                  Do While Timer < Start + duration
                  Loop
              End Sub
   169   170   171   172   173   174   175   176   177   178   179