@GregAmes
Here is some RGBC code that calibrates fully. Might be a variable or 2 missing, I just clipped this from a bigger segment. Key is that if init routine is called with non-zero (TRUE) parameter it will try to do an auto calibrate based on the ambient light, else it will use presaved values in Capas and Integ arrays.
If you would like to calibrate at a certain level then change the value of THRS to that level.
Shout if more info required.
// ***************** ADJD-S311-CR999 RGB Digital Colour Sensor ************* if{ function rgbdcsInit(init) { const rgbdcsControl = 0x00; const rgbdcsConfig = 0x01; const rgbdcsRed = 0x06; // -? 0x09 (x4) for Red, Green, Blue & Clear const rgbdcsIntRedLo = 0x0A; const rgbdcsIntRedHi = 0x0B; // -> 0x11 (x4) for Red, Green, Blue & Clear const rgbdcsDatRedLo = 0x40; const rgbdcsDatRedHi = 0x41; // -> 0x47 (x4) for Red, Green, Blue & Clear const rgbdcsOffsetRed = 0x48; // -> 0x4B (x4) for Red, Green, Blue & Clear sx1509SetPin(pinRgbSleep, LOW); // Must be set LOW to Disable the RGB Sensor SLEEP sx1509SetDir(pinRgbSleep, ioexpPinDirOut); // rgbdcsCapas <- [15, 15, 15, 15 ]; rgbdcsInteg <- [88, 104, 120, 64 ]; // sunny indirect // rgbdcsCapas <- [15, 15, 15, 15 ]; rgbdcsInteg <- [308, 372, 532, 196 ]; // Inside + cloudy rgbdcsCapas <- [13, 13, 13, 13 ]; rgbdcsInteg <- [1157, 869, 1641, 441 ]; // Early eve + Fluorescent rgbdcsLevel <- [0x0000, 0x0000, 0x0000, 0x0000 ]; // Last Read levels rgbdcsOffset <- [0x0000, 0x0000, 0x0000, 0x0000 ]; // Last read Offset if (init) rgbdcsCalibrate(); // RGB Digital Colour Sensor rgbdcs object else { for (local col = rgbdcsRED; col <= rgbdcsCLEAR; col++) { rgbdcsSetCapas(col, rgbdcsCapas[col]); rgbdcsSetInteg(col, rgbdcsInteg[col]); } } if (debug & debugRGBC) rgbdcsShowConf(); } function rgbdcsCalibrate() { const INTEG = 4; const THRS = 950; local color = 0, integ = 0, capas = 0x0D; for (color = rgbdcsRED; color <= rgbdcsCLEAR; color++ ) { // Now cycle through colors from RED thru CLEAR rgbdcsSetCapas(color, capas); for (integ = INTEG+1; integ < 4096; integ += INTEG) { rgbdcsSetInteg(color, integ); rgbdcsReadSensor(color); if (rgbdcsLevel[color] > THRS) break; } if (rgbdcsLevel[color] < THRS) MessageLog(format("Calibration error Ca=0x%x Col=0x%x, Int=0x%04X, Val=0x%04X", capas, color, integ, rgbdcsLevel[color])); else rgbdcsSetInteg(color, integ - INTEG); // Reset to previous value } // rgbdcsReadOffset(); // rgbdcsReadSensor(); } function rgbdcsSetCapas(color, value) { if ((color > rgbdcsCLEAR) || (color < rgbdcsRED) || (value > 0x0f) || (value < 0x00)) { MessageLog(format("SetCapas: color %d / capas %d OOR", color, value)); return (ERROR); } WriteByte(i2c_als, rgbdcsRed + color, value); rgbdcsCapas[color] = value; } function rgbdcsSetInteg(color, value) { if ((color > rgbdcsCLEAR) || (color < rgbdcsRED) || (value > 0x0FFF) || (value < 0x0000)) { MessageLog(format("SetInteg: color %d / integ %d OOR", color, value)); return (ERROR); } WriteWord(i2c_als, rgbdcsIntRedLo + (color * 2), value); // Write a 12 bit value for Integration Window rgbdcsInteg[color] = value; } function rgbdcsSetupRead(command) { local status = 0; status = WriteByte(i2c_als, rgbdcsControl, command); // Setup for reading of Offset if (status || (status == null)) return ERROR; do {imp.sleep(0.001); // Wait for conversion status = ReadByte(i2c_als, rgbdcsControl); // Done ? if (status == null) // Error ? return ERROR; // Yes, return } while(status) // No, wait more... return status; } function rgbdcsReadSensor(...) { if (rgbdcsSetupRead(0x01)) return ERROR; if (vargv.len() > 0) { local col = vargv[0] & 0x03; rgbdcsLevel[col] = ReadByteS(i2c_als, rgbdcsDatRedLo + (col * 2)); return rgbdcsLevel[col]; } else { rgbdcsLevel[rgbdcsRED] = ReadByteS(i2c_als, rgbdcsDatRedLo + (rgbdcsRED * 2)); rgbdcsLevel[rgbdcsGREEN] = ReadByteS(i2c_als, rgbdcsDatRedLo + (rgbdcsGREEN * 2)); rgbdcsLevel[rgbdcsBLUE] = ReadByteS(i2c_als, rgbdcsDatRedLo + (rgbdcsBLUE * 2)); rgbdcsLevel[rgbdcsCLEAR] = ReadByteS(i2c_als, rgbdcsDatRedLo + (rgbdcsCLEAR * 2)); } return 0; } function rgbdcsReadOffset() { // Light source MUST be turned off before running this.. if (rgbdcsSetupRead(0x02)) // Setup for Offset value read return ERROR; local status = WriteByte(i2c_als, rgbdcsConfig, BIT0); // Acquire the offsets rgbdcsOffset[rgbdcsRED] = Convert2Complement(ReadByte(i2c_als, rgbdcsOffsetRed + rgbdcsRED), 8); rgbdcsOffset[rgbdcsGREEN] = Convert2Complement(ReadByte(i2c_als, rgbdcsOffsetRed + rgbdcsGREEN), 8); rgbdcsOffset[rgbdcsBLUE] = Convert2Complement(ReadByte(i2c_als, rgbdcsOffsetRed + rgbdcsBLUE), 8); rgbdcsOffset[rgbdcsCLEAR] = Convert2Complement(ReadByte(i2c_als, rgbdcsOffsetRed + rgbdcsCLEAR), 8); } function rgbdcsShowConf() { rgbdcsReadSensor(); rgbdcsReadOffset(); MessageLog(format("Capas : R = %02d G = %02d B = %02d C = %02d", rgbdcsCapas[rgbdcsRED], rgbdcsCapas[rgbdcsGREEN], rgbdcsCapas[rgbdcsBLUE], rgbdcsCapas[rgbdcsCLEAR])); MessageLog(format("Integ : R=%04d G=%04d B=%04d C=%04d", rgbdcsInteg[rgbdcsRED], rgbdcsInteg[rgbdcsGREEN], rgbdcsInteg[rgbdcsBLUE], rgbdcsInteg[rgbdcsCLEAR])); MessageLog(format("Offset: R=%04d G=%04d B=%04d C=%04d", rgbdcsOffset[rgbdcsRED], rgbdcsOffset[rgbdcsGREEN], rgbdcsOffset[rgbdcsBLUE], rgbdcsOffset[rgbdcsCLEAR])); MessageLog(format("CurVal: R=%04d G=%04d B=%04d C=%04d", rgbdcsLevel[rgbdcsRED], rgbdcsLevel[rgbdcsGREEN], rgbdcsLevel[rgbdcsBLUE], rgbdcsLevel[rgbdcsCLEAR])); } //}