Created
June 25, 2017 14:57
-
-
Save ivanseidel/651c834079961d1df578aa176bac725d to your computer and use it in GitHub Desktop.
NoLight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Ivan Seidel | |
// A Led-Strip Light controller using a single button to control on/off and dimmer | |
// | |
#include <Adafruit_NeoPixel.h> | |
#define LEDS D2 | |
#define BTN D1 | |
// How many NeoPixels are attached to the Arduino? | |
#define NUMPIXELS 189 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDS, NEO_GBR + NEO_KHZ800); | |
void setup() { | |
Serial.begin(9600); | |
pinMode(BTN, INPUT); | |
digitalWrite(BTN, HIGH); | |
pixels.begin(); // This initializes the NeoPixel library. | |
} | |
boolean isOn = false; | |
double fillRaw = 0.0; | |
double fill = 0.0; | |
uint32_t colorOn = pixels.Color(255, 255, 255); | |
uint32_t colorOff = pixels.Color(0, 0, 0); | |
int intensity = 255; | |
int intensityDir = -1; | |
#define FILL_SPEED 1.2 | |
#define CYCLE_DELAY 20 | |
unsigned long lastCall = 0; | |
boolean isPressing = false; | |
boolean wasPressing = false; | |
unsigned long btnStartPress = 0; | |
void loop() { | |
// Check for button press | |
isPressing = !digitalRead(BTN); | |
if(isPressing && !wasPressing){ | |
// Serial.println("A"); | |
// Started pressing! | |
btnStartPress = millis(); | |
}else if(isPressing && wasPressing){ | |
// Serial.println("B"); | |
// Continues to press button... | |
if(millis() - btnStartPress > 2000){ | |
// Turn on | |
isOn = true; | |
// Change direction | |
if(intensity <= 20) | |
intensityDir = 1; | |
else if(intensity >= 254) | |
intensityDir = -1; | |
intensity = min(max(intensity + intensityDir * 3, 0), 255); | |
} | |
}else if(!isPressing && !wasPressing){ | |
// Serial.println("C"); | |
// Still Released. | |
}else if(!isPressing && wasPressing){ | |
// Serial.println("D"); | |
// Just released! | |
// Check if it was a loooong time pressing, and change state | |
if(millis() - btnStartPress < 2000){ | |
isOn = !isOn; | |
if(isOn && fill < 0.02){ | |
intensity = 255; | |
intensityDir = -1; | |
} | |
} | |
} | |
wasPressing = isPressing; | |
// if(millis() - lastCall > 6000){ | |
// lastCall = millis(); | |
// isOn = !isOn; | |
// } | |
// Cicle delay | |
delay(CYCLE_DELAY); | |
// Animate Fill | |
fillRaw += (CYCLE_DELAY / 1000.0) * FILL_SPEED * (isOn * 2 - 1); | |
// Limits fillRaw | |
fillRaw = fmax(fillRaw, 0.0); | |
fillRaw = fmin(fillRaw, 1.0); | |
// Applies interpolation | |
fill = (tanh(fillRaw * 4.0 - 2.0) + 1) / 2.0; | |
// Rounds to match 0.0 or 1.0 | |
fill = (fill < 0.02 ? 0.0 : fill); | |
fill = (fill > 0.98 ? 1.0 : fill); | |
// Skip update if fill is 0.0 | |
//if(fill < 0.001) | |
//return; | |
// Refresh strip status | |
for(int i=0;i<NUMPIXELS;i++){ | |
// Distance from center | |
float dist = abs(NUMPIXELS / 2.0 - i) / (NUMPIXELS / 2.0); | |
// Check if in range of `fill` | |
if(dist > fill) | |
pixels.setPixelColor(i, colorOff); | |
else | |
pixels.setPixelColor(i, intensity, intensity, intensity); | |
} | |
pixels.show(); // This sends the updated pixel color to the hardware. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment