diff --git a/Adafruit_TCS34725.cpp b/Adafruit_TCS34725.cpp index ff666f6..c71cc2e 100644 --- a/Adafruit_TCS34725.cpp +++ b/Adafruit_TCS34725.cpp @@ -125,26 +125,8 @@ void Adafruit_TCS34725::enable() { AEN triggers an automatic integration, so if a read RGBC is performed too quickly, the data is not yet valid and all 0's are returned */ - switch (_tcs34725IntegrationTime) { - case TCS34725_INTEGRATIONTIME_2_4MS: - delay(3); - break; - case TCS34725_INTEGRATIONTIME_24MS: - delay(24); - break; - case TCS34725_INTEGRATIONTIME_50MS: - delay(50); - break; - case TCS34725_INTEGRATIONTIME_101MS: - delay(101); - break; - case TCS34725_INTEGRATIONTIME_154MS: - delay(154); - break; - case TCS34725_INTEGRATIONTIME_700MS: - delay(700); - break; - } + /* 12/5 = 2.4, add 1 to account for integer truncation */ + delay((256 - _tcs34725IntegrationTime) * 12 / 5 + 1); } /*! @@ -164,8 +146,7 @@ void Adafruit_TCS34725::disable() { * @param gain * Gain */ -Adafruit_TCS34725::Adafruit_TCS34725(tcs34725IntegrationTime_t it, - tcs34725Gain_t gain) { +Adafruit_TCS34725::Adafruit_TCS34725(uint8_t it, tcs34725Gain_t gain) { _tcs34725Initialised = false; _tcs34725IntegrationTime = it; _tcs34725Gain = gain; @@ -239,7 +220,7 @@ boolean Adafruit_TCS34725::init() { * @param it * Integration Time */ -void Adafruit_TCS34725::setIntegrationTime(tcs34725IntegrationTime_t it) { +void Adafruit_TCS34725::setIntegrationTime(uint8_t it) { if (!_tcs34725Initialised) begin(); @@ -288,26 +269,8 @@ void Adafruit_TCS34725::getRawData(uint16_t *r, uint16_t *g, uint16_t *b, *b = read16(TCS34725_BDATAL); /* Set a delay for the integration time */ - switch (_tcs34725IntegrationTime) { - case TCS34725_INTEGRATIONTIME_2_4MS: - delay(3); - break; - case TCS34725_INTEGRATIONTIME_24MS: - delay(24); - break; - case TCS34725_INTEGRATIONTIME_50MS: - delay(50); - break; - case TCS34725_INTEGRATIONTIME_101MS: - delay(101); - break; - case TCS34725_INTEGRATIONTIME_154MS: - delay(154); - break; - case TCS34725_INTEGRATIONTIME_700MS: - delay(700); - break; - } + /* 12/5 = 2.4, add 1 to account for integer truncation */ + delay((256 - _tcs34725IntegrationTime) * 12 / 5 + 1); } /*! diff --git a/Adafruit_TCS34725.h b/Adafruit_TCS34725.h index 260366c..d85a0ee 100644 --- a/Adafruit_TCS34725.h +++ b/Adafruit_TCS34725.h @@ -138,20 +138,48 @@ #define TCS34725_BDATAH (0x1B) /**< Blue channel data high byte */ /** Integration time settings for TCS34725 */ -typedef enum { - TCS34725_INTEGRATIONTIME_2_4MS = - 0xFF, /**< 2.4ms - 1 cycle - Max Count: 1024 */ - TCS34725_INTEGRATIONTIME_24MS = - 0xF6, /**< 24ms - 10 cycles - Max Count: 10240 */ - TCS34725_INTEGRATIONTIME_50MS = - 0xEB, /**< 50ms - 20 cycles - Max Count: 20480 */ - TCS34725_INTEGRATIONTIME_101MS = - 0xD5, /**< 101ms - 42 cycles - Max Count: 43008 */ - TCS34725_INTEGRATIONTIME_154MS = - 0xC0, /**< 154ms - 64 cycles - Max Count: 65535 */ - TCS34725_INTEGRATIONTIME_700MS = - 0x00 /**< 700ms - 256 cycles - Max Count: 65535 */ -} tcs34725IntegrationTime_t; +/* + * 60-Hz period: 16.67ms, 50-Hz period: 20ms + * 100ms is evenly divisible by 50Hz periods and by 60Hz periods + */ +#define TCS34725_INTEGRATIONTIME_2_4MS \ + (0xFF) /**< 2.4ms - 1 cycle - Max Count: 1024 */ +#define TCS34725_INTEGRATIONTIME_24MS \ + (0xF6) /**< 24.0ms - 10 cycles - Max Count: 10240 */ +#define TCS34725_INTEGRATIONTIME_50MS \ + (0xEB) /**< 50.4ms - 21 cycles - Max Count: 21504 */ +#define TCS34725_INTEGRATIONTIME_60MS \ + (0xE7) /**< 60.0ms - 25 cycles - Max Count: 25700 */ +#define TCS34725_INTEGRATIONTIME_101MS \ + (0xD6) /**< 100.8ms - 42 cycles - Max Count: 43008 */ +#define TCS34725_INTEGRATIONTIME_120MS \ + (0xCE) /**< 120.0ms - 50 cycles - Max Count: 51200 */ +#define TCS34725_INTEGRATIONTIME_154MS \ + (0xC0) /**< 153.6ms - 64 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_180MS \ + (0xB5) /**< 180.0ms - 75 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_199MS \ + (0xAD) /**< 199.2ms - 83 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_240MS \ + (0x9C) /**< 240.0ms - 100 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_300MS \ + (0x83) /**< 300.0ms - 125 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_360MS \ + (0x6A) /**< 360.0ms - 150 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_401MS \ + (0x59) /**< 400.8ms - 167 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_420MS \ + (0x51) /**< 420.0ms - 175 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_480MS \ + (0x38) /**< 480.0ms - 200 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_499MS \ + (0x30) /**< 499.2ms - 208 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_540MS \ + (0x1F) /**< 540.0ms - 225 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_600MS \ + (0x06) /**< 600.0ms - 250 cycles - Max Count: 65535 */ +#define TCS34725_INTEGRATIONTIME_614MS \ + (0x00) /**< 614.4ms - 256 cycles - Max Count: 65535 */ /** Gain settings for TCS34725 */ typedef enum { @@ -167,7 +195,7 @@ typedef enum { */ class Adafruit_TCS34725 { public: - Adafruit_TCS34725(tcs34725IntegrationTime_t = TCS34725_INTEGRATIONTIME_2_4MS, + Adafruit_TCS34725(uint8_t = TCS34725_INTEGRATIONTIME_2_4MS, tcs34725Gain_t = TCS34725_GAIN_1X); boolean begin(uint8_t addr, TwoWire *theWire); @@ -175,7 +203,7 @@ class Adafruit_TCS34725 { boolean begin(); boolean init(); - void setIntegrationTime(tcs34725IntegrationTime_t it); + void setIntegrationTime(uint8_t it); void setGain(tcs34725Gain_t gain); void getRawData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c); void getRGB(float *r, float *g, float *b); @@ -198,7 +226,7 @@ class Adafruit_TCS34725 { uint8_t _i2caddr; boolean _tcs34725Initialised; tcs34725Gain_t _tcs34725Gain; - tcs34725IntegrationTime_t _tcs34725IntegrationTime; + uint8_t _tcs34725IntegrationTime; }; #endif diff --git a/examples/integration_time/integration_time.ino b/examples/integration_time/integration_time.ino new file mode 100644 index 0000000..42950a3 --- /dev/null +++ b/examples/integration_time/integration_time.ino @@ -0,0 +1,65 @@ +#include +#include "Adafruit_TCS34725.h" + +/* Example code for the Adafruit TCS34725 breakout library */ + +/* Connect SCL to analog 5 + Connect SDA to analog 4 + Connect VDD to 3.3V DC + Connect GROUND to common ground */ + +/* Initialise with default values (int time = 2.4ms, gain = 1x) */ +// Adafruit_TCS34725 tcs = Adafruit_TCS34725(); + +/* Initialise with specific int time and gain values */ +Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X); + +void setup(void) { + Serial.begin(9600); + + if (tcs.begin()) { + Serial.println("Found sensor"); + } else { + Serial.println("No TCS34725 found ... check your connections"); + while (1); + } + + // Now we're ready to get readings! +} + +void loop(void) { + uint16_t r, g, b, c, colorTemp, lux; + + tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_614MS); + delay(154); // Delay for one old integ. time period (to finish old reading) + delay(615); // Delay for one new integ. time period (to allow new reading) + tcs.getRawData(&r, &g, &b, &c); + Serial.print("Integ. time: 614.4ms "); + Serial.print("R: "); Serial.print(r, DEC); Serial.print(" "); + Serial.print("G: "); Serial.print(g, DEC); Serial.print(" "); + Serial.print("B: "); Serial.print(b, DEC); Serial.print(" "); + Serial.print("C: "); Serial.print(c, DEC); Serial.print(" "); + Serial.println(" "); + + tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_300MS); + delay(615); // Delay for one old integ. time period (to finish old reading) + delay(300); // Delay for one new integ. time period (to allow new reading) + tcs.getRawData(&r, &g, &b, &c); + Serial.print("Integ. time: 300.0ms "); + Serial.print("R: "); Serial.print(r, DEC); Serial.print(" "); + Serial.print("G: "); Serial.print(g, DEC); Serial.print(" "); + Serial.print("B: "); Serial.print(b, DEC); Serial.print(" "); + Serial.print("C: "); Serial.print(c, DEC); Serial.print(" "); + Serial.println(" "); + + tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_154MS); + delay(300); // Delay for one old integ. time period (to finish old reading) + delay(154); // Delay for one new integ. time period (to allow new reading) + tcs.getRawData(&r, &g, &b, &c); + Serial.print("Integ. time: 153.6ms "); + Serial.print("R: "); Serial.print(r, DEC); Serial.print(" "); + Serial.print("G: "); Serial.print(g, DEC); Serial.print(" "); + Serial.print("B: "); Serial.print(b, DEC); Serial.print(" "); + Serial.print("C: "); Serial.print(c, DEC); Serial.print(" "); + Serial.println(" "); +} diff --git a/examples/interrupt/interrupt.ino b/examples/interrupt/interrupt.ino index f3fffdf..760698c 100644 --- a/examples/interrupt/interrupt.ino +++ b/examples/interrupt/interrupt.ino @@ -3,7 +3,7 @@ /* Initialise with specific int time and gain values */ -Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); +Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X); const int interruptPin = 2; volatile boolean state = false; diff --git a/examples/tcs34725/tcs34725.ino b/examples/tcs34725/tcs34725.ino index 56b15f7..88d1467 100644 --- a/examples/tcs34725/tcs34725.ino +++ b/examples/tcs34725/tcs34725.ino @@ -12,7 +12,7 @@ // Adafruit_TCS34725 tcs = Adafruit_TCS34725(); /* Initialise with specific int time and gain values */ -Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); +Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X); void setup(void) { Serial.begin(9600); diff --git a/examples/tcs34725autorange/tcs34725autorange.ino b/examples/tcs34725autorange/tcs34725autorange.ino index 9e18c39..95c5268 100644 --- a/examples/tcs34725autorange/tcs34725autorange.ino +++ b/examples/tcs34725autorange/tcs34725autorange.ino @@ -39,7 +39,7 @@ class tcs34725 { private: struct tcs_agc { tcs34725Gain_t ag; - tcs34725IntegrationTime_t at; + uint8_t at; uint16_t mincnt; uint16_t maxcnt; }; @@ -71,7 +71,7 @@ public: // the start and end of the list. // const tcs34725::tcs_agc tcs34725::agc_lst[] = { - { TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_700MS, 0, 20000 }, + { TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_614MS, 0, 20000 }, { TCS34725_GAIN_60X, TCS34725_INTEGRATIONTIME_154MS, 4990, 63000 }, { TCS34725_GAIN_16X, TCS34725_INTEGRATIONTIME_154MS, 16790, 63000 }, { TCS34725_GAIN_4X, TCS34725_INTEGRATIONTIME_154MS, 15740, 63000 },