NEOPIXEL brightness control

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: NEOPIXEL brightness control

Post by adafruit_support_bill »

Close... except it updates because its 'still in not-pressed' button state...(button press update fade-in,.... button release updates fade-out)
Sorry, but I don't see how the code posted above could possibly update more than once unless the button remains pressed.

Code: Select all

        if (buttonState == LOW) {
          //continue updating pattern if button is still pressed.
          if (millis() - lastPatternUpdate > patternUpdateInterval) {
            updatePattern(targetPattern);
          }

        } else {
          //update led pattern/animation
          //if (millis() - lastPatternUpdate > patternUpdateInterval) {
            //updatePattern(targetPattern);
          //}
        }

User avatar
xl97
 
Posts: 201
Joined: Mon Jul 27, 2009 12:51 pm

Re: NEOPIXEL brightness control

Post by xl97 »

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.

User avatar
xl97
 
Posts: 201
Joined: Mon Jul 27, 2009 12:51 pm

Re: NEOPIXEL brightness control

Post by xl97 »

ok.. so (outside of the ugly mess of code in its current state)...

the results are almost 100% of what I'm looking for!

The only current issue is that the FADE OUT, does fade completely out.. its just a DIMmer red...
I release the button.. and at whatever stage the fade IN is at.. the fade OUT takes it from there.. and starts to fade out. (good)
I press the button....and no matter the current state of fade OUT... fade IN starts.. (good)

So currently.. I guess the last thing to do is figure out WHY the fade out isnt going complete off (0,0,0) color?

Current state of sketch:

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 = 25; //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;
      currentPatternStep = 0;
      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
      Color2 = strip.getPixelColor(0);
      Color1 = nocolor; // What colors are in use

      //update led pattern/animation
      targetPattern = 2;
      patternUpdateInterval = powerDownSpeed;
      totalPatternSteps = 22;
      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) {
    currentPatternStep++;
    Serial.print(F("fading in: -------->>  "));
    Serial.println(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
    --currentPatternStep;    
    Serial.print(F("fading out: -------->>  "));
    Serial.println(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   
}



User avatar
xl97
 
Posts: 201
Joined: Mon Jul 27, 2009 12:51 pm

Re: NEOPIXEL brightness control

Post by xl97 »

Final update:

Current this is working as desired:

*still lots of code clean up and refactoring needed... but the 'results' are what I have been working toward. (I understand that this is the wrong approach as the 'class' had everything all wrapped nice & neat for usage. but for me.. there was so much extra code/stuff that I didnt need/want, and just copy/pasting code without understanding it isnt appealing. Breaking it down may have dumbed down the code and added some redundant'ish functions... but I can follow it easier and under it more)

** moving this line: --currentPatternStep; to after the conditional check let the fade OUT pattern completed to fully 'off'..

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


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 = 25; //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;
      
      //set colors for fade in pattern/animation
      Color1 = strip.getPixelColor(0); //get current color to fade from
      Color2 = red; 

      //update led pattern variables      
      totalPatternSteps = 8;
      currentPatternStep = 0;
      targetPattern = 0;
      patternUpdateInterval = powerOnSpeed;
      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
      Color2 = strip.getPixelColor(0); //get current color to fade from
      Color1 = nocolor;

      //update led pattern variables
      totalPatternSteps = 22;
      currentPatternStep = totalPatternSteps;
      targetPattern = 2;
      patternUpdateInterval = powerDownSpeed;
      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:
      FadeIn();
      //FadeUpdate(); //new addition from tutorial (function only?)
      //setColor(red);
      break;
    case 1:      
      //FadeOut();
      //FadeUpdate();
      setColor(blue);
      break;
    case 2:
      FadeOut();
      //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
}


//update the fade pattern - - //the equation of what does the tweening/easing
void FadeIn() {
  //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?
  
  Serial.print(F("fading in: -------->>  "));
  Serial.println(currentPatternStep);    
  currentPatternStep++;
  
  if (currentPatternStep >= totalPatternSteps) {
    currentPatternStep = 0;
    doFadeIn = false;
    Serial.println(F("[---fade in pattern is complete---]"));       
  }
  
  lastPatternUpdate = millis();  //update pattern time stamp
}

void FadeOut() {
  //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?
    
  Serial.print(F("fading out: -------->>  "));
  Serial.println(currentPatternStep);    
  
  if (currentPatternStep <= totalPatternSteps) {
    if(currentPatternStep <= 0){    
      doFadeOut = false;
      currentPatternStep = 0;
      Serial.println(F("[---fade out pattern is complete---]"));      
    }
  }
  --currentPatternStep;   
  
  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   
}



At this point...

I still need to add back in all my other code/behavior for the audio and what not... as well as shift focus to a final led strip 'pattern' to try and emulate some sort of fire/rocket burst/thrust effect... as the pattern for when then fadeIN is complete.. but the button is still being held down. (so I might be back!) LOL.

Thank you Adafruit_Bill for your help/guidance!

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”