Page 203 - The Ultimate Palm Robot
P. 203

Color profile: Generic CMYK printer profile
           Composite  Default screen
                                                      Bots / The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 7






                    186     The Ultimate Palm Robot



                               Sub ReadRange()
                                   Dim r as String
                                   Dim numbytes as Integer
                                   Dim readSensorStr as String
                                   Dim err as Integer

                                   range = -1
                                   readSensorStr = "BD1AD1"+chr(13)
                                   err = serialsend(readSensorStr, len(readSensorStr))
                                   numbytes = sysinfo(5)
                                   LabelNumBytesValue.label = str(numbytes)
                                   If (numbytes > 0) Then
                                        SerialReceive(r,numbytes,3.0)
                                        range = val(r)
                                        LabelRangeValue.label = str(range)
                                   EndIf
                                   Return
                               End Sub

                                 The flow of this program is the same as it was before. When the Start button
                               is tapped, the application runs a loop that reads the sensor, and does a little jig
                               if it detects an object with sensor 1. When the Stop button is tapped, the pro-
                               gram ends. Notice that while the functions to open, read, and write the serial
                               port are different in NS Basic than in HotPaw Basic, the concept is still the
                               same. The program opens the port, and reads and writes data to the SV203 as
                               simple text strings. You can find more information on the serial port functions
                               and other functions in the documentation for NS Basic.

                                 Although we will not go through this code line by line, we will point out
                               some of the functions we used. Within the LoopMethod( ) function, the call
                               to the SysEventAvailable( ) function tells us whether the user has tapped a
                               button or control. If this happens, the program assumes that the user wants
                               to stop. Within the ServoControl( ) function, the serialsend(String, Integer)
                               function sends a string to the serial port. This is the function that sends our
                               commands to the SV203. Within the ReadRange( ) function, the sysinfo(5)
                               function calls and returns the number of bytes waiting to be read from the se-
                               rial port. The SerialReceive( ) function reads those bytes from the serial port.
















           P:\010Comp\Bots\880-6\ch07.vp
           Monday, May 12, 2003 4:29:27 PM
   198   199   200   201   202   203   204   205   206   207   208