Yikes! (sorry) I must pasted the wrong version.. that last section was un-commented out.. so the 'fadeout' part would trigger..
I have two current variants..
1.) That works to get a fade IN correctly... but the fade OUT is just a repeat of the fade IN (I believe it may have something to do with the altered increment function).
* both fade IN and fade OUT should take the current 'color' state of the strip (first led should be fine enough to get a color sample from)
* be able to have its own total step value for how long it should take to complete the 'animation/pattern'
this one has a repeat of the fade in:
Code: Select all
//dotstars
#include <Adafruit_DotStar.h>
#include <SPI.h>
#define NUMPIXELS 144 //temp number
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BGR); //Royce's strip
// Patern directions supported:
enum direction { FORWARD, REVERSE };
direction Direction = FORWARD; // direction to run the pattern - no direction needed in this project (kept for posterity for now)
void (*OnComplete)(); // Callback on completion of pattern -// not implemented yet in my current sketch
const int buttonPin = 2; //A5 = D19
int buttonState = 0;
int lastButtonState = 1;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 10; // the debounce time; increase if the output flickers
unsigned long lastPatternUpdate = 0; // timestamp of last update to led pattern
uint16_t totalPatternSteps; // total number of steps in the pattern
uint16_t currentPatternStep;
unsigned long patternUpdateInterval = 30; // milliseconds between updates
unsigned long powerOnSpeed = 30; //lower = faster
unsigned long powerDownSpeed = 30; //lower = faster
uint32_t Color1; // What colors are in use
uint32_t Color2; // What colors are in use
int targetPattern = 0; // which pattern is running
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t nocolor = strip.Color(0, 0, 0);
long lastPowerOnCompletionCheck = 0; // the last time the output pin was toggled
long lastPowerDownCompletionCheck = 0; // the last time the output pin was toggled
#define S_IDLE 1
#define S_FADEIN 2
#define S_FADEOUT 3
static int state = S_IDLE;
bool doFadeOut = false;
bool doFadeIn = false;
void setup() {
//debug (monitor)
Serial.begin(115200);
//neopixel strip setup (doesnt seem work to reset leds to 'off'
Serial.println(F("turn all leds off")); //read current volume
strip.begin(); // Initialize pins for output
strip.show(); //reset all leds to off
clearall();
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
//Color1 = Wheel(random(255)); // What colors are in use
//Color2 = Wheel(random(255)); // What colors are in use
}
void loop() {
switch (state) {
case S_IDLE:
buttonState = digitalRead(buttonPin);
if ((buttonState != lastButtonState) && (millis() - lastDebounceTime > debounceDelay)) {
lastDebounceTime = millis(); //update last time pressed/released
if (buttonState == LOW) {
state = S_FADEIN;
} else {
state = S_FADEOUT;
}
}else {
//no button state change, but still being pressed/held low
if (buttonState == LOW) {
//continue updating pattern if button is still pressed.
if (millis() - lastPatternUpdate > patternUpdateInterval && doFadeIn == true) {
updatePattern(targetPattern);
}
}else {
//update led pattern/animation
if (millis() - lastPatternUpdate > patternUpdateInterval && doFadeOut == true) {
updatePattern(targetPattern);
}
}
}
lastButtonState = buttonState;
break;
case S_FADEIN:
Serial.println(F("[FADE IN]"));
doFadeIn = true;
Direction = FORWARD;
//set colors for fade in pattern/animation
//Color1 = nocolor;
//what colors are in use?
Color1 = strip.getPixelColor(0); //nocolor;
Color2 = red;
//update led pattern/animation
targetPattern = 0;
patternUpdateInterval = powerOnSpeed;
totalPatternSteps = 8;
updatePattern(targetPattern);
//update timestamp
lastPowerOnCompletionCheck = millis(); //update last time checked for file completion
//return to button checking (idle state)
state = S_IDLE;
break;
case S_FADEOUT:
Serial.println(F("[FADE OUT]"));
doFadeOut = true;
Direction = REVERSE;
//set colors for fade in pattern/animation
Color1 = strip.getPixelColor(0);
Color2 = nocolor; // What colors are in use
//update led pattern/animation
targetPattern = 2;
patternUpdateInterval = powerDownSpeed;
totalPatternSteps = 30;
currentPatternStep = totalPatternSteps;
updatePattern(targetPattern);
//update timestamp
lastPowerDownCompletionCheck = millis(); //update last time checked for file completion
//return to button checking (idle state)
state = S_IDLE;
break;
}
}
//--[neopixel pattern code]--//
void updatePattern(int newPattern) {
switch (newPattern) {
case 0:
FadeUpdate(); //new addition from tutorial (function only?)
//setColor(red);
break;
case 1:
FadeUpdate();
//setColor(blue);
break;
case 2:
FadeUpdate();
//setColor(green);
break;
case 3:
setColor(red);
break;
}
}
void clearall() {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
//Serial.println(F("--should be all cleared--"));
}
void setColor(uint32_t targetColor) {
for (int i = 0; i < NUMPIXELS; i++) {
if (i < 12) {
strip.setPixelColor(i, targetColor);
} else {
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
lastPatternUpdate = millis(); //update pattern time stamp
}
// Increment the index and reset at the end
void Increment() {
if (Direction == FORWARD) {
Serial.print(F("fading in: -------->> "));
Serial.println(currentPatternStep);
currentPatternStep++;
if (currentPatternStep >= totalPatternSteps) {
//currentPatternStep = 0;
doFadeIn = false;
Serial.println(F("----------------fade in pattern is complete------------------"));
if (OnComplete != NULL) {
OnComplete(); // call the comlpetion callback
}
}
}else { // Direction == REVERSE
Serial.print(F("fading out: -------->> "));
Serial.println(currentPatternStep);
--currentPatternStep;
if (currentPatternStep <= totalPatternSteps) {
if(currentPatternStep <= 0){
doFadeOut = false;
//currentPatternStep = 0;
Serial.println(F("----------------fade out pattern is complete------------------"));
if (OnComplete != NULL) {
OnComplete(); // call the comlpetion callback
}
}
}
}
}
//update the fade pattern - - //the equation of what does the tweening/easing
void FadeUpdate() {
//Serial.println(F("Fade pattern update....."));
uint8_t red = ((Red(Color1) * (totalPatternSteps - currentPatternStep)) + (Red(Color2) * currentPatternStep)) / totalPatternSteps;
uint8_t green = ((Green(Color1) * (totalPatternSteps - currentPatternStep)) + (Green(Color2) * currentPatternStep)) / totalPatternSteps;
uint8_t blue = ((Blue(Color1) * (totalPatternSteps - currentPatternStep)) + (Blue(Color2) * currentPatternStep)) / totalPatternSteps;
ColorSet(strip.Color(red, green, blue));
strip.show(); //-there is a call in ColorSet() function to show() ...why here as well?
Increment();
/*
currentPatternStep++;
if (currentPatternStep >= totalPatternSteps) {
currentPatternStep = 0;
doFadeIn = false;
Serial.println(F("----------------pattern is complete------------------"));
}
*/
lastPatternUpdate = millis(); //update pattern time stamp
}
// Returns the Red component of a 32-bit color
uint8_t Red(uint32_t color) {
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
uint8_t Green(uint32_t color) {
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
uint8_t Blue(uint32_t color) {
return color & 0xFF;
}
// Set all pixels to a color (synchronously)
void ColorSet(uint32_t color) {
//Serial.println(F("Setting color...."));
for (int i = 0; i < NUMPIXELS; i++) {
if (i < 12) {
strip.setPixelColor(i, color);
} else {
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
lastPatternUpdate = millis(); //update pattern time stamp
}
This one... on fade OUT.. I just simply set the color strip to GREEN (this was before attempting to implement the above real working fade OUT routine)..
What I found was.. (and maybe I am just understanding the fade function wrong?.. or more realistic.. I've messed something up! lol)... when I pressed button..(and held it).. the faded in worked correctly.
When I let go.. I simple changed led strip to green. (no fade, /animation..etc)
When I pressed the button again.. I was expecting it to change from (currently color/state of) green, to red..
but it only seemed to 'mix' the color? leaving me at an 'orange' color state...
* Am I understanding this fade function wrong? Is it supposed to take the current color? and target color and 'mix' them? Or should Color2 always be the color the strip ultimately ends up at?
Code: Select all
//dotstars
#include <Adafruit_DotStar.h>
#include <SPI.h>
#define NUMPIXELS 144 //temp number
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BGR); //Royce's strip
// Patern directions supported:
enum direction { FORWARD, REVERSE };
direction Direction = FORWARD; // direction to run the pattern - no direction needed in this project (kept for posterity for now)
void (*OnComplete)(); // Callback on completion of pattern -// not implemented yet in my current sketch
const int buttonPin = 2; //A5 = D19
int buttonState = 0;
int lastButtonState = 1;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 10; // the debounce time; increase if the output flickers
unsigned long lastPatternUpdate = 0; // timestamp of last update to led pattern
uint16_t totalPatternSteps; // total number of steps in the pattern
uint16_t currentPatternStep;
unsigned long patternUpdateInterval = 30; // milliseconds between updates
unsigned long powerOnSpeed = 30; //lower = faster
unsigned long powerDownSpeed = 30; //lower = faster
uint32_t Color1; // What colors are in use
uint32_t Color2; // What colors are in use
int targetPattern = 0; // which pattern is running
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t nocolor = strip.Color(0, 0, 0);
long lastPowerOnCompletionCheck = 0; // the last time the output pin was toggled
long lastPowerDownCompletionCheck = 0; // the last time the output pin was toggled
#define S_IDLE 1
#define S_FADEIN 2
#define S_FADEOUT 3
static int state = S_IDLE;
bool doFadeOut = false;
bool doFadeIn = false;
void setup() {
//debug (monitor)
Serial.begin(115200);
//neopixel strip setup (doesnt seem work to reset leds to 'off'
Serial.println(F("turn all leds off")); //read current volume
strip.begin(); // Initialize pins for output
strip.show(); //reset all leds to off
clearall();
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
//Color1 = Wheel(random(255)); // What colors are in use
//Color2 = Wheel(random(255)); // What colors are in use
}
void loop() {
switch (state) {
case S_IDLE:
buttonState = digitalRead(buttonPin);
if ((buttonState != lastButtonState) && (millis() - lastDebounceTime > debounceDelay)) {
lastDebounceTime = millis(); //update last time pressed/released
if (buttonState == LOW) {
state = S_FADEIN;
} else {
state = S_FADEOUT;
}
}else {
//no button state change, but still being pressed/held low
if (buttonState == LOW) {
//continue updating pattern if button is still pressed.
if (millis() - lastPatternUpdate > patternUpdateInterval && doFadeIn == true) {
updatePattern(targetPattern);
}
}else {
//update led pattern/animation
if (millis() - lastPatternUpdate > patternUpdateInterval && doFadeOut == true) {
updatePattern(targetPattern);
}
}
}
lastButtonState = buttonState;
break;
case S_FADEIN:
Serial.println(F("[FADE IN]"));
doFadeIn = true;
Direction = FORWARD;
//set colors for fade in pattern/animation
//Color1 = nocolor;
//what colors are in use?
Color1 = strip.getPixelColor(0); //nocolor;
Color2 = red;
//update led pattern/animation
targetPattern = 0;
patternUpdateInterval = powerOnSpeed;
totalPatternSteps = 8;
updatePattern(targetPattern);
//update timestamp
lastPowerOnCompletionCheck = millis(); //update last time checked for file completion
//return to button checking (idle state)
state = S_IDLE;
break;
case S_FADEOUT:
Serial.println(F("[FADE OUT]"));
doFadeOut = true;
//Direction = REVERSE;
//set colors for fade in pattern/animation
Color1 = strip.getPixelColor(0);
Color2 = nocolor; // What colors are in use
//update led pattern/animation
targetPattern = 2;
patternUpdateInterval = powerDownSpeed;
//totalPatternSteps = 30;
//currentPatternStep = totalPatternSteps;
updatePattern(targetPattern);
//update timestamp
lastPowerDownCompletionCheck = millis(); //update last time checked for file completion
//return to button checking (idle state)
state = S_IDLE;
break;
}
}
//--[neopixel pattern code]--//
void updatePattern(int newPattern) {
switch (newPattern) {
case 0:
FadeUpdate(); //new addition from tutorial (function only?)
//setColor(red);
break;
case 1:
//FadeUpdate();
setColor(blue);
break;
case 2:
//FadeUpdate();
setColor(green);
break;
case 3:
setColor(red);
break;
}
}
void clearall() {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
//Serial.println(F("--should be all cleared--"));
}
void setColor(uint32_t targetColor) {
for (int i = 0; i < NUMPIXELS; i++) {
if (i < 12) {
strip.setPixelColor(i, targetColor);
} else {
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
lastPatternUpdate = millis(); //update pattern time stamp
doFadeOut = false;
}
// Increment the index and reset at the end
void Increment() {
if (Direction == FORWARD) {
Serial.print(F("fading in: -------->> "));
Serial.println(currentPatternStep);
currentPatternStep++;
if (currentPatternStep >= totalPatternSteps) {
//currentPatternStep = 0;
doFadeIn = false;
Serial.println(F("----------------fade in pattern is complete------------------"));
if (OnComplete != NULL) {
OnComplete(); // call the comlpetion callback
}
}
}else { // Direction == REVERSE
Serial.print(F("fading out: -------->> "));
Serial.println(currentPatternStep);
--currentPatternStep;
if (currentPatternStep <= totalPatternSteps) {
if(currentPatternStep <= 0){
doFadeOut = false;
currentPatternStep = 0;
Serial.println(F("----------------fade out pattern is complete------------------"));
if (OnComplete != NULL) {
OnComplete(); // call the comlpetion callback
}
}
}
}
}
//update the fade pattern - - //the equation of what does the tweening/easing
void FadeUpdate() {
//Serial.println(F("Fade pattern update....."));
uint8_t red = ((Red(Color1) * (totalPatternSteps - currentPatternStep)) + (Red(Color2) * currentPatternStep)) / totalPatternSteps;
uint8_t green = ((Green(Color1) * (totalPatternSteps - currentPatternStep)) + (Green(Color2) * currentPatternStep)) / totalPatternSteps;
uint8_t blue = ((Blue(Color1) * (totalPatternSteps - currentPatternStep)) + (Blue(Color2) * currentPatternStep)) / totalPatternSteps;
ColorSet(strip.Color(red, green, blue));
strip.show(); //-there is a call in ColorSet() function to show() ...why here as well?
//Increment();
currentPatternStep++;
if (currentPatternStep >= totalPatternSteps) {
currentPatternStep = 0;
doFadeIn = false;
Serial.println(F("----------------pattern is complete------------------"));
}
lastPatternUpdate = millis(); //update pattern time stamp
}
// Returns the Red component of a 32-bit color
uint8_t Red(uint32_t color) {
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
uint8_t Green(uint32_t color) {
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
uint8_t Blue(uint32_t color) {
return color & 0xFF;
}
// Set all pixels to a color (synchronously)
void ColorSet(uint32_t color) {
//Serial.println(F("Setting color...."));
for (int i = 0; i < NUMPIXELS; i++) {
if (i < 12) {
strip.setPixelColor(i, color);
} else {
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
lastPatternUpdate = millis(); //update pattern time stamp
}
More messing with it.....the closer things get I guess!
Thanks for sticking in there! (I know it probably painful for you!) LOL.. but I do appreciate it.