Skip to content

Instantly share code, notes, and snippets.

@thankthemaker
Created December 23, 2020 18:20
Show Gist options
  • Save thankthemaker/9d62f732945c2eaa19921c2f7ad2cb96 to your computer and use it in GitHub Desktop.
Save thankthemaker/9d62f732945c2eaa19921c2f7ad2cb96 to your computer and use it in GitHub Desktop.
Fading example
#include <Adafruit_NeoPixel.h>
#define PIN D1
#define TRIGGER D2
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
int oldVal = LOW;
int newVal = LOW;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
void setup() {
pinMode(TRIGGER, INPUT);
pixels.begin(); // This initializes the NeoPixel library.
delay(100);
setAll(255, 0, 0);
delay(200);
}
void loop() {
newVal = digitalRead(TRIGGER);
if(newVal != oldVal) {
if(newVal == HIGH) {
fadeIn(0);
} else {
fadeOut(0);
}
oldVal = newVal;
} else {
if(oldVal == HIGH) {
setAll(255, 0, 0);
} else {
setAll(0, 0, 0);
}
}
delay(20);
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUMPIXELS; i++ ) {
pixels.setPixelColor(i, red, green, blue);
}
pixels.show();
}
void showStrip() {
// NeoPixel
pixels.show();
}
void fadeIn(int color ) {
for(int k = 0; k < 256; k++) {
switch(color) {
case 0: setAll(k,0,0); break;
case 1: setAll(0,k,0); break;
case 2: setAll(0,0,k); break;
}
showStrip();
delay(15);
}
}
void fadeOut(int color) {
for(int k = 255; k >= 0; k--) {
switch(color) {
case 0: setAll(k,0,0); break;
case 1: setAll(0,k,0); break;
case 2: setAll(0,0,k); break;
}
showStrip();
delay(15);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment