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?