Page 145 - The Definitive Guide to Building Java Robots
P. 145

Preston_5564C04.fm  Page 126  Wednesday, October 5, 2005  7:22 AM



                 126    CHAPTER 4  ■  SENSORS



                        main:
                              SERIN 16,16468,main,[WAIT(100), cmd]
                            IF cmd = 101 THEN single_switch
                            IF cmd = 102 THEN multi_switch
                            PAUSE 5
                            GOTO main



                        single_switch:
                          IF switch1 = 0 THEN
                            SEROUT 16,N9600,["0", CR]
                            GOTO main
                          ENDIF
                          IF switch1 = 1 THEN
                            SEROUT 16,N9600,["1", CR]
                            GOTO main
                          ENDIF
                        multi_switch:
                          IF m_switch1 = 0 THEN
                            SEROUT 16,N9600,["0", CR]
                          ENDIF
                          IF m_switch1 = 1 THEN
                            SEROUT 16,N9600,["1", CR]
                          ENDIF
                          IF m_switch2 = 0 THEN
                            SEROUT 16,N9600,["0", CR]
                          ENDIF
                          IF m_switch2 = 1 THEN
                            SEROUT 16,N9600,["1", CR]
                          ENDIF
                          IF m_switch3 = 0 THEN
                            SEROUT 16,N9600,["0", CR]
                          ENDIF
                          IF m_switch3 = 1 THEN
                            SEROUT 16,N9600,["1", CR]
                          ENDIF
                          GOTO main
                            The corresponding Java class for this BASIC Stamp program is SwitchStamp. The class has
                        static fields for enumeration of the commands and the switches connected to the stamp pins.
                            The getSingle() method looks for a single character “1” or “0” to be returned from the
                        program. As for the two methods getMulti(): one takes an index parameter that returns a specific
                        reading, and the other returns the entire string of ones or zeroes.
                            In the main method(), I show the examples of usage of each of these methods (see
                        Example 4-5).
   140   141   142   143   144   145   146   147   148   149   150