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

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



                 128    CHAPTER 4  ■  SENSORS



                            public boolean getMulti(int index) throws Exception{
                                String i = getMulti().substring(index);
                                if (i.equalsIgnoreCase("1")) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }

                            public static void main(String[] args) {
                                try {
                                    // since i am testing at my desk and not on my robot
                                    SwitchStamp s = new SwitchStamp(SingleSerialPort.getInstance(1));
                                       // get single switch
                                    System.out.println("Single Switch = " + s.getSingle());
                                    // get multiple readings
                                    System.out.println("Multiple Switches = " + s.getMulti());
                                    // get proximity switch from multiple readings
                                    System.out.println("Proximity Sensor = " +
                        s.getMulti(SwitchStamp.PROXIMITY_SENSOR));

                                } catch (Exception e) {
                                    e.printStackTrace();
                                    System.exit(1);

                                }
                            }
                        }

                        Section Summary

                        In this section, I discussed a few types of sensors that give logical readings to the microcontroller
                        and ways to access those readings from a Java class. The programs created were

                            • switch.bs2: The BASIC Stamp program that reads the logic sensor values.
                            • SwitchStamp.java: Gets the sensor data from the switch.bs2 program.
                            The next type of sensor I’ll discuss are the distance sensors. Like the proximity sensor,
                        which gives a logical reading, these sensors provide an actual distance reading from the sensor
                        to an object it detects.
   142   143   144   145   146   147   148   149   150   151   152