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