// PWM2LED // goebish 2017 // #include // (lib available at https://github.com/FastLED/FastLED/archive/master.zip ) // see https://github.com/FastLED/FastLED/wiki/Pixel-reference for reference // settings #define NUM_LEDS 12 #define WS2812_pin 4 // ws2812 stripe Din #define PWM_pin 3 // throttle PWM input #define PWM_MIN 1000 #define PWM_NEUTRAL 1445 #define PWM_MAX 2000 // FastLed hue chart: http://i.imgur.com/0utnZcB.jpg #define BRAKE_HUE 0 // red #define NEUTRAL_HUE 160 // blue #define ACCELERATE_HUE 96 // green // end settings CRGB leds[NUM_LEDS]; void setup() { // init PWM input pinMode(PWM_pin, INPUT_PULLUP); // init leds FastLED.addLeds(leds, NUM_LEDS); FastLED.showColor(CRGB::Black); delay(100); } void loop() { // set leds hue according to throttle value uint8_t hue; uint16_t pwm = pulseIn(PWM_pin, HIGH); if (pwm >= PWM_NEUTRAL) { // accelerate hue = map(pwm, PWM_NEUTRAL, PWM_MAX, NEUTRAL_HUE, ACCELERATE_HUE); } else { // brake hue = map(pwm, PWM_NEUTRAL, PWM_MIN, NEUTRAL_HUE, BRAKE_HUE); } FastLED.showColor(CHSV(hue, 255, 255)); delay(25); }