Buttoncycler and CenterFire sketch toggle

I am attempting to toggle between a simple "strip.color" found within Examples> Adafruit Noepixel>buttoncycler and the "Centerfire"sketch found at:

The problem I am having is that while I can get either the sketch to work by themselves I have been unable to successfully include the Centerfire sketch as a toggle item. The setup values seem to be consistent between them.

My thought was that I should be able to somehow replace the "case3:" to "toggle" over to the centerfire sketch. when the button is depressed... its probably something simple but epic fail for me.

Any help,direction appreciated.

The sketches are included below. The buttoncycler was edited to a much shorter version and verified working. The CenterFire sketch has only pin call out and pin location changes for consistancy between the sketches.

Any help direction appreciated.

//buttoncycler_short
//Cycle between Adafruit Neopixel>buttoncycler  control of all on strip and centerfire sketch


// Simple demonstration on using an input device to trigger changes on your
// NeoPixels. Wire a momentary push button to connect from ground to a
// digital IO pin. When the button is pressed it will change to a new pixel
// animation. Initial state has all pixels off -- press the button once to
// start the first animation. As written, the button does not interrupt an
// animation in-progress, it works only when idle.

#include <Adafruit_NeoPixel.h>

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

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

#define NUM_LEDS 10  // Number of NeoPixels 70 when working

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(NUM_LEDS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'
}

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

  // 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) {      // Yes, still low
      if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
      switch(mode) {           // Start the new animation...
        case 0:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 1:
          colorWipe(strip.Color(240,   240,   240), 240);    // White
          break;
        case 2:
          colorWipe(strip.Color(  0, 255,   0), 50);    // Green
          break;
        case 3:
          colorWipe(strip.Color(  0,   0, 255), 50);    // Blue
          break;
        
      }
    }
  }
  // Set the last-read button state to the old state.
  oldState = newState;
}

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

Centerfire Sketch to be toggled:


//https://forum.arduino.cc/t/help-starting-fire-code-in-middle-of-strip-going-out-to-both-ends/575890

//

//
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 6
#define PIXEL_PIN  2  // Digital IO pin connected to the NeoPixels.
#define NUM_LEDS 10   // set to an even number
#define MIRROR 5     // set to 1/2 of the LED count
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  Fire(55,120,15,1);
}

void Fire(int Cooling, int Sparking, int SpeedDelay, int Mirror) {
  static byte heat[NUM_LEDS];
  int cooldown;
  
  // Step 1.  Cool down every cell a little
  for( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
    
    if(cooldown>heat[i]) {
      heat[i]=0;
    } else {
      heat[i]=heat[i]-cooldown;
    }
  }
  
  // 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' near the bottom
  if( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160,255);
    //heat[y] = random(160,255);
  }

  // Step 4.  Convert heat to LED colors
  for( int j = 0; j < NUM_LEDS - MIRROR; j++) {
    setPixelHeatColor(j+MIRROR, heat[j] );
    setPixelHeatColor(MIRROR-j, heat[j] );
    }

  showStrip();
  delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature/255.0)*191);
 
  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252
 
  // figure out which third of the spectrum we're in:
  if( t192 > 0x80) {                     // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if( t192 > 0x40 ) {             // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

In what way does it fail?

Show a sketch containing both things that are to be done.

fire's looks nice..

fudging things around, loose all the blocking code..
changed the button processing..

seems good..

#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 6
#define PIXEL_PIN  2  // Digital IO pin connected to the NeoPixels.
#define NUM_LEDS 10   // set to an even number
#define MIRROR 5     // set to 1/2 of the LED count
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-4
byte lastMode = 0;
unsigned long lastRead;
int intervalDebounce = 50;
byte aniStep = 0;
int CurrentAni = 0;
uint32_t CurrentColor = 0;
int  CurrentPixel = 0;
unsigned long msecLastAniStep = 0;
int AniStepDelay = 250;
bool AniDone = true;


void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {

  unsigned long now = millis();

  StepAni();

  if (now - lastRead >= intervalDebounce) {
    // Get current button state.
    boolean newState = digitalRead(BUTTON_PIN);
    if (newState != oldState) {
      lastRead = now;//start debouncing..
      oldState = newState;
      if (newState == LOW) {     // Yes, still low
        if (++mode > 4) mode = 0; // Advance to next mode, wrap around after #8
      }
    }
  }

  if (mode != lastMode) {
    switch (mode) {          // Start the new animation...
      case 0:
        AniDone = false;
        CurrentPixel = 0;
        colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
        break;
      case 1:
        AniDone = false;
        CurrentPixel = 0;
        colorWipe(strip.Color(240,   240,   240), 50);    // White
        break;
      case 2:
        AniDone = false;
        CurrentPixel = 0;
        colorWipe(strip.Color(  0, 255,   0), 50);    // Green
        break;
      case 3:
        AniDone = false;
        CurrentPixel = 0;
        colorWipe(strip.Color(  0,   0, 255), 50);    // Blue
        break;
      case 4:
        CurrentAni = 1;//fire
        AniStepDelay = 15;
        colorBlack();
        break;
    }
    lastMode = mode;
  }
}


void StepAni ()
{ //see if it's time for a step..
  unsigned long currMilli = millis();
  if (currMilli - msecLastAniStep >= AniStepDelay)
  {
    msecLastAniStep = currMilli;
    switch (CurrentAni) {
      case 0: colorWipe(CurrentColor, AniStepDelay); break;
      case 1: Fire(55, 120, 15, 1); break;
    }
  }
}


void colorBlack() {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, strip.Color(  0,   0,   0));
  }
  strip.show();
}

void colorWipe(uint32_t color, unsigned long wait)
{
  CurrentAni = 0;
  AniStepDelay = wait;
  CurrentColor  = color;

  if (!AniDone)
  {
    strip.setPixelColor(CurrentPixel, CurrentColor);
    strip.show();
  }

  if (CurrentPixel < strip.numPixels())
  {
    CurrentPixel++;
  } else
  {
    CurrentPixel = 0;
    AniDone = true;
  }
}


void Fire(int Cooling, int Sparking, int SpeedDelay, int Mirror) {
  static byte heat[NUM_LEDS];
  int cooldown;

  // Step 1.  Cool down every cell a little
  for ( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);

    if (cooldown > heat[i]) {
      heat[i] = 0;
    } else {
      heat[i] = heat[i] - cooldown;
    }
  }

  // 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' near the bottom
  if ( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160, 255);
    //heat[y] = random(160,255);
  }

  // Step 4.  Convert heat to LED colors
  for ( int j = 0; j < NUM_LEDS - MIRROR; j++) {
    setPixelHeatColor(j + MIRROR, heat[j] );
    setPixelHeatColor(MIRROR - j, heat[j] );
  }

  showStrip();
  // delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature / 255.0) * 191);

  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252

  // figure out which third of the spectrum we're in:
  if ( t192 > 0x80) {                    // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if ( t192 > 0x40 ) {            // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

Code simmed here..

have fun.. ~q

qubits-us
Thank You!!!
This works exactly as needed.

Im a bit of a novice here and the fire sketch came from "damadscientist"..I too thought he did a great job with it.

Question: I'm not familiar with the use of "StepAni" can you comment on where I can learn more about this one?

Thanks again

You're welcome..

StepAni is just a function that steps the animations instead of using blocking delay functions..
Ported some blocking code to non-blocking few months back..
One of the anis was colorWipe, so yeah, not my first rodeo.. :slight_smile:

there's a few more in there you could port..

AsyncLeds..

have fun.. ~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.