Run Function Throughout Program

I have created a function called “cyclotron” in my sketch to make a 3-LED chaser that should run all the time, regardless of what else is going on in the sketch. I am not sure if I should test against time, or if there is a better way to schedule the task. Please let me know your thoughts. Here is my complete code:

#include <Adafruit_NeoPixel.h>

// Define pin to connect Neopixels to
#define PIN 6

//Define pin for pushbutton to connect to
const int BUTTON = 2;

//Initialize value to determine button press HIGH or LOW
int val = 0;

//Initialize values for Cyclotron LEDs
int led1 = 13;
int led2 = 12;
int led3 = 11;

//Initilize counter
long count = 0;

//Define variable to read millis()
unsigned long start;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:

Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off
}

void loop() {
  
  val = digitalRead(BUTTON);
  
  unsigned long time;
  
  if (val == HIGH) {
    
    if (count == 0) {
      start = millis();
    }
    
    colorWipe2(strip.Color(139, 0, 0), 50); // Display all Neopixels as red
    clearStrip();
    count++;
    
    if ((millis() - start) >= 10000) {
      time = (millis() - start);
      do {
      val = digitalRead(BUTTON);
      colorWipe3(strip.Color(255, 255, 255), 50); // Display all Neopixels as white
      clearStrip();
      } while (val == HIGH && time >= 10000);
    }
  }
  
  else {
    colorWipe(strip.Color(0, 0, 255), 50); // Display all Neopixels as blue
    clearStrip();
  }
  
}

// Fill the dots one after the other with a color at speed 1
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(35);
  }
}

// Fill the dots one after the other with a color at speed 2
void colorWipe2(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(25);
  }
}

// Fill the dots one after the other with a color at speed 3
void colorWipe3(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(15);
  }
}

// Turn off all pixels
void clearStrip() {
  for( int i = 0; i<16; i++){
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

// 3-LED Chaser
void cyclotron() {
  for(int i = 11; i <= 13; i++) {
    digitalWrite(i, HIGH);
    delay(1000);
    digitalWrite(i, LOW);
  }
}

Sorry, I only have time for a quick - "I think you're about to get an education in ..." 1) using assembly code in a sketch. 2) reading the ATmega datasheet. 3) setting Timer1. 4) using the Timer interrupt 5) putting the LED-moving code in the interrupt-called routine. Good luck! PS. Perhaps search the forum using those new keywords?

Think about reorganiing your code so that loop() looks like this

void loop() { readButton(); doColorWipes(); cyclotron(); }

Don't use delay() anywhere - use the technique from the Blink Without Delay example sketch

Organise all your functions ( the -wipes and the cyclotron) so they only execute one iteration of their loop every time they are called.

The demo in the first post of this Thread might give you the idea.

...R