Skip to content

Instantly share code, notes, and snippets.

@natendaben
Created December 5, 2019 07:12
Show Gist options
  • Save natendaben/96d0f39fe768ac47a2be9c7752a9bf73 to your computer and use it in GitHub Desktop.
Save natendaben/96d0f39fe768ac47a2be9c7752a9bf73 to your computer and use it in GitHub Desktop.
A pulsing effect for Neopixels
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
// Desert - Gold / brown
uint32_t desert = strip.gamma32(strip.Color(255, 183, 0));
// Underwater - Deep blues / dark
uint32_t water = strip.gamma32(strip.Color(0, 32, 194));
// Rainforest - Deep greens / dark
uint32_t forest = strip.gamma32(strip.Color(0, 179, 116));
// Grassland - Light greens / yellows
uint32_t grass = strip.gamma32(strip.Color(234, 255, 143));
// Beach - Light blues / tans
uint32_t beach = strip.gamma32(strip.Color(0, 255, 255));
uint32_t black = strip.gamma32(strip.Color(0, 0, 0));
bool rampUp = false;
bool steady = false;
bool rampDown = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(5);
pinMode(2, INPUT);
}
void loop() {
int switchOn = digitalRead(2);
// Serial.print("rampUp = "); //+ rampUp + " // steady = " + steady + " // rampDown = " + rampDown);
// Serial.println(rampUp);
// if(rampUp==false){
// if(switchOn==HIGH){
// rampUp=true;
// }
// } else { //if rampUp==true
// if (steady==false){ //if needs to rampUp
// strip.setBrightness(5);
// strip.show();
// for(int i=0; i<strip.numPixels(); i++){
// strip.setPixelColor(i, forest);
// strip.show();
// }
// delay(40);
// for(int bright=10; bright<100; bright=bright+5){
// strip.setBrightness(bright);
// strip.show();
// delay(40);
// }
// steady = true;
// }
// else { //if rampUp has happened and steady state
// if(switchOn == LOW){ //if switch has turned off
// rampDown = true;
// steady = false;
// rampUp = false;
// } else {
// for(int bright=100; bright>50; bright=bright-5){
// strip.setBrightness(bright);
// strip.show();
// delay(40);
// }
// for(int bright=50; bright<100; bright=bright+5){
// strip.setBrightness(bright);
// strip.show();
// delay(40);
// }
// }
// }
// }
// if(rampDown = true){
// for(int bright=100; bright>0; bright=bright-5){
// strip.setBrightness(bright);
// strip.show();
// delay(40);
// }
// strip.setBrightness(0);
// strip.show();
// rampDown = false;
// }
if(switchOn==HIGH){
strip.setBrightness(5);
strip.show();
for(int i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, forest);
strip.show();
}
delay(200);
for(int bright=10; bright<100; bright=bright+2){
strip.setBrightness(bright);
strip.show();
delay(200);
}
for(int bright=100; bright>0; bright=bright-2){
strip.setBrightness(bright);
strip.show();
delay(200);
}
strip.setBrightness(0);
strip.show();
}
else{
for(int i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, black);
strip.show();
}
}
// for(int i=0; i<strip.numPixels(); i++){
// strip.setPixelColor(i, desert);
// strip.show();
// }
// delay(500);
// // put your main code here, to run repeatedly:
// setAllStrips(255,255,255);
// delay(500);
// for(int i=50; i>0; i=i-10){
// strip.setBrightness(i);
// delay(500);
// }
}
void setAllStrips(byte red, byte green, byte blue) {
// for each strip
// for each pixel
for (int pixel = 0; pixel < strip.numPixels(); pixel++) {
strip.setPixelColor(pixel, red, green, blue);
}
strip.show(); // Output to strip
}
void fadeToBlack(uint32_t color){
//extract rgb values
byte r = Red(color);
byte g = Green(color);
byte b = Blue(color);
for(int i=0; i<5; i++){
if(r>0){
r = r-20;
}
if(g>0){
g = g-20;
}
if(b>0){
b = b-20;
}
setAllStrips(r,g,b);
delay(1000);
}
}
// Returns the Red component of a 32-bit color
byte Red(uint32_t color)
{
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
byte Green(uint32_t color)
{
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
byte Blue(uint32_t color)
{
return color & 0xFF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment