Skip to content

Instantly share code, notes, and snippets.

@thankthemaker
Created December 24, 2020 09:35
Show Gist options
  • Save thankthemaker/4ddefd4f092d6554faf18a8307edd923 to your computer and use it in GitHub Desktop.
Save thankthemaker/4ddefd4f092d6554faf18a8307edd923 to your computer and use it in GitHub Desktop.
Funwheel fading lights
//#include "FastLED.h"
#include <Adafruit_NeoPixel.h>
#define PIN D1
#define TRIGGER D2
#define DIRECTION D3
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
#define MAX_BRIGHTNESS 25 // allowed values 1-255
#ifndef ADAFRUIT_NEOPIXEL_H
CRGB leds[NUMPIXELS];
#endif
int oldVal = LOW;
int newVal = LOW;
int forward = HIGH;
#ifdef ADAFRUIT_NEOPIXEL_H
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#endif
int delayval = 500; // delay for half a second
void setup() {
pinMode(TRIGGER, INPUT);
#ifdef ADAFRUIT_NEOPIXEL_H
pixels.begin(); // This initializes the NeoPixel library.
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUMPIXELS).setCorrection( TypicalLEDStrip );
#endif
delay(100);
setAll(0);
delay(200);
}
void loop() {
forward = digitalRead(DIRECTION);
newVal = digitalRead(TRIGGER);
if(newVal != oldVal) {
if(newVal == HIGH) {
fadeIn();
} else {
fadeOut();
}
oldVal = newVal;
} else {
if(oldVal == HIGH) {
setAll(MAX_BRIGHTNESS);
} else {
setAll(0);
}
}
delay(20);
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
pixels.setPixelColor(Pixel, pixels.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte brightness) {
if(forward) {
for(int i = 0; i < NUMPIXELS/2; i++ ) {
setPixel(i, brightness, brightness, brightness);
}
for(int i = NUMPIXELS/2; i < NUMPIXELS; i++) {
setPixel(i, brightness, 0, 0);
}
} else {
for(int i = 0; i < NUMPIXELS/2; i++ ) {
setPixel(i, brightness, 0, 0);
}
for(int i = NUMPIXELS/2; i < NUMPIXELS; i++) {
setPixel(i, brightness, brightness, brightness);
}
}
showStrip();
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
pixels.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void fadeIn() {
for(int k = 0; k < MAX_BRIGHTNESS+1; k++) {
setAll(k);
showStrip();
delay(15);
}
}
void fadeOut() {
for(int k = MAX_BRIGHTNESS; k >= 0; k--) {
setAll(k);
showStrip();
delay(15);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment