Pushbutton trigger for advancing Neopixel animations?

Greetings all,

I have used the Neopixel Code Generator to build build some basic animations, but I was curious if anyone could tell me how to modify the code so that the animation sequences can be advanced with a pushbutton trigger, instead of just a round robin loop. Here's the code from the generator:

#include <Adafruit_NeoPixel.h>

class Strip
{
public:
  uint8_t   effect;
  uint8_t   effects;
  uint16_t  effStep;
  unsigned long effStart;
  Adafruit_NeoPixel strip;
  Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, pin, striptype) {
    effect = -1;
    effects = toteffects;
    Reset();
  }
  void Reset(){
    effStep = 0;
    effect = (effect + 1) % effects;
    effStart = millis();
  }
};

struct Loop
{
  uint8_t currentChild;
  uint8_t childs;
  bool timeBased;
  uint16_t cycles;
  uint16_t currentTime;
  Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {currentTime=0;currentChild=0;childs=totchilds;timeBased=timebased;cycles=tottime;}
};

Strip strip_0(14, 8, 14, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(4, false, 1);
struct Loop strip0loop00(2, false, 1);
struct Loop strip0loop01(2, false, 1);

//[GLOBAL_VARIABLES]

void setup() {

  //Your setup here:

  strip_0.strip.begin();
}

void loop() {

  //Your code here:

  strips_loop();
}

void strips_loop() {
  if(strip0_loop0() & 0x01)
    strip_0.strip.show();
}

uint8_t strip0_loop0() {
  uint8_t ret = 0x00;
  switch(strip0loop0.currentChild) {
    case 0: 
           ret = strip0_loop0_eff0();break;
    case 1: 
           ret = strip0_loop0_eff1();break;
    case 2: 
           ret = strip0_loop00();break;
    case 3: 
           ret = strip0_loop01();break;
  }
  if(ret & 0x02) {
    ret &= 0xfd;
    if(strip0loop0.currentChild + 1 >= strip0loop0.childs) {
      strip0loop0.currentChild = 0;
      if(++strip0loop0.currentTime >= strip0loop0.cycles) {strip0loop0.currentTime = 0; ret |= 0x02;}
    }
    else {
      strip0loop0.currentChild++;
    }
  };
  return ret;
}

uint8_t strip0_loop0_eff0() {
    // Strip ID: 0 - Effect: Fade - LEDS: 14
    // Steps: 996 - Delay: 1
    // Colors: 2 (255.0.0, 255.130.0)
    // Options: duration=996, every=1, 
  if(millis() - strip_0.effStart < 1 * (strip_0.effStep)) return 0x00;
  uint8_t r,g,b;
  double e;
  e = (strip_0.effStep * 1) / (double)996;
  r = ( e ) * 255 + 255 * ( 1.0 - e );
  g = ( e ) * 130 + 0 * ( 1.0 - e );
  b = ( e ) * 0 + 0 * ( 1.0 - e );
  for(uint16_t j=0;j<14;j++) {
    if((j % 1) == 0)
      strip_0.strip.setPixelColor(j, r, g, b);
    else
      strip_0.strip.setPixelColor(j, 0, 0, 0);
  }
  if(strip_0.effStep >= 996) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

uint8_t strip0_loop0_eff1() {
    // Strip ID: 0 - Effect: Fade - LEDS: 14
    // Steps: 990 - Delay: 1
    // Colors: 2 (255.130.0, 255.0.0)
    // Options: duration=990, every=1, 
  if(millis() - strip_0.effStart < 1 * (strip_0.effStep)) return 0x00;
  uint8_t r,g,b;
  double e;
  e = (strip_0.effStep * 1) / (double)990;
  r = ( e ) * 255 + 255 * ( 1.0 - e );
  g = ( e ) * 0 + 130 * ( 1.0 - e );
  b = ( e ) * 0 + 0 * ( 1.0 - e );
  for(uint16_t j=0;j<14;j++) {
    if((j % 1) == 0)
      strip_0.strip.setPixelColor(j, r, g, b);
    else
      strip_0.strip.setPixelColor(j, 0, 0, 0);
  }
  if(strip_0.effStep >= 990) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

uint8_t strip0_loop00() {
  uint8_t ret = 0x00;
  switch(strip0loop00.currentChild) {
    case 0: 
           ret = strip0_loop00_eff0();break;
    case 1: 
           ret = strip0_loop00_eff1();break;
  }
  if(ret & 0x02) {
    ret &= 0xfd;
    if(strip0loop00.currentChild + 1 >= strip0loop00.childs) {
      strip0loop00.currentChild = 0;
      if(++strip0loop00.currentTime >= strip0loop00.cycles) {strip0loop00.currentTime = 0; ret |= 0x02;}
    }
    else {
      strip0loop00.currentChild++;
    }
  };
  return ret;
}

uint8_t strip0_loop00_eff0() {
    // Strip ID: 0 - Effect: Move - LEDS: 14
    // Steps: 13 - Delay: 50
    // Colors: 0 ()
    // Options: toLeft=true, rotate=false, 
  if(millis() - strip_0.effStart < 50 * (strip_0.effStep)) return 0x00;
  for(uint16_t j=0;j<13;j++) 
    strip_0.strip.setPixelColor(j, strip_0.strip.getPixelColor(j+1)); 
  if(strip_0.effStep >= 13) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

uint8_t strip0_loop00_eff1() {
    // Strip ID: 0 - Effect: Move - LEDS: 14
    // Steps: 14 - Delay: 50
    // Colors: 0 ()
    // Options: toLeft=false, rotate=false, 
  if(millis() - strip_0.effStart < 50 * (strip_0.effStep)) return 0x00;
  for(uint16_t j=14-1;j>0;j--) 
    strip_0.strip.setPixelColor(j, strip_0.strip.getPixelColor(j-1)); 
  if(strip_0.effStep >= 14) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

uint8_t strip0_loop01() {
  uint8_t ret = 0x00;
  switch(strip0loop01.currentChild) {
    case 0: 
           ret = strip0_loop01_eff0();break;
    case 1: 
           ret = strip0_loop01_eff1();break;
  }
  if(ret & 0x02) {
    ret &= 0xfd;
    if(strip0loop01.currentChild + 1 >= strip0loop01.childs) {
      strip0loop01.currentChild = 0;
      if(++strip0loop01.currentTime >= strip0loop01.cycles) {strip0loop01.currentTime = 0; ret |= 0x02;}
    }
    else {
      strip0loop01.currentChild++;
    }
  };
  return ret;
}

uint8_t strip0_loop01_eff0() {
    // Strip ID: 0 - Effect: Blink - LEDS: 14
    // Steps: 115 - Delay: 10
    // Colors: 2 (255.130.0, 255.0.0)
    // Options: timeBegin=500, timeToOn=50, timeOn=50, timeToOff=50, timeOver=500, every=2, 
  if(millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
  uint8_t e,r,g,b;
  if(strip_0.effStep < 50) {
    for(uint16_t j=0;j<14;j++) 
      strip_0.strip.setPixelColor(j, 255, 130, 0);
  }
  else if(strip_0.effStep  < 55) {
    e = (strip_0.effStep * 10) - 500;
    r = 255 * ( e / 50 ) + 255 * ( 1.0 - e / 50 );
    g = 0 * ( e / 50 ) + 130 * ( 1.0 - e / 50 );
    b = 0 * ( e / 50 ) + 0 * ( 1.0 - e / 50 );
    for(uint16_t j=0;j<14;j++) 
      if((j%2)==0) strip_0.strip.setPixelColor(j, r, g, b);
      else strip_0.strip.setPixelColor(j, 255, 130, 0);
  }
  else if(strip_0.effStep < 60) {
    for(uint16_t j=0;j<14;j++) 
      if((j%2)==0) strip_0.strip.setPixelColor(j, 255, 0, 0);
      else strip_0.strip.setPixelColor(j, 255, 130, 0);
  }
  else if(strip_0.effStep < 65) {
    e = (strip_0.effStep * 10) - 600;
    r = 255 * ( e / 50 ) + 255 * ( 1.0 - e / 50 );
    g = 130 * ( e / 50 ) + 0 * ( 1.0 - e / 50 );
    b = 0 * ( e / 50 ) + 0 * ( 1.0 - e / 50 );
    for(uint16_t j=0;j<14;j++) 
      if((j%2)==0) strip_0.strip.setPixelColor(j, r, g, b);
      else strip_0.strip.setPixelColor(j, 255, 130, 0);
  }
  else {
    for(uint16_t j=0;j<14;j++) 
      strip_0.strip.setPixelColor(j, 255, 130, 0);
  }
  if(strip_0.effStep >= 115) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

uint8_t strip0_loop01_eff1() {
    // Strip ID: 0 - Effect: Blink - LEDS: 14
    // Steps: 115 - Delay: 10
    // Colors: 2 (255.0.0, 255.130.0)
    // Options: timeBegin=500, timeToOn=50, timeOn=50, timeToOff=50, timeOver=500, every=2, 
  if(millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
  uint8_t e,r,g,b;
  if(strip_0.effStep < 50) {
    for(uint16_t j=0;j<14;j++) 
      strip_0.strip.setPixelColor(j, 255, 0, 0);
  }
  else if(strip_0.effStep  < 55) {
    e = (strip_0.effStep * 10) - 500;
    r = 255 * ( e / 50 ) + 255 * ( 1.0 - e / 50 );
    g = 130 * ( e / 50 ) + 0 * ( 1.0 - e / 50 );
    b = 0 * ( e / 50 ) + 0 * ( 1.0 - e / 50 );
    for(uint16_t j=0;j<14;j++) 
      if((j%2)==0) strip_0.strip.setPixelColor(j, r, g, b);
      else strip_0.strip.setPixelColor(j, 255, 0, 0);
  }
  else if(strip_0.effStep < 60) {
    for(uint16_t j=0;j<14;j++) 
      if((j%2)==0) strip_0.strip.setPixelColor(j, 255, 130, 0);
      else strip_0.strip.setPixelColor(j, 255, 0, 0);
  }
  else if(strip_0.effStep < 65) {
    e = (strip_0.effStep * 10) - 600;
    r = 255 * ( e / 50 ) + 255 * ( 1.0 - e / 50 );
    g = 0 * ( e / 50 ) + 130 * ( 1.0 - e / 50 );
    b = 0 * ( e / 50 ) + 0 * ( 1.0 - e / 50 );
    for(uint16_t j=0;j<14;j++) 
      if((j%2)==0) strip_0.strip.setPixelColor(j, r, g, b);
      else strip_0.strip.setPixelColor(j, 255, 0, 0);
  }
  else {
    for(uint16_t j=0;j<14;j++) 
      strip_0.strip.setPixelColor(j, 255, 0, 0);
  }
  if(strip_0.effStep >= 115) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

Can anyone assist?

Why do users choose to ignore the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

Sorry I'm new to this. Thanks for your suggestion! I have made the adjustment.

Thank you

I hope that you can see how much easier the code is to see and deal with when code tags are used

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