Skip to content

Instantly share code, notes, and snippets.

@AdySan
Last active February 1, 2016 03:25
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 AdySan/27cd7133c7ec8f2d8288 to your computer and use it in GitHub Desktop.
Save AdySan/27cd7133c7ec8f2d8288 to your computer and use it in GitHub Desktop.
A simple 6 NeoPixel nightlight using a digispark and IKEA SOLVINDEN
#include <Adafruit_NeoPixel.h>
//#ifdef __AVR__
#include <avr/power.h>
//#endif
#define PIN 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, PIN, NEO_GRB + NEO_KHZ800);
unsigned long long previousMillis = 0;
unsigned long long timeON = 54000000; // 15 hours
unsigned long long timeOFF = 32400000; // 9 hours
int ledState = HIGH; // ledState used to set the LED
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*1; 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.setBrightness(60);
strip.show();
delay(wait);
}
}
// 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 setup() {
clock_prescale_set(clock_div_1);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
unsigned long long currentMillis = millis();
unsigned long long countTime = currentMillis - previousMillis;
// State machine for Night Light
if (ledState == HIGH){
if (countTime >= timeON) {
previousMillis = currentMillis;
// if (ledState == LOW) {
// ledState = HIGH;
// }
// else {
ledState = LOW;
// }
}
}
else{
if (countTime >= timeOFF) {
previousMillis = currentMillis;
// if (ledState == LOW) {
// ledState = HIGH;
// }
// else {
ledState = HIGH;
// }
}
}
// Use state to set NeoPixels appropriately
if(ledState == HIGH){
rainbowCycle(50);
}
else{
for(uint8_t i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, 0 );
}
strip.begin();
strip.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment