analog vs digital
The most important differences (in terms of object avoidance):
Range:Ultrasonic is accurate to about 5 feet, while IR will be lucky to return 1 foot. (Sorry, Calculon doesn’t process centimeters)
Method of Finding Range:
The ultrasonic returns a value by measuring the time it takes the pulsout sound to bounce off an object. Thus, the number increases with further range. This number can potentially be larger than 255, so you want to use a WORD variable to store it.
IR, on the other hand, measures the amount of voltage returned by the object. The closer the object, the more light is returned, the higher the number. Because this is returned by the analog-to-digital converter, it will be between 0 and 255, so you will use a BYTE variable to store it.
Speed:
The difference between them is negligible. Ultrasonic will theoretically take longer, but it’s a matter of microseconds. An ultrasonic sensor also uses 3 lines of code compared to 1, but that’s not a big deal if it’s a subroutine.
Weaknesses:
Sonar can easily miss soft surfaces and surfaces that angle away from the unit. Carpet can also interfere greatly with a sensor close to the ground.
IR: Many types of plastic surfaces are invisible to IR, while light from TV’s, Monitors, and the Sun are TOO visible.
So?
If you want to swap in an IR sensor for the sonar, simply replace the “get_range” (or whatever it’s called) subroutine with this:
readadc IRpin, b0
where IRpin is your analog sensor, and b0 is the variable you are storing it to.