Converting code to arduino

I have this code that is for a picaxe and I would like to convert it to arduino c code. I have very little knowledge of the wire library so I need any help you guys can provide.
Here is the picaxe code

#picaxe 20x2
#no_table
' PICAXE-20X2 sample program for Lego NXT Sensors
' **** AXE216 i2c Experimenter Board Setup ****
' 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
hi2csetup i2cmaster, $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 B.7   ; fake an extra pulse
input B.7   ; back to input
hi2cin  (distance) ; read distance


; debug display
debug
; wait 1 second then loop
pause 1000
goto main

and here is what I have for arduino code which does not work(Nothing comes out of the serial port)

#include <Wire.h>

void setup()
{
  Serial.begin(9600);
  Wire.begin();
   Wire.beginTransmission(0x41);
}

void loop()
{

   
  Wire.send(0x42);             

  digitalWrite(19,LOW);
  pinMode(19,INPUT);

    char c = Wire.receive();
    Serial.print(c);         
  delay(1000);
}

Thanks for the help.

Hello.

What pins on the Arduino are you expecting the I2C to use? When you say “serial port” it sounds like you are referring to the TX and RX pins; the I2C module on the AVR is on the pins the Arduino labels as Analog 4 and 5.

- Ryan

I have it outputting the i2c data over serial and nothing comes out in the terminal.

Sorry, I didn’t understand you were talking about the serial monitor. Do you have some datasheet or website that explains this “non-standard” I2C protocol better?

It looks like you aren’t getting the slave address into your Arduino code. Maybe something like this would work better:

 Wire.beginTransmission(0x02);
 Wire.send(0x41);
 Wire.endTransmission();
...

Wire.beginTransmission(0x02);
 Wire.send(0x42);
...
 Wire.endTransmission();

Do you have a logic analyzer that you can use to look at what your PIC is doing and what your Arduino are doing?

- Ryan

It’s kind of complicated. The part does not have a data sheet it is part of a toy. I don’t actually have a pic so I thought I could convert it to my arduino to use the sensor.
Here is the post were that code is from : picaxeforum.co.uk/showthread … hlight=nxt.
So what your saying is my code should look like this

#include <Wire.h>

    void setup()
    {
      Serial.begin(9600);
      Wire.begin();
     Wire.beginTransmission(0x02);
Wire.send(0x41);
Wire.endTransmission();
    }

    void loop()
    {

       
 
Wire.beginTransmission(0x02);
Wire.send(0x42);

      digitalWrite(19,LOW);
      pinMode(19,INPUT);
Wire.endTransmission();

        char c = Wire.receive();
        Serial.print(c);         
      delay(1000);
    }

I tried the above code using int c instead of char c and got all 0’s displayed.