


Concept:
light changes color based on distance with a 6 foot range.
How to:

Code:
#include <Adafruit_CircuitPlayground.h>
#include <FastLED.h>
// — HC-SR04 pins —
#define TRIG_PIN A2
#define ECHO_PIN A3
#define NUM_LEDS 10
// — Distance range in inches —
const float DIST_MIN_IN = 1.0;
const float DIST_MAX_IN = 72.0;
// — Palette: cool (far) to warm (close) —
CRGBPalette16 distancePalette = CRGBPalette16(
CRGB( 171, 228, 245), // soft blue (far)
CRGB( 149, 208, 252),
CRGB( 87, 137, 255), // blue
CRGB( 65, 82, 232),
CRGB( 51, 28, 255), // bleu fonce
CRGB( 100, 38, 255),
CRGB( 146, 104, 252), // purpleee
CRGB(190, 130, 250),
CRGB(209, 82, 255), // purply magenta
CRGB(238, 82, 255),
CRGB(255, 82, 249), // purplish pink
CRGB(255, 82, 197),
CRGB(250, 15, 172), // hot pink
//CRGB(250, 15, 132),
CRGB(250, 15, 89), //hotter pink…?
// CRGB(250, 15, 54),
CRGB(209, 15, 54), // deep red
CRGB(255, 252, 232) // warm white, soft yelllow (close)
);
float readDistanceIN() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000);
if (duration == 0) return DIST_MAX_IN;
return (duration * 0.00675);
}
void setup() {
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
CircuitPlayground.setBrightness(10); // starting low; we override per-frame
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
float dist = readDistanceIN();
dist = constrain(dist, DIST_MIN_IN, DIST_MAX_IN);
// far=0 (cool/blue), close=255 (warm/amber)
uint8_t paletteIndex = (uint8_t) map(
(long)(dist * 100),
(long)(DIST_MAX_IN * 100),
(long)(DIST_MIN_IN * 100),
0, 255
);
// far=dim, close=bright
uint8_t brightness = (uint8_t) map(
(long)(dist * 100),
(long)(DIST_MAX_IN * 100),
(long)(DIST_MIN_IN * 100),
20, 255
);
CRGB color = ColorFromPalette(distancePalette, paletteIndex, 255, LINEARBLEND);
CircuitPlayground.setBrightness(brightness);
for (int i = 0; i < NUM_LEDS; i++) {
CircuitPlayground.strip.setPixelColor(i, color.r, color.g, color.b);
}
CircuitPlayground.strip.show();
delay(30);
}
^^no more than 16 colors