adafruit_support_bill wrote:So your states are not really states then. Just transitions between states.
Fair enough. I suppose that is more accurate.
So how is Fade out supposed to work then? You reverse the fade colors when the button is released, but you don't update them unless the button is pressed.
Close... except it updates because its 'still in not-pressed' button state...(button press update fade-in,.... button release updates fade-out)
I havent given too much though to the fadeout function yet.. (as I was still just trying to wrap my head around getting a normal 'fade in' function to work....at all)..
but in theory.. yes. When the button is released.
* reverse colors (although in the end.. I'd like to use getPixelColor() to set the initial color1 (because you cant be sure at what point in the 'fade-in' pattern (color) you are when released..
* set some other vars like what pattern to use, patternIntervalSpeed, totalSteps...etc..etc..
* return back to the button checking (idle) state...
if there has been NO button state change once back in the IDLE state.. then continue to update until pattern is complete/total steps reached..etc..etc..
This latest version is sorta working (for the fade-in portion only)
Except that it keeps fading in over and over (and I need to play with the speed/timing more I suppose)..
However.. the release (fade-out) doesnt work.. and when I press the button again.. it doesn random behavior color, blinks..etc)
Code: Select all
//neopixels
/*
#include <Adafruit_NeoPixel.h>
#define PINforControl 6 // pin connected to the small NeoPixels strip
#define NUMPIXELS 8 // number of LEDs on strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PINforControl, NEO_GRB + NEO_KHZ800);
*/
//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(255, 255, 255);
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;
//set colors for fade in pattern/animation
Color1 = strip.getPixelColor(0); //nocolor; // What colors are in use
Color2 = red; // What colors are in use
//update led pattern/animation
targetPattern = 0;
patternUpdateInterval = powerOnSpeed;
totalPatternSteps = 200;
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;
//set colors for fade in pattern/animation
//Color1 = red; // What colors are in use
Color1 = strip.getPixelColor(0);
Color2 = nocolor; // What colors are in use
//update led pattern/animation
targetPattern = 2;
patternUpdateInterval = powerDownSpeed;
totalPatternSteps = 300;
//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) {
currentPatternStep++;
if (currentPatternStep >= totalPatternSteps) {
currentPatternStep = 0;
doFadeIn = false;
if (OnComplete != NULL) {
OnComplete(); // call the comlpetion callback
}
}
} else { // Direction == REVERSE
--currentPatternStep;
if (currentPatternStep <= 0) {
currentPatternStep = totalPatternSteps - 1;
if (OnComplete != NULL) {
OnComplete(); // call the comlpetion callback
}
}
}
}
// Update the Fade Pattern - - //the gold of what I need.
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();
Increment();
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 < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
// 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);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
// Initialize for a Fade -- // Needed? can be set by poweron/powerdown states?
/*
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t patternUpdateInterval, direction dir = FORWARD) {
targetPattern = 0;
patternUpdateInterval = powerOnSpeed;
totalPatternSteps = steps;
Color1 = color1;
Color2 = color2;
currentPatternStep = 0;
Direction = dir;
}
*/
I have commented out the call to FADEOUT state.. and also the check when the button is -still- in released mode that updates the fadeout pattern..
So this is no only focusing on the fadeIn behavior..
This works pretty good..
fades in from off (initially set color).. to red... at a decent speed.. however if I keep the button pressed after the fade in starts and finishes.... it starts to blink with some purple/blue hues in it.... when I let go and press the button again.. more blinking and either a red or light purple color when released.