Skip to content

Instantly share code, notes, and snippets.

@GreenMoonArt
Last active May 6, 2019 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreenMoonArt/73795d10dfc0a074047679ff47436bfc to your computer and use it in GitHub Desktop.
Save GreenMoonArt/73795d10dfc0a074047679ff47436bfc to your computer and use it in GitHub Desktop.
ATTiny85 NeoPixel Ring After Dark with Randomized Colors and Sparkle Effect
// Originally copied from Becky Stern https://www.tinkercad.com/things/hMoLHIEK0Ny-neopixel-strip
// Modified to add reverse direction and adapted to ATTiny85
// Added photoresistor to activate at night time
// For randomized sparkle effect, refer to "Sipping Power With NeoPixels" guide.
// https://learn.adafruit.com/sipping-power-with-neopixels/demo-code
// This circuit can be viewed and simulated at:
// https://www.tinkercad.com/things/8dXZVFS18mn-attiny85-neopixel-after-dark-randomized-colors-sparkle-effect
#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 12 // number of neopixels in Ring
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 100; // timing delay
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
const int lightThreshold = 350; //adjust to your particular lighting situation
unsigned long currentMillis;
unsigned long previousMillis;
const unsigned long onTime = 5000; // when to turn on random function - adjust to your liking
uint32_t currentColor = pixels.Color(0, 0, 0);
void setup() {
pixels.begin(); // Initialize the NeoPixel library.
randomSeed(analogRead(A2)); //helps with randomization
}
void loop() {
int lightValue = analogRead(A3);
if(lightValue <= lightThreshold)
{
currentMillis = millis();
if(currentMillis - previousMillis > onTime)
{
currentMillis = millis();
while(millis() - currentMillis < onTime)
{
currentColor = pixels.Color(random(128), random(128), random(128));
mode_sparkle(currentColor);
pixels.show();
delay(25);
}
previousMillis = millis();
}
else
{
setColor();
for(int i=0; i<NUMPIXELS; i++)
{
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
//reverse order of lighting
setColor();
for(int i=NUMPIXELS-1; i>=0; i--)
{
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
}
else
{
ledsOff(1000);
}
}
// setColor()
// picks random values to set for RGB
void setColor(){
redColor = random(0, 255);
greenColor = random(0, 255);
blueColor = random(0, 255);
}
// Randomly, quickly turns on ONE pixel at a time.
// Demonstrates minimal power use while still doing something "catchy."
// Can also use just a primary color to reduce power use.
void mode_sparkle(uint32_t randomColor) {
static uint8_t randomPixel = 0;
pixels.clear(); // Clear pixels
uint8_t r;
do {
r = random(NUMPIXELS); // Pick a new random pixel
} while(r == randomPixel); // but not the same as last time
randomPixel = r; // Save new random pixel index
//strip.setPixelColor(randomPixel, 0x0000FF); //primary blue
pixels.setPixelColor(randomPixel, randomColor);
}
// turn off all LEDs
void ledsOff(uint8_t wait)
{
for(uint16_t j=0; j<pixels.numPixels(); j++)
{
pixels.setPixelColor(j, pixels.Color(0, 0, 0));
}
pixels.show();
delay(wait);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment