I am trying to use my NXT ultrasonic sensor with my picaxe. I am using the code and circuit in the code below but all I get is 255 for the distance. It also works fine with the NXT so the sensor is okay. Any ideas? thanks.
Code:
#picaxe 18x
' J1 External Power Fitted (4.5V)
' J2 SCL Pullup Not fitted (pullup is in sensor)
' J3 SDA Pullup Not fitted (pullup is in sensor)
' H2 9V Power Fitted (9V)
' Lego Ultrasonic Sensor (part 9846)
' Note this sensor is much more difficult than the other
' Lego sensors to use. Consider using the SRF005 instead!
' You must provide both 9V and 5V supplies .
' Also i2c needs to be hacked due to a bug in the ultrasonic i2c firmware
' This means only the ultrasonic sensor can be used at once on the i2c bus
' as we are now using a non-standard i2c protocol
' We recommnd using the SRF005 instead!
symbol distance = b0 'distance in cm
' Wires on NXT jack plug.
' Wire colours may vary. Pin 1 is always end nearest latch.
' 1 White +9V
' 2 Black 0V
' 3 Red 0V
' 4 Green +5V
' 5 Yellow SCL
' 6 Blue SDA
' Do not use i2c pullup resistor - already provided within sensor.
init:
'Slave address is $02
i2cslave $02, i2cslow, i2cbyte
hi2cout $41,($02) 'continuous mode
pause 100
main:
; read values
; This single line should work:
;hi2cin $42,(distance)
; but does not as dodgy firmware in ultrasonic sensor
; therefore here is the workaround
hi2cout ($42) ; address $42
low 4 ; fake an extra pulse
hi2cin (distance) ; read distance
; debug display
debug
; wait 1 second then loop
pause 1000
goto main