the NXT color sensor is currently not so urgently needed because I may use the ev3 color sensor intermediately as a substitute.
What I still don't understand concerning i2c sensors:
How does the program know which address registers or data registers have to be read or written?
In case of the nxt USS in continuous mode I suspect you just need to read raw values at addr reg 0x02,
for the PCF8574 you need to send 2 bytes to addr 0x42 (or optionally switched to 0x70 or 0x72 using different IC settings) to get returned 1 byte which represents the bit mask of pressed (8) touch sensors
for the USS in single shot mode you first write 3 bytes (i.g., write "0x01" to i2caddr=0x02 and regaddr=0x42 {0x02, 0x41, 0x01}) and then read at {i2caddr=0x02, regaddr=0x42} 4 bytes which represent 4 different echoes
Code: Select all
// Lego US sensor in single shot mode
#define MAX_BYTES 4
const byte ping_cmnd[] = {0x2, 0x41, 0x1}; // sensor one-shot-mode command
const byte register_select[] = {0x2, 0x42}; // sensor data register
// Wait until Select button is bumped.
void wait_for_btnpressed()
{
until (ButtonPressed(BTNCENTER, true));
while (ButtonPressed(BTNCENTER, true));
}
// Ping the US sensor when the Select button is bumped.
task main()
{
byte data[MAX_BYTES];
byte data_size = MAX_BYTES;
SetSensorLowspeed(IN_4);
while (true)
{
TextOut(0,0,"press BtnCenter");
wait_for_btnpressed();
ClearScreen();
I2CWrite(IN_4, 0, ping_cmnd); // send the ping command
Wait(MS_50); // give sensor time to do the ping
// Read and display the range data produced by the ping
if (I2CBytes(IN_4, register_select, data_size, data))
{
for (int i = 0; i < MAX_BYTES; ++i)
NumOut(40, 56 - 8 * i, data[i]);
}
else TextOut(0, LCD_LINE1, "Sensor Error");
}
}
and I have no idea what to do if I plug 3 i2c sensors to 1 port like I already did before using a port splitter plus 3 i2c sensors (Lego USS_singleshot, PCF8574, MS RX MMux) and then poll them sequentially.
And then I'm also curious how to use the HT sensor multiplexer which also is an i2c device itself.
Do have an idea?