Skip to content

Instantly share code, notes, and snippets.

@jamesabruce
Created March 10, 2016 10:02
Show Gist options
  • Save jamesabruce/f053800ab4a310eae7b8 to your computer and use it in GitHub Desktop.
Save jamesabruce/f053800ab4a310eae7b8 to your computer and use it in GitHub Desktop.
Light-Up Clutch Demo of Element14 Flora Arduino Starter Kit
// things from the color sensor library
#include
#include "Adafruit_TCS34725.h"
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
// including things from light sensor library
#include "TSL2561.h"
TSL2561 tsl(TSL2561_ADDR_FLOAT);
// neopixel library
#include
#ifdef __AVR__
#include <avr/power.h>
#endif
#define BUTTON_PIN 6 // Digital IO pin connected to the button. This will be
// driven with a pull-up resistor so the switch should
// pull the pin to ground momentarily. On a high -> low
// transition the button press logic will execute.
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 9
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 5
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
bool oldState;
int showType = 0;
int delayval = 0; // don't delay for half a second
float brightness;
uint8_t LEDr;
uint8_t LEDg;
uint8_t LEDb;
// for chameleon
// our RGB -> eye-recognized gamma color
byte gammatable[256];
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pixels.begin(); // This initializes the NeoPixel library.
Serial.begin(9600);
delay(1000);
Serial.println("Color View Test!");
// finding color sensor
if (tcs.begin()) {
Serial.println("Found color sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
Serial.println("Light View Test!");
// more stuff from the tsl 2561 example
if (tsl.begin()) {
Serial.println("Found light sensor");
} else {
Serial.println("No light sensor?");
while (1);
}
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
tsl.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
//tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)
// Now we're ready to get readings!
// this is from the chameleon scarf project
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]);
}
for (int i=0; i<3; i++){ //this sequence flashes the third pixel three times as a countdown to the color reading.
pixels.setPixelColor (2, pixels.Color(188, 188, 188)); //white, but dimmer-- 255 for all three values makes it blinding!
pixels.show();
delay(500);
pixels.setPixelColor (2, pixels.Color(0, 0, 0));
pixels.show();
delay(250);
}
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(pixels.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<pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
}
// separate the color values
for(uint16_t i=0; i<pixels.numPixels(); i++) { LEDr =(pixels.getPixelColor(i) >> 16);
LEDg =(pixels.getPixelColor(i) >> 8);
LEDb =(pixels.getPixelColor(i)) ;
pixels.setPixelColor(i, LEDr, LEDg, LEDb);
}
oldState = digitalRead(BUTTON_PIN);
}
void loop() {
/*
//color sensor stuff
uint16_t r, g, b, c, colorTemp, lux;
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
//print colour sensor info
Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
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(" ");
*/
// data from the light sensor
uint16_t x = tsl.getLuminosity(TSL2561_VISIBLE); //data from visible light spectrum only
Serial.println(x, DEC);
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
// more advanced data read example
// stuff for serial monitor light values
Serial.print("IR: "); Serial.print(ir); Serial.print("\t\t");
Serial.print("Full: "); Serial.print(full); Serial.print("\t");
Serial.print("Visible: "); Serial.print(full - ir); Serial.print("\t");
Serial.print("Lux: "); Serial.println(tsl.calculateLux(full, ir));
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
/* brightness = 1;
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, brightness*LEDr, brightness*LEDg, brightness*LEDb);
pixels.show();
}
delay(1000);
brightness = .3;
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, brightness*LEDr, brightness*LEDg, brightness*LEDb);
pixels.show();
}
delay(1000);
*/
/*
for(uint16_t i=0; i<pixels.numPixels(); i++) { if(full>=100){
pixels.setPixelColor(i, .1*LEDr, .1*LEDg, .1*LEDb); // be dim in a bright room
pixels.show();
}
else{
pixels.setPixelColor(i, 1*LEDr, 1*LEDg, 1*LEDb); // be bright in a dark room
pixels.show();
}
}
*/
for(uint16_t i=0; i<pixels.numPixels(); i++) {
if(full<=50){ pixels.setPixelColor(i, LEDr, LEDg, LEDb); // be bright in a dark room pixels.show(); } else{ pixels.setPixelColor(i, .5*LEDr, .5*LEDg, .5*LEDb); // be dimmer in a bright room pixels.show(); } } // stuff from the button cycler example // Get current button state. bool newState = digitalRead(BUTTON_PIN); // Check if state changed from high to low or vice versa(button press). if (newState != oldState) { // Short delay to debounce button. delay(20); // Check if button is still different after debounce. newState = digitalRead(BUTTON_PIN); if (newState != oldState) { showType++; if (showType > 9)
showType=0;
startShow(showType);
}
// Set the lastest button state to the old state.
oldState = newState;
}
/* for(int i=0;i<NUMPIXELS;i++){
// make pixel brightness "breathe"
brightness = 1;
pixels.setPixelColor(i, (brightness*currentcolor));
pixels.show();
delay(1000);
brightness = .5;
pixels.setPixelColor(i, (brightness*currentcolor));
pixels.show();
delay(1000);
}
*/
}
void startShow(int i) {
switch(i){
case 0: colorWipe(pixels.Color(0, 0, 0), 50); // Black/off
break;
case 1: colorWipe(pixels.Color(255, 0, 0), 50); // Red
break;
case 2: colorWipe(pixels.Color(0, 255, 0), 50); // Green
break;
case 3: colorWipe(pixels.Color(0, 0, 255), 50); // Blue
break;
case 4: theaterChase(pixels.Color(127, 127, 127), 50); // White
break;
case 5: theaterChase(pixels.Color(127, 0, 0), 50); // Red
break;
case 6: theaterChase(pixels.Color( 0, 0, 127), 50); // Blue
break;
case 7: rainbow(20);
break;
case 8: rainbowCycle(20);
break;
case 9: theaterChaseRainbow(50);
break;
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 2; q++) {
for (int i=0; i < pixels.numPixels(); i=i+2) {
pixels.setPixelColor(i+q, c); //turn every other pixel on
}
pixels.show();
delay(wait);
for (int i=0; i < pixels.numPixels(); i=i+2) {
pixels.setPixelColor(i+q, 0); //turn every other pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < pixels.numPixels(); i=i+3) {
pixels.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
pixels.show();
delay(wait);
for (int i=0; i < pixels.numPixels(); i=i+3) {
pixels.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel((i+j) & 255));
}
pixels.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment