ROBOTC
Reference
       Ultrasonic Rangefinder                                                Overview
                                                                         The Ultrasonic Rangefinder, pictured left, is
                                                                         used to detect objects at a distance, without the
                                                                         need for the robot to actually contact them. The
                                                                         Ultrasonic Rangefinder uses sound pulses to
                                                                         measure distance, in a similar way to how bats
                                                                         or submarines find their way around. By emitting
                                                                         an ultrasonic pulse and timing how long it takes
                                                                         to hear an echo, the Ultrasonic Rangefinder can
                                                                         accurately estimate how far away the object is.
      The Ultrasonic Rangefinder for VEX
                                                                         This sensor is most often used in obstacle
                                                                         detection. It can detect objects at a range
                                                                         of up to 255 inches (approximately 647
                                                                         centimeters), but this range can be limited
                                                                         in certain environments. Since the Ultrasonic
                                                                         Rangefinder relies on sound waves, any
                                                                         surfaces that absorb or deflect sound, such as
                                                                         cushioned surfaces or sharp angles.
                                                                         When wiring the Ultrasonic Rangefinder to the
                                                                         Cortex, both wires are plugged into adjacent
                                                                         digital sensor ports. For the sensor to work
                                                                         properly, the “INPUT” wire must be in the
                                                                         lower numbered slot, and the “OUTPUT” wire
                                                                         in the higher numbered slot. For example, one
      Example of an Ultrasonic Rangefinder attachment                    working configuration is having the “INPUT”
                                                                         wire in digital port 8, and the “OUTPUT” wire
                                                                         in digital port 9.
      Note: The units that the Ultrasonic Rangefinder uses (inches, centimeters, etc.) are controlled by the
      user in the “Motors and Sensors Setup” window.
© Carnegie Mellon Robotics Academy / For use with VEX robotics systems                                 Ultrasonic Rangefinder • 1
                                                                                                        ROBOTC
          Reference
      Ultrasonic Rangefinder
         Natural Language Sample Code
       Move Forward Until Near
       This code has the robot move forward until the Ultrasonic Rangefinder sees an object within 20 units.
                task main ()			
                {
                  robotType(recbot); //Specifies the robot type
                  forward(63);         // Move forward at speed 63.
                  untilSonarLessThan(20, dgtl8);   // Wait for the Ultrasonic
                								                           //Rangefinder in digital ports
                								                           //8 and 9 to see an object
                								                           //within 20 units
                  stop();              // Stop.
                }
       Move Backward Until Far
       This code has the robot move backward until the Ultrasonic Rangefinder sees that the object is greater
       than 20 units away.
                task main ()			
                {
                  robotType(recbot); //Specifies the robot type
                  backward(63); 		     // Move backward at speed 63.
                  untilSonarGreaterThan(20, dgtl8); // Wait for the Ultrasonic
                								                             //Rangefinder in digital
                								                             //ports 8 and 9 to see an
                								                             //object greater than 20
                								                             //units away
                  stop();               // Stop.
                }
© Carnegie Mellon Robotics Academy / For use with VEX robotics systems                    Ultrasonic Rangefinder • 2