Skip to content

Instantly share code, notes, and snippets.

@nmwilk
Created December 22, 2020 16:09
Show Gist options
  • Save nmwilk/1c733e35f2f5f337793ede00c42a7f83 to your computer and use it in GitHub Desktop.
Save nmwilk/1c733e35f2f5f337793ede00c42a7f83 to your computer and use it in GitHub Desktop.
#include <FastLED.h>
#define LED_COUNT 10
CRGB leds[LED_COUNT];
int lastPot = -100;
float col[3];
void setup() {
Serial.begin(115200);
pinMode(0, INPUT);
pinMode(2, OUTPUT);
FastLED.addLeds<NEOPIXEL, 2>(leds, LED_COUNT);
}
void loop() {
int potVal = analogRead(A0);
int diff = abs(potVal - lastPot);
if (diff > 20) {
Serial.println(potVal);
lastPot = potVal;
hsv2rgb(potVal / 730.0, 1.0, 1.0, col);
for(int i=0; i < LED_COUNT; i++) {
leds[i].setRGB(col[0] * 255, col[1] * 255, col[2] * 255);
}
}
FastLED.show();
}
float fract(float x) { return x - int(x); }
float mix(float a, float b, float t) { return a + (b - a) * t; }
float* hsv2rgb(float h, float s, float b, float* rgb) {
rgb[0] = b * mix(1.0, constrain(abs(fract(h + 1.0) * 6.0 - 3.0) - 1.0, 0.0, 1.0), s);
rgb[1] = b * mix(1.0, constrain(abs(fract(h + 0.6666666) * 6.0 - 3.0) - 1.0, 0.0, 1.0), s);
rgb[2] = b * mix(1.0, constrain(abs(fract(h + 0.3333333) * 6.0 - 3.0) - 1.0, 0.0, 1.0), s);
return rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment