前言
参考
https://www.bilibili.com/read/cv14207304/
硬件:
esp8266或者nodeMCU
TCS3472模块
ws2812灯带
软件:
arduino IDE
库 : fastled库
TCS库链接
https://pan.baidu.com/s/1yARU2xpdoVXaYBItTL5J2A
提取码:flx5
接线图:(仅适用于本文)
ws2812 ---- esp8266
D0 ----------- GPIO15(或者nodeMCU D 8)
VCC -------- vcc
GND -------- GND
** TCS3472 ---- esp8266 **
SCL---------- GPIO5(或者nodeMCU D1)
SDA---------- GPIO4(或者nodeMCU D2)
VCC -------- vcc
GND -------- GND
程序
#include <FastLED.h>
#include "MH_TCS34725.h"
#define LED_PIN 15
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define NUM_LEDS 20
#define BRIGHTNESS 200
#define FRAMES_PER_SECOND 60
bool gReverseDirection = false;
CRGB leds[NUM_LEDS];
int t =10000;
MH_TCS34725 tcs = MH_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
delay(3000); // sanity delay
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
}
void loop()
{
uint16_t clear, red, green, blue;
tcs.getRGBC(&red, &green, &blue, &clear);
tcs.lock(); // turn off LED
// Figure out some basic hex code for visualization
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
// Add entropy to random number generator; we use a lot of it.
// random16_add_entropy( random());
for(int i=0;i<=NUM_LEDS;i++)
{
Serial.print(r);
leds[i]=CRGB((int)r,(int)g,(int)b);
FastLED.show(); // display this frame
delay(5);
}
//fill_solid(leds, 20, CRGB::Red);
//FastLED.show(); // display this frame
//FastLED.delay(t / FRAMES_PER_SECOND);
}
#define COOLING 55
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120
void Fire2012()
{
// Array of temperature readings at each simulation cell
static byte heat[NUM_LEDS];
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(7);
heat[y] = qadd8( heat[y], random8(160,255) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
CRGB color = HeatColor( heat[j]);
int pixelnumber;
if( gReverseDirection ) {
pixelnumber = (NUM_LEDS-1) - j;
} else {
pixelnumber = j;
}
leds[pixelnumber] = color;
}
}