Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>
//based on https://learn.adafruit.com/chameleon-scarf/overview?view=all#code
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// 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)
const int numPixels = 8;
const int ledPin = 2;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, ledPin, NEO_RGB + NEO_KHZ800);
// our RGB -> eye-recognized gamma color
byte gammatable[256];
int capture_state = 0;
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(115200);
Serial.println("Color learner simple");
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]);
}
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.println();
Serial.println("Running...");
}
uint16_t clear, red, green, blue;
const int max_length = 10;
int recorded[max_length][3];
int recordedLength = 0;
int lastButton = HIGH;
void loop()
{
Serial.println("capture state ");
Serial.println(capture_state);
float r = 255;
float g = 255;
float b = 255;
if (capture_state == 0) {
Serial.println("recording");
//flash to show we're recording
colorWipe(strip.Color(0,0,0), 0);
strip.show(); // Initialize all pixels to 'off'
strip.setPixelColor (0, strip.Color(r, g, b));
/*for (int i=0; i<3; i++){ //this sequence flashes the first pixel three times as a countdown to the color reading.
strip.setPixelColor (0, strip.Color(188, 188, 188)); //white, but dimmer-- 255 for all three values makes it blinding!
strip.show();
delay(1000);
strip.setPixelColor (0, strip.Color(0, 0, 0));
strip.show();
delay(500);
}*/
recordedLength = 0;
for (int i=0; i<3; i++){
Serial.println("counting through "+i);
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; // clear contains RGB already so no need to re-add it
//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();
recorded[i][0] = (int)red;
recorded[i][1] = (int)green;
recorded[i][2] = (int)blue;
Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
// strip.setPixelColor(recordedLength, strip.Color(gammatable[(int)g], gammatable[(int)r], gammatable[(int)b]));
// strip.show();
}
Serial.println("argh");
Serial.println( abs(recorded[0][0]-recorded[2][0]) );
if( (abs(recorded[0][0]-recorded[2][0])<100) &&
(abs(recorded[0][1]-recorded[2][1])<100) &&
(abs(recorded[0][2]-recorded[2][2])<100) ){
Serial.println("setting capture state to 1");
capture_state = 1;
Serial.println("capture state ");
Serial.println(capture_state);
}else{
//back to the beginning
recordedLength = 0;
}
}
if (capture_state == 1) {
Serial.println("PLAYBACK");
Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.println();
colorWipe(strip.Color(gammatable[(int)g], gammatable[(int)r], gammatable[(int)b]), 0);
delay(5000);
capture_state = 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.show();
delay(wait);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.