Hello, New to a lot of this.

Hello, New to a lot of this. I have an adafruit trinket and a neopixel jewel wired up. I am using the Fire2012withpallete code from the FASTLED library. I have a push button connected and I want it to turn off the LEDs when pressed. However, when I push the button it just changes the color state to a blue color. Could anyone help me out on how to tweak the code below so that the neo turns off and doesnt not just change color? Thanks.

#include <FastLED.h>

#define LED_PIN 2
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define NUM_LEDS 7
#define BRIGHTNESS 300
#define FRAMES_PER_SECOND 60

#define BUTTON_PIN 0 // the pin to which the button is attached

bool gReverseDirection = false;

CRGB leds[NUM_LEDS];

// Fire2012 with programmable Color Palette
//
// This code is the same fire simulation as the original “Fire2012”,
// but each heat cell’s temperature is translated to color through a FastLED
// programmable color palette, instead of through the “HeatColor(…)” function.
//
// Four different static color palettes are provided here, plus one dynamic one.
//
// The three static ones are:
// 1. the FastLED built-in HeatColors_p – this is the default, and it looks
// pretty much exactly like the original Fire2012.
//
// To use any of the other palettes below, just “uncomment” the corresponding code.
//
// 2. a gradient from black to red to yellow to white, which is
// visually similar to the HeatColors_p, and helps to illustrate
// what the ‘heat colors’ palette is actually doing,
// 3. a similar gradient, but in blue colors rather than red ones,
// i.e. from black to blue to aqua to white, which results in
// an “icy blue” fire effect,
// 4. a simplified three-step gradient, from black to red to white, just to show
// that these gradients need not have four components; two or
// three are possible, too, even if they don’t look quite as nice for fire.
//
// The dynamic palette shows how you can change the basic ‘hue’ of the
// color palette every time through the loop, producing “rainbow fire”.

CRGBPalette16 gPal;

void setup() {

pinMode(BUTTON_PIN, INPUT_PULLUP); // use of PULLUP is important
pinMode(LED_PIN, OUTPUT);
delay(3000); // sanity delay
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );

// This first palette is the basic ‘black body radiation’ colors,
// which run from black to red to bright yellow to white.
gPal = LavaColors_p;

// These are other ways to set up the color palette for the ‘fire’.
// First, a gradient from black to red to yellow to white – similar to HeatColors_p
// gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);

// Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
// gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);

// Third, here’s a simpler, three-step gradient, from black to red to white
// gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);

}

void loop()
{
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy( random());

// Fourth, the most sophisticated: this one sets up a new palette every
// time through the loop, based on a hue that changes every time.
// The palette is a gradient from black, to a dark color based on the hue,
// to a light color based on the hue, to white.
//
// static uint8_t hue = 0;
// hue++;
// CRGB darkcolor = CHSV(hue,255,192); // pure hue, three-quarters brightness
// CRGB lightcolor = CHSV(hue,128,255); // half ‘whitened’, full brightness
// gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);

Fire2012WithPalette(); // run simulation frame, using palette colors

FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}

// Fire2012 by Mark Kriegsman, July 2012
// as part of “Five Elements” shown here: "Five Elements" light sculpture (2nd installation) - YouTube
////
// This basic one-dimensional ‘fire’ simulation works roughly as follows:
// There’s a underlying array of ‘heat’ cells, that model the temperature
// at each point along the line. Every cycle through the simulation,
// four steps are performed:
// 1) All cells cool down a little bit, losing heat to the air
// 2) The heat from each cell drifts ‘up’ and diffuses a little
// 3) Sometimes randomly new ‘sparks’ of heat are added at the bottom
// 4) The heat from each cell is rendered as a color into the leds array
// The heat-to-color mapping uses a black-body radiation approximation.
//
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
//
// This simulation scales it self a bit depending on NUM_LEDS; it should look
// “OK” on anywhere from 20 to 100 LEDs without too much tweaking.
//
// I recommend running this simulation at anywhere from 30-100 frames per second,
// meaning an interframe delay of about 10-35 milliseconds.
//
// Looks best on a high-density LED setup (60+ pixels/meter).
//
//
// There are two main parameters you can play with to control the look and
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
// in step 3 above).
//
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames. More cooling = shorter flames.
// Default 55, suggested range 20-100
#define COOLING 55

// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120

void Fire2012WithPalette()
{
// Array of temperature readings at each simulation cell
static byte heat[NUM_LEDS];

// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}

// Step 2.  Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
  heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}

// Step 3.  Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
  int y = random8(7);
  heat[y] = qadd8( heat[y], random8(160,255) );
}

// Step 4.  Map from heat cells to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
  // Scale the heat value from 0-255 down to 0-240
  // for best results with color palettes.
  byte colorindex = scale8( heat[j], 240);
  CRGB color = ColorFromPalette( gPal, colorindex);
  int pixelnumber;
  if( gReverseDirection ) {
    pixelnumber = (NUM_LEDS-1) - j;
  } else {
    pixelnumber = j;
  }
  leds[pixelnumber] = color;
}


if (digitalRead(BUTTON_PIN) == LOW) {
// button is pressed
digitalWrite(LED_PIN, HIGH);

} else {
// button is NOT pressed
digitalWrite(LED_PIN, LOW);
}

}

When posting code, please use http://gist.github.com and share the link. It’s easier to read on mobile devices, code doesn’t get mangled by G+, and line numbers can be referenced for discussion.

I’m not understanding how your button is interacting with or effecting FastLED at all? It looks like it just momentarily turns on the onboard LED on the controller.

There could be multiple ways to do what you want to do, but one of the simplest would be to have the button press toggle the BRIGHTNESS value to zero. You’ll need to change your code from:
#define BRIGHTNESS 300
to
uint8_t BRIGHTNESS = 255;
so it’s a variable that can be changed while the program is running.

if (digitalRead(BUTTON_PIN) == LOW) {
BRIGHTNESS = 0;
} else {
BRIGHTNESS = 255;
}

Also, note you have BRIGHTNESS set to 300. That’s equivalent to setting it to only 44 since a byte’s range is 0-255.

Oops, I was confusing LED_PIN with LED_BUILTIN in my mind. Ok, that’s why you’re seeing a change with your display, but not in a good way. By having the button press change the value of the pin you’ve set as your data pin going to the leds, you’re messing with the data steam in some fashion. In any case, try what I posted above.

Cool. Like I said, new to this. :slight_smile: Where do I put this piece go:
uint8_t BRIGHTNESS = 255;

and do I totally remove the #define Brightness 300.
Thanks!

Yes, replace the #define line with the other one.

so
#define uint8_t BRIGHTNESS = 255;
#define BRIGHTNESS 300

Hello,
Can’t seem to get this to work. I removed the #define BRIGHTNESS 300

and just have the

#define uint8_t BRIGHTNESS = 255;

in the are where all the defines are.

I then have the other code at the bottom of all the code. I’m not getting any errors, the LED comes on, but when I push the button it does not cut the brightness.

You need:
uint8_t BRIGHTNESS = 255;
(without a #define)

Also, thinking about it a bit more, I think you’ll need to add a show() line. Otherwise the button will momentarily make the change, but if the change is not displayed you’ll never see it. So in this part add a show() line like this:

if (digitalRead(BUTTON_PIN) == LOW) {
BRIGHTNESS = 0;
http://FastLED.show();
} else {
BRIGHTNESS = 255;
}

Please post a link to the updated code if it’s not working.

Huh, not working. Here’s the link.

Sorry, I completely forgot that setting the variable BRIGHTNESS to a number is only part of what’s needed. FastLED.setBrightness(BRIGHTNESS) would also need to be called to /actually set/ it.

Also, it’s probably better practice to not have the button read inside the Fire2012WithPalette function. So… here’s a few more updates for you:

First, delete lines 184-190 to just get rid of that from inside the Fire function.

Second, inside the main loop, just above FastLED.show() (above line 99) add this:

if (digitalRead(BUTTON_PIN) == LOW) {
FastLED.setBrightness( 0 ); //off
} else {
FastLED.setBrightness( 255 ); //on
}

That should do it! :slight_smile:

Wow! Thank you so MUCH!! This worked great. Now I can finish this project and then go through this code so I can figure out how this all works with the button. Thanks again.

Super, glad to hear you have it going. Share some photos or a video later if you can. Always fun to see it in action.

Hey, again, thanks for the help. Here’s the video I did for my channel. The LED is part of a cosplay prop I’m working on.

Here the main project playlist

Orb is looking great @kevin_volo , and nice videos. Thanks for sharing!

I have one big suggestion-- do not modify library files if at all possible! As soon as you start modifying library files then anytime there’s an update and you re-down load the library you’ll have to remember not to overwrite the file(s) you modified, or you’ll need to take the time to go back in and re-modify any file(s) you had changed. Error prone and time consuming. And if you accidentally break something in a library file it might make it much more troublesome for others trying to help figure out a code problem as they probably wouldn’t know you modified the library.

Instead you can make your own custom palette easily by adding it directly into the bottom of your code. Give it a custom name, ex. “purpleOrbPalette”, and then call that palette name in your code instead of the lava one you were using.

const TProgmemPalette16 purpleOrbPalette PROGMEM =
{
CRGB::Purple,
CRGB::Blue,
etc…

};

See this FastLED example for more examples of making custom palettes: