Neopixel toggle swich implementation

Hey, I’m just started to get into arduino and I have a Arduino micro and neopixel 24 pixel ring. So I have three codes that I want to make pushbutton toggleable:

#include <FastLED.h>

#define NUM_LEDS 24

// Data pin that led data will be written out over
#define DATA_PIN 10

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

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

int Led1 = 24;  // Setting LED on neopixel ring
int Led2 = 0;   // Setting LED on neopixel ring
int Led3 = 1;   // Setting LED on neopixel ring
int Led4 = 2;   // Setting LED on neopixel ring
int Led5 = 3;   // Setting LED on neopixel ring
int Led6 = 4;   // Setting LED on neopixel ring
int Led7 = 5;   // Setting LED on neopixel ring

int Led11 = 11; // Setting LED on neopixel ring
int Led12 = 12; // Setting LED on neopixel ring
int Led13 = 13; // Setting LED on neopixel ring
int Led14 = 14; // Setting LED on neopixel ring
int Led15 = 15; // Setting LED on neopixel ring
int Led16 = 16; // Setting LED on neopixel ring
int Led17 = 17; // Setting LED on neopixel ring

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

void setup() {
  pinMode(A1, INPUT); // Data output pin
  Serial.begin(9600); // For debuging
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS); // Library setup
}

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

void loop() {
  leds[Led1] = CRGB(0, 0, 0);     // Seting LED off
  leds[Led11] = CRGB(0, 0, 0);    // Seting LED off

  leds[Led2] = CRGB(5, 21, 0);    // Seting LED on
  leds[Led12] = CRGB(5, 21, 0);   // Seting LED on

  leds[Led3] = CRGB(23, 85, 0);   // Seting LED on
  leds[Led13] = CRGB(23, 85, 0);  // Seting LED on

  leds[Led4] = CRGB(23, 85, 0);   // Seting LED on
  leds[Led14] = CRGB(23, 85, 0);  // Seting LED on

  leds[Led5] = CRGB(23, 85, 0);   // Seting LED on
  leds[Led15] = CRGB(23, 85, 0);  // Seting LED on

  leds[Led6] = CRGB(7, 28, 0);    // Seting LED on
  leds[Led16] = CRGB(7, 28, 0);   // Seting LED on

  leds[Led7] = CRGB(5, 21, 0);    // Seting LED on
  leds[Led17] = CRGB(5, 21, 0);   // Seting LED on

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

  FastLED.show();   // Displaying on/off LED's to physical neopixel ring
  delay(100);       // Delaying (rotation speed)

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

  // Adding 1 to each int to "move" 1 Led further

  Led1 ++;
  Led11 ++;

  Led2 ++;
  Led12 ++;

  Led3 ++;
  Led13 ++;

  Led4 ++;
  Led14 ++;

  Led5 ++;
  Led15 ++;

  Led6 ++;
  Led16 ++;

  Led7 ++;
  Led17 ++;

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

  // When LED reaches last LED setting it to first Led to create a loop

  if (Led1 >= 24) {
    Led1 = 0;
  }
  if (Led2 == 24) {
    Led2 = 0;
  }
  if (Led3 == 24) {
    Led3 = 0;
  }
  if (Led4 == 24) {
    Led4 = 0;
  }
  
  if (Led5 == 24) {
    Led5 = 0;
  }
  
  if (Led6 == 24) {
    Led6 = 0;
  }
  
  if (Led7 == 24) {
    Led7 = 0;
  }
  
  if (Led11 == 24) {
    Led11 = 0;
  }
  
  if (Led12 == 24) {
    Led12 = 0;
  }
  
  if (Led13 == 24) {
    Led13 = 0;
  }
  
  if (Led14 == 24) {
    Led14 = 0;
  }
  
  if (Led15 == 24) {
    Led15 = 0;
  }
  
  if (Led16 == 24) {
    Led16 = 0;
  }
  
  if (Led17 == 24) {
    Led17 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////
#include "FastLED.h"

#define NUM_LEDS 24

// Data pin that led data will be written out over
#define DATA_PIN 10

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

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

int Led1 = 0; // Setting LED on neopixel ring
int Led2 = 1; // Setting LED on neopixel ring
int Led3 = 2; // Setting LED on neopixel ring
int Led4 = 3; // Setting LED on neopixel ring
int Led5 = 4; // Setting LED on neopixel ring
int Led6 = 5; // Setting LED on neopixel ring

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

void setup() {
  pinMode(A1, INPUT); // Data output pin
  Serial.begin(9600); // For debuging
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); //Library setup
}

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

void loop() {
  leds[Led1] = CRGB(0,0,0);     // Seting LED off
  leds[Led2] = CRGB(85,23,0);   // Seting LED on
  leds[Led3] = CRGB(85,23,0);   // Seting LED on
  leds[Led4] = CRGB(85,23,0);   // Seting LED on
  leds[Led5] = CRGB(85,23,0);   // Seting LED on
  leds[Led6] = CRGB(85,23,0);   // Seting LED on

///////////////////////////////////////////////////////////////////////////
  
  FastLED.show();   // Displaying on/off LED's to physical neopixel ring
  delay(100);       // Delaying (rotation speed)

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

  // Adding 1 to each int to "move" 1 Led further

  Led1 ++;
  Led2 ++;
  Led3 ++;
  Led4 ++;
  Led5 ++;
  Led6 ++;

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

  // When LED reaches last LED setting it to first Led to create a loop

  if(Led1==24){
    Led1 = 0;
  }
  
 if(Led2==24){
    Led2 = 0;
  }

  if(Led3==24){
    Led3 = 0;
  }
  
  if(Led4==24){
    Led4 = 0;
  }

  if(Led5==24){
    Led5 = 0;
  }
  
  if(Led6==24){
    Led6 = 0;
  }
    
}
///////////////////////////////////////////////////////////////////////////
#include "FastLED.h"

#define NUM_LEDS 24

// Data pin that led data will be written out over
#define DATA_PIN 10

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

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

int Led1 = 0; // Setting LED on neopixel ring
int Led2 = 1; // Setting LED on neopixel ring
int Led3 = 2; // Setting LED on neopixel ring
int Led4 = 3; // Setting LED on neopixel ring
int Led5 = 4; // Setting LED on neopixel ring
int Led6 = 5; // Setting LED on neopixel ring

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

void setup() {
  pinMode(A1, INPUT); // Data output pin
  Serial.begin(9600); // For debuging
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); //Library setup

  for(int i=0; i<=24; i++) {                // Fill neopixel ring

    leds[i] = leds[Led2] = CRGB(85,23,0);   // Seting LED on
    FastLED.show();                         // Displaying on/off LED's to physical neopixel ring
    
  }
  
}

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

void loop(){}

I know code is not perfect and needs optimizations which I have problems too. Anyways could you help me to make it toggle between theese codes even in the middle of code execution, if posible?

Thanks for any help! :slight_smile:

There is a thread with a similar requirement here http://forum.arduino.cc/index.php?topic=513293.0

There must be nothing in the program to slow it down, which implies that delay() should not be used. Switching between states is probably best done with switch/case to enable only the required code to be run for the current state.