Skip to content

Instantly share code, notes, and snippets.

@GreenMoonArt
Created December 26, 2019 17:13
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/bfa727f8c097ef0bb23f8e0339539704 to your computer and use it in GitHub Desktop.
Save GreenMoonArt/bfa727f8c097ef0bb23f8e0339539704 to your computer and use it in GitHub Desktop.
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 9
// This is how many neopixles are in one candy cane.
// I connected several candy canes in parallel, so they are all identical
#define NUMPIXELS 45
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 150; // edit based on preferences
unsigned long currentTime;
unsigned long loopTime;
// Need to cast each operand, otherwise the hex math is not correct:
unsigned long interval = (unsigned long)7 * (unsigned long)60 * (unsigned long)60 * (unsigned long)1000; // 7 hours
int darknessThreshold = 50; // edit based on lighting conditions
void setup() {
Serial.begin(9600);
strip.begin(); // This initializes the NeoPixel library.
randomSeed(analogRead(A3));
currentTime = millis();
loopTime = currentTime;
}
void loop() {
int darknessValue = analogRead(A0);
Serial.println(darknessValue);
currentTime = millis();
if( (darknessValue < darknessThreshold) && ((unsigned long)(currentTime - loopTime) <= interval) )
{
//light each pixel incrementally with red
for(int i=0;i<NUMPIXELS;i++)
{
// strip.Color takes RGB values, from 0,0,0 up to 255,255,255
strip.setPixelColor(i, strip.Color(150,0,0)); // Moderately bright red color
strip.show(); // This sends the updated pixel color to the hardware
delay(delayval); // Delay for a period of time (in milliseconds)
}
delay(500);
//reverse direction and light with green
for(int i=NUMPIXELS-1; i>=0; i--)
{
strip.setPixelColor(i, strip.Color(0,150,0)); // Moderately bright green color
strip.show(); // This sends the updated pixel color to the hardware
delay(delayval); // Delay for a period of time (in milliseconds)
}
delay(500);
//light every other one with red
for(int i=0;i<NUMPIXELS;i++)
{
if(i % 2 == 0)
{
strip.setPixelColor(i, strip.Color(150,0,0)); // Moderately bright red color
strip.show(); // This sends the updated pixel color to the hardware
delay(delayval * 2); // Delay for a period of time (in milliseconds)
}
}
delay(500);
//light every third one with white, reverse direction
for(int i=NUMPIXELS-1; i>=0; i--)
{
if(i % 3 == 0)
{
strip.setPixelColor(i, strip.Color(150,150,150));
strip.show(); // This sends the updated pixel color to the hardware
delay(delayval * 3); // Delay for a period of time (in milliseconds)
}
}
delay(1000);
//blocks of color (red, white, green) every 5 pixels or so
// for the test candycane, there are 45 total pixels, so 45 / 9 = 5
byte divisor = 9;
byte colorBlock = NUMPIXELS / divisor;
for(int x = 0; x < 2; x++)
{
for(int incrementNum = 0; incrementNum < 45; incrementNum++)
{
for(int i = 0+incrementNum; i < colorBlock+incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(150,150,150));
strip.show();
}
for(int i = colorBlock + incrementNum; i < 2*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(150,0,0));
strip.show();
}
for(int i = 2*colorBlock + incrementNum; i < 3*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(0,150,0));
strip.show();
}
for(int i = 3*colorBlock + incrementNum; i < 4*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(150,150,150));
strip.show();
}
for(int i = 4*colorBlock + incrementNum; i < 5*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(150,0,0));
strip.show();
}
for(int i = 5*colorBlock + incrementNum; i < 6*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(0,150,0));
strip.show();
}
for(int i = 6*colorBlock + incrementNum; i < 7*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(150,150,150));
strip.show();
}
for(int i = 7*colorBlock + incrementNum; i < 8*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(150,0,0));
strip.show();
}
for(int i = 8*colorBlock + incrementNum; i < 9*colorBlock + incrementNum; i++)
{ int j = i;
if(j >= NUMPIXELS) { j = j - NUMPIXELS; }
strip.setPixelColor(j, strip.Color(0,150,0));
strip.show();
}
delay(100);
}
}
rainbowCycle(20);
//random sprinkle reds, greens, and whites
int pixelNum = 0;
int colorValue = 150;
int colorType = 1;
long onDuration = 12000;
unsigned long previousMillis = millis();
while(millis() - previousMillis <= onDuration)
{
pixelNum = random(0, NUMPIXELS);
colorValue = random(75, 200);
colorType = random(1, 4);
if(colorType == 1)
{
strip.setPixelColor(pixelNum, strip.Color(colorValue,0,0));
}
else if(colorType == 2)
{
strip.setPixelColor(pixelNum, strip.Color(0,colorValue,0));
}
if(colorType == 3)
{
strip.setPixelColor(pixelNum, strip.Color(colorValue,colorValue,colorValue));
}
strip.show();
delay(25);
}
theaterChaseRainbow(50);
}
else if ((darknessValue < darknessThreshold) && ( (unsigned long)(currentTime - loopTime) > interval) )
{
// It is still dark, but we have exceeded the time limit
//// turn off LEDs
ledsOff(1000);
}
else //it's daylight
{
// keep LEDs off and reset timer
loopTime = currentTime;
ledsOff(1000);
}
} //loop()
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void ledsOff(uint8_t wait)
{
for(uint16_t j=0; j<strip.numPixels(); j++)
{
strip.setPixelColor(j, strip.Color(0, 0, 0));
}
strip.show();
delay(wait);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment