Capteur de couleur TCS347525

Bonjour à tous,

Lors des divers évènements auxquels nous avons participé (MIA, Apérobo), nous avons fait découvrir le capteur de couleur TCS347525 d'Adadafruit combiné à un Neopixel Ring du même fabricant. Notre petit montage de démonstration à attiré l'attention de beaucoup de monde et plusieurs personnes nous ont demandées de partager les infos pour pouvoir le reproduire. Voici donc le montage et le code qui va avec.

Le code :

//Code de micmac.caliban

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>

// Le capteur de couleur se connecte sur les broche SLC et SDA
// Parameter 1 = number of pixels in strip (dans mon cas c'est 16)
// Parameter 2 = pin number (most are valid) (pin 6 dans mon cas)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
int NbresLED = 16; //Nombres de led sur le strip ou anneau néopixel
int PinLED = 6;  // Broche de connection à l'arduino
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NbresLED, PinLED, NEO_GRB + NEO_KHZ800);
//

// our RGB -> eye-recognized gamma color
byte gammatable[256];

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {
Serial.begin(9600);
Serial.println("Color View Test!");

strip.begin();
strip.setBrightness(255);
strip.show(); // Initialize all pixels to 'off'

if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}

// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;

gammatable[i] = x;
//Serial.println(gammatable[i]);
}

uint16_t clear, red, green, blue;

tcs.setInterrupt(false);      // turn on LED

delay(60);  // takes 50ms to read

tcs.getRawData(&red, &green, &blue, &clear);

tcs.setInterrupt(true);  // turn off LED

Serial.print("C:\t"); Serial.print(clear);
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.print(blue);

// Figure out some basic hex code for visualization
uint32_t sum = red;
sum += green;
sum += blue;
sum += clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.println();

Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );
colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.setPixelColor((i+1), 0);
strip.show();
delay(wait);
}
}

void loop() {

uint16_t clear, red, green, blue;

tcs.setInterrupt(false);      // turn on LED

delay(60);  // takes 50ms to read

tcs.getRawData(&red, &green, &blue, &clear);

tcs.setInterrupt(true);  // turn off LED

Serial.print("C:\t"); Serial.print(clear);
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.print(blue);

// Figure out some basic hex code for visualization
uint32_t sum = red;
sum += green;
sum += blue;
sum += clear;

float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.println();

Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );
Serial.println();
colorWipe(strip.Color(min(gammatable[(int)r]*10, 255), min(gammatable[(int)g]*10, 255), min(gammatable[(int)b]*10, 255)), 500*16);
Serial.println(gammatable[(int)r]); Serial.println(gammatable[(int)g]); Serial.println(gammatable[(int)b]);

}
Fred de Caliban