Skip to content

Instantly share code, notes, and snippets.

@CalvinLogan
Last active May 30, 2021
Embed
What would you like to do?
#include "FastLED.h"
#define NUM_LEDS 43
#define PIXEL_PIN 3
CRGB leds[43];
int tiltPin = 0; // the number of the input pin
int LEDstate = 100; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the following variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 50; // the debounce time, increase if the output flickers
void setup()
{
pinMode(tiltPin, INPUT);
digitalWrite(tiltPin, HIGH); // turn on the built in pull-up resistor
pinMode(PIXEL_PIN, OUTPUT);
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_LEDS);
FastLED.setCorrection(Typical8mmPixel);
FastLED.setTemperature(MercuryVapor);
}
void loop() {
int switchstate;
reading = digitalRead(tiltPin);
// If the switch changed, due to bounce or pressing...
if (reading != previous) {
// reset the debouncing timer
time = millis();
}
if ((millis() - time) > debounce) {
// whatever the switch is at, its been there for a long time
// so lets settle on it!
switchstate = reading;
// Now invert the output on the pin 0 LED
if (switchstate == HIGH) {
LEDstate = 255;
FastLED.setBrightness(LEDstate);
} else {
LEDstate = 0;
FastLED.setBrightness(LEDstate);
}
}
// Save the last reading so we keep a running tally
previous = reading;
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::IndianRed;
gammaCorrection();
FastLED.show();
}
void gammaCorrection(){
for (uint16_t i = 0; i < NUM_LEDS; i++) {
leds[i].r = dim8_video(leds[i].r); // gamma correction function for more nuanced color output
leds[i].g = dim8_video(leds[i].g);
leds[i].b = dim8_video(leds[i].b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment