problem with i2c

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

problem with i2c

Postby lalos » Thu Jan 23, 2014 11:50 am

Hello,

I am connecting the NXT with an arduino and i am trying to read the data with i2c in order to use the data sent by the arduino correctly.

My problem is that i can not receive any data.

I have proved this code to see what is doing the NXT:

Code: Select all

import lejos.nxt.*;

public class prueba1{

   public static void main(String [] args){

      boolean control = true;
      int contador = 0;

      int address = 0x04; //address del arduino con i2c

      byte[] buff = new byte[3]; //longitud del buffer en bytes

      int nBytes = 3; //Numero de bytes del data

      I2CPort port = (I2CPort) SensorPort.S4;
      I2CSensor sensor = new I2CSensor (port, I2CPort.LEGO_MODE);

      port.setType(SensorConstants.TYPE_HISPEED);
      sensor.setAddress(address);

      while(control){
         try{

            Thread.sleep(900);

         }
         catch(Exception e){

         }

         SensorPort.S4.i2cEnable(0);

         int a = sensor.getData(0x00, buff, nBytes);
         int b = sensor.getData(0x04, buff, nBytes);
         int c = sensor.getData(0x01, buff, nBytes);
         int d = sensor.getData(0x02, buff, nBytes);
         int e = sensor.getData(0x03, buff, nBytes);

         LCD.drawInt(a,0,0);
         LCD.drawInt(b,2,0);
         LCD.drawInt(c, 4, 0);
         LCD.drawInt(d, 6, 0);
         LCD.drawInt(e,8,0);

         LCD.drawInt(sensor.getAddress(), 3,1);

         LCD.drawInt(contador, 0, 4);
         LCD.drawInt(buff[0], 0, 6);
         LCD.drawInt(buff[1], 4, 4);
         LCD.drawInt(buff[2], 4, 2);

         try{

            Thread.sleep(900);

         }
         catch(Exception e1){

         }


         if(Button.ENTER.isPressed()){
            control = false;
         }
         
         contador++;                               
      }
      }
}


in the first 5 LCD.drawInt it shows a -5.

In the 6th one 4, that i the address of the arduino.

and those related with the buffer it receives a 0.

I do not know why is this happening and my biggest problem is the fact that the getData is receiving a negative number.

Can anyone help me please? Thank you in advanced
lalos
New User
 
Posts: 6
Joined: Tue Oct 15, 2013 1:27 pm

Re: problem with i2c

Postby gloomyandy » Thu Jan 23, 2014 12:09 pm

Things you can try....
1. Are you sure your i2c address is correct? I2C address can be very confusing some devices specify a 7bit address some an 8bit address. I would at the very least try address 0x2 and 0x8 in addition to 0x4.
2. Try using a different port to port 4, port 4 has additional hardware attached to it inside the NXT which can sometimes cause problems with i2c.
3. What i2c pull up resistors are you using? Take a look at this article: http://www.dexterindustries.com/howto/c ... -together/
4. Have you tried using the standard Lego software or NXC to talk with your device?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4082
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: problem with i2c

Postby lalos » Thu Jan 23, 2014 12:19 pm

Thank you, i am going to try right now what you have told me.

About the las question the answer is no, because the standard software consumes too much resources and we haven't got enough flash memory with this software. And i haven't tried NXC because i haven't got much idea about C.

I have no idea about what pull up resistor am i using. What importance has it?
lalos
New User
 
Posts: 6
Joined: Tue Oct 15, 2013 1:27 pm

Re: problem with i2c

Postby gloomyandy » Thu Jan 23, 2014 1:59 pm

The pull up resistors are vital and they must be of approximately the correct value otherwise things simply won't work. Do you have any pull up resistors at all?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4082
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 2 guests

cron
more stuff