Neopixel Breathe function code help

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
upgrd
 
Posts: 12
Joined: Mon Mar 14, 2016 12:10 pm

Neopixel Breathe function code help

Post by upgrd »

Please bear with me, pretty new at this.
I have code that now works close to what I need. Except for one thing. I need each color to fade in and out.
What is does now: 5 colors that you can cycle through with the 6th cycle all off.
Each press of the switch cycles to next color.
The colors are solid now (all on), but I would like to make them all slowly pulse or breathe....
The code I have cobbled together may or may not be able to modified to do this. Its a start point to get the discussion moving.
Any help would be appreciated.

Thanks....

Code: Select all

// You should wire a momentary push button to connect from ground to a digital IO pin. 
//This sketch is a modified buttoncycler sketch from the Adafruit libiary. As coded it will light up a strip of Neopixel
//leds as follows.
//Button press 1: All off
//Button press 2: All pixels light color
//Button press 3: All pixels light color
//Button press 4: All pixels light color
//Button press 5: All pixels light color

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   9    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    8    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 7

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);


bool oldState = HIGH;
int showType = 0;





void setup() {           // **********************this section runs one time at startup
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}



void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);

for (int i = 0; i < PIXEL_COUNT; i++) {
    strip.setPixelColor(i, 0);
}

// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 5)//******************** controls amount of modes (case)If you add or subtract modes (case) this needs to reflect that.
showType=0;
startShow(showType);
}
}

 // Set the last button state to the old state.
oldState = newState;
}

void startShow(int i) {
switch(i){
case 0: setPixelColor(strip.Color(0, 0, 0), 50);// OFF
    break;
case 1: setPixelColor(strip.Color(255, 255, 255), 50);// WHITE
    break;
case 2: setPixelColor(strip.Color(180, 100,0), 50);  // AMBER
    break;
case 3: setPixelColor(strip.Color(0,0,255), 50);  // BLUE
    break;
case 4: setPixelColor(strip.Color(255,0,0), 50);  // RED
    break;
case 5: setPixelColor(strip.Color(0,255,0), 50);  // GREEN
    break;       



 }
}


//not used
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}



void setPixelColor(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) { //  ******i=0 - 0 is the start pixel
strip.setPixelColor(i, c);       // *****change i to led\pixel number

//delay(wait);  // ************ removing eliminated the chase effect
  }
  strip.show();
}

User avatar
oesterle
 
Posts: 806
Joined: Tue Sep 17, 2013 11:32 pm

Re: Neopixel Breathe function code help

Post by oesterle »

Try adding this:

Code: Select all

// breathing pixel variables
unsigned long startTime = 0;
int colorR = 0, colorG = 0, colorB = 0;
int breathTime = 6000; // 6000ms = six seconds; adjust for your desired speed
float breathTimeFloat = breathTime;
float pi = 3.14;

// initialize the breath timer
void startBreath(int r, int g, int b){
  colorR = r;
  colorG = g;
  colorB = b;
  startTime = millis();
}

// call this inside your loop to update the color of the pixels
void updateBreath(){
  const float pi = 3.14;
  float frac; // ratio of color to use, based on time
  int r, g, b;

  // calculate a brightness fraction, based on a sine curve changing over time from 0 to 1 and back
  frac = (sin((((millis() - startTime) % breathTime)/breathTimeFloat - 0.25f) * 2.0f * pi) + 1.0f)/2.0f;

  // multiply each color by the brightness fraction
  r = colorR * frac;
  g = colorG * frac;
  b = colorB * frac;

  setPixelColor(strip.Color(r, g, b), 0);
}
Then add this just at the end of your loop() code:

Code: Select all

updateBreath();

Then, you can use this instead of setPixelColor() for each color in your startShow() code:

Code: Select all

case 1:
  // setPixelColor(strip.Color(255, 255, 255), 0, 50); // WHITE -- old way
  startBreath(255,255,255); // WHITE -- new way
  break;
Cheers,

Eric

User avatar
upgrd
 
Posts: 12
Joined: Mon Mar 14, 2016 12:10 pm

Re: Neopixel Breathe function code help

Post by upgrd »

Thanks for your time, I really appreciate it!
I took your instructions literally using my very limited knowledge. I did not delete any of the previous code. And am sure the placement is incorrect.
The new code does verify but the Breath Case's nothing happens. The cases that I left unedited (solid color) still work.
Let me know your thoughts/corrections.

Thanks.................

Code: Select all

// You should wire a momentary push button to connect from ground to a digital IO pin. 
//This sketch is a modified buttoncycler sketch from the Adafruit libiary. As coded it will light up a strip of Neopixel
//leds as follows.
//Button press 1: All off
//Button press 2: All pixels light color
//Button press 3: All pixels light color
//Button press 4: All pixels light color
//Button press 5: All pixels light color

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   9    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    8    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 7

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);


bool oldState = HIGH;
int showType = 0;

///////////////////////////////////
// breathing pixel variables
unsigned long startTime = 0;
int colorR = 0, colorG = 0, colorB = 0;
int breathTime = 6000; // 6000ms = six seconds; adjust for your desired speed
float breathTimeFloat = breathTime;
float pi = 3.14;

// initialize the breath timer
void startBreath(int r, int g, int b){
  colorR = r;
  colorG = g;
  colorB = b;
  startTime = millis();
}

///////////////////////////////////////////////////////////

void setup() {           // **********************this section runs one time at startup
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}



void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);

for (int i = 0; i < PIXEL_COUNT; i++) {
    strip.setPixelColor(i, 0);
}

// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 5)//******************** controls amount of modes (case)If you add or subtract modes (case) this needs to reflect that.
showType=0;
startShow(showType);
}
}

 // Set the last button state to the old state.
oldState = newState;
}



/////////////////////////////////////////////////////////////////

void updateBreath(){
  const float pi = 3.14;
  float frac; // ratio of color to use, based on time
  int r, g, b;

  // calculate a brightness fraction, based on a sine curve changing over time from 0 to 1 and back
  frac = (sin((((millis() - startTime) % breathTime)/breathTimeFloat - 0.25f) * 2.0f * pi) + 1.0f)/2.0f;

  // multiply each color by the brightness fraction
  r = colorR * frac;
  g = colorG * frac;
  b = colorB * frac;

  setPixelColor(strip.Color(r, g, b), 0);
}
///////////////////////////////////////////////////////////////////////////


void startShow(int i) {
switch(i){
case 0: setPixelColor(strip.Color(0, 0, 0), 50);// OFF
    break;
case 1:  startBreath(255,255,255); // WHITE -- new way
  break;
case 2:  startBreath(255,0,0); // red -- new way
  break;
case 3: startBreath(0,255,0); // green -- new way
  break;
case 4: setPixelColor(strip.Color(255,0,0), 50);  // RED
    break;
case 5:  startBreath(0,0,255); // blue -- new way
  break;





///////////////////////////////
updateBreath();
///////////////////////////




 }
}


//not used
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}



void setPixelColor(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) { //  ******i=0 - 0 is the start pixel
strip.setPixelColor(i, c);       // *****change i to led\pixel number

//delay(wait);  // ************ removing eliminated the chase effect
  }
  strip.show();
}


User avatar
upgrd
 
Posts: 12
Joined: Mon Mar 14, 2016 12:10 pm

Re: Neopixel Breathe function code help

Post by upgrd »

OK, caught one of my errors. The
updateBreath();
was incorrectly placed. Moved it to the end of the Loop section.

Now it only reads the startBreath cases , ignoring the setPixelColor (solid color).

Is there a way to have a combination of solid colors and breath colors?
for example:

Code: Select all

case 0: setPixelColor(strip.Color(0, 0, 0), 50);// OFF
    break;
case 1:  startBreath(255,255,255); // WHITE-Breath
  break;
case 2:  startBreath(255,0,0); // red -Breath
  break;
case 3: startBreath(0,255,0); // green -Breath
  break;
case 4: setPixelColor(strip.Color(255,0,0), 50);  // RED solid
    break;
case 5:  startBreath(0,0,255); // blue -Breath
  break;

Thanks............

User avatar
oesterle
 
Posts: 806
Joined: Tue Sep 17, 2013 11:32 pm

Re: Neopixel Breathe function code help

Post by oesterle »

Hi there!

Yeah, I thought that you wanted to replace solid colors with the breathing effect. Maybe you're just looking for it to crossfade to the next color when you press a button?

Can you sketch out a timeline on paper, of what happens before, at, and after a button press? That would help. You can take a photo of your sketch and post it here, using the file upload button below your reply.

Cheers,

Eric

User avatar
upgrd
 
Posts: 12
Joined: Mon Mar 14, 2016 12:10 pm

Re: Neopixel Breathe function code help

Post by upgrd »

Sorry about the late reply, work has been crazy...
Also sorry for any confusion. I did a poor job of describing.
I think this is what you mean by time line.

Press 1
All Pixels Green, Breathing
Press 2
All Pixels Green, steady
Press 3
All Pixels Red, steady
Press 4
All Pixels Amber, Breathing
Press 5
All Pixels Blue, Breathing
Press 5
All Off

User avatar
upgrd
 
Posts: 12
Joined: Mon Mar 14, 2016 12:10 pm

Re: Neopixel Breathe function code help

Post by upgrd »

I just wanted to post the most updated code I have.
This code works. All 5 modes "Breathe", but I need some modes to be all pixels, all on, not breath. See above...
Thanks.........

Code: Select all

// You should wire a momentary push button to connect from ground to a digital IO pin. 
//This sketch is a modified buttoncycler sketch from the Adafruit libiary. As coded it will light up a strip of Neopixel
//leds as follows.
//Button press 1: All off
//Button press 2: All pixels light color
//Button press 3: All pixels light color
//Button press 4: All pixels light color
//Button press 5: All pixels light color

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   9    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    8    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 7

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);


bool oldState = HIGH;
int showType = 0;

///////////////////////////////////
// breathing pixel variables
unsigned long startTime = 0;
int colorR = 0, colorG = 0, colorB = 0;
int breathTime = 6000; // 6000ms = six seconds; adjust for your desired speed
float breathTimeFloat = breathTime;
float pi = 3.14;

// initialize the breath timer
void startBreath(int r, int g, int b){
  colorR = r;
  colorG = g;
  colorB = b;
  startTime = millis();
}

///////////////////////////////////////////////////////////

void setup() {           // **********************this section runs one time at startup
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}



void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);

for (int i = 0; i < PIXEL_COUNT; i++) {
    strip.setPixelColor(i, 0);
}

// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 5)//******************** controls amount of modes (case)If you add or subtract modes (case) this needs to reflect that.
showType=0;
startShow(showType);
}
}

 // Set the last button state to the old state.
oldState = newState;


///////////////////////////////
updateBreath();
///////////////////////////


}



/////////////////////////////////////////////////////////////////

void updateBreath(){
  const float pi = 3.14;
  float frac; // ratio of color to use, based on time
  int r, g, b;

  // calculate a brightness fraction, based on a sine curve changing over time from 0 to 1 and back
  frac = (sin((((millis() - startTime) % breathTime)/breathTimeFloat - 0.25f) * 2.0f * pi) + 1.0f)/2.0f;

  // multiply each color by the brightness fraction
  r = colorR * frac;
  g = colorG * frac;
  b = colorB * frac;

  setPixelColor(strip.Color(r, g, b), 0);
}
///////////////////////////////////////////////////////////////////////////


void startShow(int i) {
switch(i){
case 0: startBreath(0,0,0); // OFF
    break;
case 1:  startBreath(0,255,0); // GREEN -- BREATH
  break;
case 2:  startBreath(0,255,0); // GREEN -- BREATH
  break;
case 3:  startBreath(255,0,0); // red -- BREATH
  break;
case 4: startBreath(155,155,0); // AMBER -- BREATH
  break;
case 5:  startBreath(0,0,255); // blue -- BREATH
  break;







 }
}


//not used
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}



void setPixelColor(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) { //  ******i=0 - 0 is the start pixel
strip.setPixelColor(i, c);       // *****change i to led\pixel number

//delay(wait);  // ************ removing eliminated the chase effect
  }
  strip.show();
}

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

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