Last active
August 7, 2020 20:48
-
-
Save goebish/01afc6bc3c52a678747c4a4590f54674 to your computer and use it in GitHub Desktop.
PWM2LED
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PWM2LED | |
// goebish 2017 | |
// | |
#include <FastLED.h> // (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<NEOPIXEL, WS2812_pin>(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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment