Need help converting code to arduino

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Need help converting code to arduino

Postby Evil Genius jr. » Tue Apr 19, 2011 8:37 am

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
Code: Select all
#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)
Code: Select all
#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.
Evil Genius jr.
 
Posts: 26
Joined: Fri Jan 28, 2011 4:25 pm

Re: Need help converting code to arduino

Postby Alan Chatham » Tue Apr 19, 2011 11:04 pm

Take a look at the Wire library examples that come with Arduino (File -> Examples -> Wire -> master_writer). Basically, you'll need to tell Wire to listen for a certain amount of data.
Code: Select all
void loop()
{
  Wire.requestFrom(2, 6);    // request 6 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  {
    char c = Wire.receive(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}


Play around with that, and hopefully that should start to get things working. One thing to note, sometimes I2C addresses will be bit-shifted left or right by 1 (i.e., 0x02 might be accessible in Wire as 0x01 or 0x04) due to some differences in how people quote the address (I2C uses a 7 bit address, with a data direction bit at the end of the byte packet - technical banned).

Hope it helps!
OpenChord.org - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/banned/USB controllers.
Alan Chatham
 
Posts: 143
Joined: Thu Feb 04, 2010 1:30 am

Re: Need help converting code to arduino

Postby Evil Genius jr. » Wed Apr 20, 2011 7:58 am

I just cant seam to get the right address and command's from the picaxe code. I should be able to do the arduino code. I just don't understand the picaxe code.
Evil Genius jr.
 
Posts: 26
Joined: Fri Jan 28, 2011 4:25 pm

Re: Need help converting code to arduino

Postby Alan Chatham » Sat Apr 23, 2011 3:30 pm

Ahh, I'm seeing the issue - I missed it earlier, but looking at the code, where it talks about how the sensor uses a broken I2C communication method. It's probably going to be difficult to transition that code over to the Arduino, since I don't think that code will work with the Wire library and possibly not with the built-in I2C hardware on the underlying AVR. Something you might want to look into would be a bit-banged I2C library; I think if you search for Jee Labs you should be able to find a firmware I2C library that works with Arduino, and then it'll be a matter of hacking that to include the extra low pulse that the janky sensor needs.
OpenChord.org - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/banned/USB controllers.
Alan Chatham
 
Posts: 143
Joined: Thu Feb 04, 2010 1:30 am

Re: Need help converting code to arduino

Postby franklin97355 » Sat Apr 23, 2011 4:23 pm

You will need to find out more about the sensor, like what is broken and what is the workaround that is not discussed in the code. I would actually take their recommendation and use the srf005
User avatar
franklin97355
 
Posts: 1706
Joined: Mon Apr 21, 2008 1:33 pm


Return to Microcontrollers

Who is online

Users browsing this forum: Google [Bot] and 4 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[108]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[69]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]