Quote: /* Test HiTechnic Color Sensor
This program reads the HiTechnic color and displays the results on the screen.
The memory model for the HiTechnic Color Sensor is
Address Type Contents 00 - 07H chars Sensor version number 08 - 0FH chars Manufacturer 10 - 17H chars Sensor type 18 - 3DH bytes Not used 3E, 3FH chars Reserved 40H byte Not used 41H byte Reserved 42H byte Color number 43H byte Red reading 44H byte Green reading 45H byte Blue reading
*/
task main() { int color, red, green, blue;
typedef struct{ byte nMsgSize; byte nDeviceAddress; byte nLocationPtr; byte nCompassMode; } TI2C_Output;
SensorType[S1] = sensorI2CCustomStd; wait10Msec(5); nI2CBytesReady[S1]=0; while(nI2CStatus[S1]== STAT_COMM_PENDING) wait1Msec(2);
byte replyMessage[4];
TI2C_Output sOutput;
while(true) { nxtDisplayTextLine(1, " HiTechnic"); nxtDisplayTextLine(2, "Color Sensor test"); nI2CBytesReady[S1] = 0; sOutput.nMsgSize = 2; // go to read the values from the sensor sOutput.nDeviceAddress = 0x02; sOutput.nLocationPtr = 0x42; sendI2CMsg(S1, sOutput.nMsgSize, sizeof(replyMessage)); // read from port 1
while (nI2CStatus[S1] == STAT_COMM_PENDING) wait1Msec(2);
if (nI2CStatus[S1] == NO_ERR) { readI2CReply(S1, replyMessage[0], sizeof(replyMessage)); //get the color data starting from 0x42
color = (replyMessage[0] & 0x00FF); red = (replyMessage[1] & 0x00FF); green = (replyMessage[2] & 0x00FF); blue = (replyMessage[3] & 0x00FF);
nxtDisplayTextLine(4, "color= %d", color); nxtDisplayTextLine(5, "red val= %d", red); nxtDisplayTextLine(6, "green val = %d", green); nxtDisplayTextLine(7, "blue val = %d", blue); wait1Msec(125); } } } |