Ultra Sound Sensor goes PING, PING…
NXT Repository/Programming/
NXT Repository/Tutorials/
One of simplest, yet quite cool, stuff you can do with the I2C Write block is work with your US sensors in “ping” mode, i.e. measure distance by command instead of continous operation. Let’s look at the following sample NXT-G program (click to expand):
The first US Block initializes the US sensor (i.e. set’s the sensor to LOWSPEED_9V and RAWMODE), and performs a read call. Since this is the first read call in the program, it also sets the sensor to work in continous mode. Any other US sensor blocks afterwards DON’T do either of these operations, only read from the US sensor ‘measurement 0′ byte (i.e. distance of closest object). The first time the I2C Write in the loop is called it turns the sensor to work in ping mode, ping once and store the measurements (the US sensor measures the 8 closest distances each time, see below). Next times, it just tells the sensor to ping. You can use the “regular” US Sensor block to read the last distance stored. Try changing the time delay on the Wait block and see how this works. The last I2C block returns the sensor to the “regular” mode, just to be extra safe…
Now for something more exciting - as I said the US sensor measures 8 distances. The US Sensor block gives access only to the first (closest) object. Extending the previous program we now use the I2C Read to read and display the remaining 7 distances:
Last, but not least, we can now work with two US on the same NXT with alternating pinging. First we should set both sensors:

and then we loop, pinging both sensors (a delay between sensors may be needed to allow echos die out before second sensor fires, in my tests it didn’t show any effect) and showing measurements:


