Skip to content

Commit 01929f7

Browse files
authored
Add files via upload
1 parent c9524aa commit 01929f7

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// SPDX-FileCopyrightText: 2021 Irete Hamdani for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
//
5+
#include <FastLED.h>
6+
#include <Wire.h>
7+
#include "Adafruit_TCS34725.h"
8+
9+
#if defined(FASTLED_VERSION) && FASTLED_VERSION > 3010001
10+
#error "FastLED 3.10.2 has known compile issues with SAMD boards. Please downgrade to FastLED 3.10.1"
11+
#endif
12+
13+
#define DATA_PIN 1
14+
#define LED_TYPE WS2812B
15+
#define COLOR_ORDER GRB
16+
#define NUM_LEDS 180 // Change this to reflect the number of LEDs you have
17+
#define BRIGHTNESS 255 // Set Brightness here 255
18+
19+
CRGB leds[NUM_LEDS];
20+
21+
// color sensor
22+
// our RGB -> eye-recognized gamma color
23+
byte gammatable[256];
24+
//used to store color sensor received color
25+
uint16_t clear, red, green, blue;
26+
27+
// color sensor
28+
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
29+
30+
void setup() {
31+
delay(3000); // 3 second delay for recovery
32+
33+
// tell FastLED about the LED strip configuration
34+
FastLED.addLeds<LED_TYPE,DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
35+
.setCorrection(TypicalLEDStrip) // cpt-city palettes have different color balance
36+
.setDither(BRIGHTNESS < 255);
37+
38+
// set master brightness control
39+
FastLED.setBrightness(BRIGHTNESS);
40+
41+
tcs.begin();
42+
43+
// helps convert RGB colors to what humans see
44+
for (int i=0; i<256; i++) {
45+
float x = i;
46+
x /= 255;
47+
x = pow(x, 2.5);
48+
x *= 255;
49+
50+
gammatable[i] = x;
51+
}
52+
53+
for (int i=0; i<3; i++){ //this sequence flashes the first pixel three times none/white as a countdown to the color reading.
54+
leds[16] = CRGB::Black;
55+
FastLED.show();
56+
delay(1000);
57+
leds[16] = CRGB::White;
58+
FastLED.show();
59+
delay(500);
60+
}
61+
62+
// turn on LED
63+
tcs.setInterrupt(false);
64+
delay(60); // takes 50ms to read
65+
66+
tcs.getRawData(&red, &green, &blue, &clear);
67+
68+
tcs.setInterrupt(true); // turn off LED
69+
}
70+
71+
void loop()
72+
{
73+
// Figure out some basic hex code for visualization
74+
uint32_t sum = red;
75+
sum += green;
76+
sum += blue;
77+
//sum += clear; // clear contains RGB already so no need to re-add it
78+
79+
float r, g, b;
80+
r = red; r /= sum;
81+
g = green; g /= sum;
82+
b = blue; b /= sum;
83+
r *= 256; g *= 256; b *= 256;
84+
85+
CRGBPalette16 gTargetPalette (
86+
CRGB (gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]));
87+
88+
colorwaves( leds, NUM_LEDS, gTargetPalette);//gCurrentPalette);
89+
90+
FastLED.show();
91+
FastLED.delay(20);
92+
}
93+
94+
// This function draws color waves with an ever-changing,
95+
// widely-varying set of parameters, using a color palette.
96+
void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette)
97+
{
98+
static uint16_t sPseudotime = 0;
99+
static uint16_t sLastMillis = 0;
100+
static uint16_t sHue16 = 0;
101+
102+
uint8_t sat8 = beatsin88( 87, 220, 250);
103+
uint8_t brightdepth = beatsin88( 341, 96, 224);
104+
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
105+
uint8_t msmultiplier = beat8(147); //beatsin88(147, 23, 60); - creates a more dynamic pattern [IH]
106+
107+
uint16_t hue16 = sHue16;//gHue * 256;
108+
uint16_t hueinc16 = beatsin88(113, 300, 1500);
109+
110+
uint16_t ms = millis();
111+
uint16_t deltams = ms - sLastMillis ;
112+
sLastMillis = ms;
113+
sPseudotime += deltams * msmultiplier;
114+
sHue16 += deltams * beatsin88( 400, 5,9);
115+
uint16_t brightnesstheta16 = sPseudotime;
116+
117+
for( uint16_t i = 0 ; i < numleds; i++) {
118+
hue16 += hueinc16;
119+
uint8_t hue8 = hue16 / 256;
120+
uint16_t h16_128 = hue16 >> 7;
121+
if( h16_128 & 0x100) {
122+
hue8 = 255 - (h16_128 >> 1);
123+
} else {
124+
hue8 = h16_128 >> 1;
125+
}
126+
127+
brightnesstheta16 += brightnessthetainc16;
128+
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
129+
130+
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
131+
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
132+
bri8 += (255 - brightdepth);
133+
134+
uint8_t index = hue8;
135+
//index = triwave8( index);
136+
index = scale8( index, 240);
137+
138+
CRGB newcolor = ColorFromPalette( palette, index, bri8);
139+
140+
uint16_t pixelnumber = i;
141+
pixelnumber = (numleds-1) - pixelnumber;
142+
143+
nblend( ledarray[pixelnumber], newcolor, 128);
144+
}
145+
}

0 commit comments

Comments
 (0)