NXC: read i2c bytes from a Arduino device

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: read i2c bytes from a Arduino device

Post by HaWe »

hey,
I don't get along:
I need to read an array of bytes from an Arduino slave (length 1 byte for the start, later on up to 8 bytes) from a device (devaddr 0x03), no register address.
the device is attached to NXTport S4.
But I don't understand the NXC syntax of all those i2c API commands, nothing matches what I'm looking for.

Now i need a simple command like

Code: Select all

result=I2CReadAddr(port, devaddr, cnt, reply); 

all over, the code would look like this:

Code: Select all

long Readi2cDevice(byte port, byte devaddr, byte cnt, byte &reply){
  byte nByteReady = 0;
  long result = -1;

  while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING){
     Yield();
  }

  result=I2CReadAddr(port, devaddr, cnt, reply);  // <<<<<<<<<<<<<<< how to...?

  if(result!=NO_ERR){
     TextOut(0, LCD_LINE1, "Err:no dev ");   NumOut(78,LCD_LINE1, devaddr);
     PlayTone(100,500);
     Wait(500);
     ClearScreen();
     return result;
  }

}
thanks in advance!
Last edited by HaWe on 17 Jun 2014, 06:36, edited 1 time in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: read i2c bytes from a Arduino device

Post by HaWe »

update 2 -
I found in the NXC code 2 bugs -
1st issue was the return value of i2cbytes,
2nd issue might be the Arduino-7bit vs. NXC-8bit i2c address (to be shifted (??) << 1)

but still not working.
the NXT gets no i2c connection to the Arduino board


a) Arduino:

The Arduino reads each 20ms an external touch sensor at digital pin 2 (Touch at VCC via 100kOhm to GND)
value=digitalRead(signalpin);
result is shown by ledpin 13 (internal LED)
digitalWrite(ledpin,value);

This works so far!


Arduino is defined as devaddr 0x03 (software serial, analog pin 4+5, 2x pullup resistors):
Wire.begin(3);

now the Arduino shall return at i2c-request the signalpin state as byte via i2c-Bus to the master
void requestEvent() {
Wire.write(signalpin); // respond with message of 1 byte
}

Code: Select all

// Arduino slave
// touch sensor with external Pullup at pin digital 2
// i2c as slave, dev addr. 0x03

#include <Wire.h> // serial lib

int  ledpin=13;      // on-board LED
byte signalpin=12;  // digital pin2
byte value=0;  

//*************************************************************************  
void setup()  
{  
  pinMode(ledpin,OUTPUT);    
  pinMode(signalpin,INPUT);
  
  Wire.begin(3);                // join i2c bus with dev address 0x03
  Wire.onRequest(requestEvent); // register event
}  

//*************************************************************************  
  
void requestEvent() {
    Wire.write(signalpin);         // respond with message of 1 byte
}

//*************************************************************************

void loop()  
{  
  value=digitalRead(signalpin);    
  digitalWrite(ledpin,value);

  delay(20);  
}                            


b) NXT:

The NXT master sends i2c messages each 5-20 ms (tried different delays already) to the Arduino (devaddr=3) and shall read the returned value (the touch state).
The NXT screen shall show an i2c error at lcd_line1, and the slave's value at line 2:

Code: Select all



int ArduinoReadi2c(byte port, byte devaddr, byte cnt, byte &values[]){

  byte I2CMsg[];
  byte nByteReady = 0;
  int  result = 0;

  devaddr=devaddr<<1; // ???  Arduino uses upper 7 of 8  bits  ???
  
  ArrayBuild(I2CMsg, devaddr);

  while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING){ Yield(); }

  result=I2CBytes(port, I2CMsg, cnt, values);
  
  if(!result){
     PlayTone(100,500);
     Wait(750);
     ClearScreen();
  }
  return result;
}




task main() {
  int i2cresult;

  byte Arduinoport=S4,
       Arduinoaddr=0x03,
       i2creply[1],
       cnt=1;
  int  value;


  SetSleepTime(0);
  SetSensorLowspeed(S4);

  while(true) {

    i2cresult=ArduinoReadi2c(Arduinoport, Arduinoaddr, cnt, i2creply);  // Arduino 1 byte
    TextOut(0,LCD_LINE1,"i2cresult= ");
    NumOut(60,LCD_LINE1, i2cresult, DRAW_OPT_CLEAR_EOL );
    
    value=i2creply[0];
    TextOut(0,LCD_LINE2,"value0= ");
    NumOut(48,LCD_LINE2, value);

    Wait(50);
  }

}


ps
cable connections:

(GND) NXTred - ArduinoGND_dig + ArduinoGND_ana
(SCL) NXTyellow - Arduino_ana4 - 4.7k - Arduino_VCC+
(SDA) NXTblue - Arduino_ana5 - 4.7k - Arduino_VCC+

Arduino is powered by USB (VCC +5V).
For the pullups I also 47k or 10k or even 4.7 kOhm instead, but in vain, no change.
According to a lejos example I now took 4.7 kOhm pullups.

so what's still faulty?
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests