[Circuit Playground Express]
[Make Code Adafruit]
Change from green to red based on sound loudness.
function lerp(start: number, end: number, amt: number) {
return (1.0 - amt) * start + amt * end;
}
function lerpColor(from: number, to: number, t: number) {
let red = Math.floor(lerp((from & 0xFF), (to & 0xFF), t));
let green = Math.floor(lerp((from & 0xFF00) >> 8, (to & 0xFF00) >> 8, t));
let blue = Math.floor(lerp((from & 0xFF0000) >> 16, (to & 0xFF0000) >> 16, t));
let color = red | (green << 8) | (blue << 16);
return color;
}
function getRed(color: number) {
let red = (color & 0xFF);
return red;
}
function getGreen(color: number) {
let green = (color & 0xFF00) >> 8;
return green;
}
function getBlue(color: number) {
let blue = (color & 0xFF0000) >> 16;
return blue;
}
forever(function () {
let red = 0x0000FF;
let green = 0x00FF00;
light.setBrightness(10);
let t = input.soundLevel() / 255.0;
let color = lerpColor(red, green, 1.0 - t);
light.setAll(light.rgb(getRed(color), getGreen(color), getBlue(color)));
})