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

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



                                                                                 CHAPTER 4  ■  SENSORS   125


























                        Figure 4-13. The Sharp IR Sensor GP2D15

                            Now that you’ve seen some of the sensor types (that return only logical values), I’ll describe
                        a BASIC Stamp program that returns single or multiple logic sensor readings, and also explain
                        the Java class that reads it.

                        Code Objective

                        The code objective here is to return single or multiple logic readings from the microcontroller,
                        indicating a true or false.


                        Code Discussion
                        In the first example, I’ll create the BASIC Stamp program (see Example 4-4). For the single
                        switch, I’ll either return a “0” for false or a “1” for true. For reading multiple switches at once, it
                        will return a string of ones and zeroes as soon as the readings are made—thus, when reading
                        from multiple switches, the example output from the BASIC Stamp might be “010” or “111,”
                        these would then correspond to “false, true, false” and “true, true, true,” respectively.

                        Example 4-4. switch.bs2

                        '{$STAMP BS2}
                        ' {$PBASIC 2.5}
                        '{$PORT COM1}

                        cmd           VAR   Byte
                        N9600         CON   16468
                        switch1       CON   1
                        m_switch1     CON   2
                        m_switch2     CON   3
                        m_switch3     CON   4
   139   140   141   142   143   144   145   146   147   148   149