I Need Help Looping Two Programs

Hello. I've been trying this for a while and just can't get it to work no matter how I write the code. I want my twinkle program to run for around 3 minutes then loop into my fade program, that will run for 30 seconds. It should be easy, right. I must be missing something. Here are the codes I want to combine

Twinkle

#include <FastLED.h>                                           // FastLED library
 
#define datapin 4                                              // Data pin
#define NUM_LEDS 30                                            // Number of LED's
#define COLOR_ORDER GRB                                        // Change the order as necessary
#define LED_TYPE WS2812B                                        // What kind of strip are you using?
#define BRIGHTNESS  196                                        // How bright do we want to go

struct CRGB ledsA[NUM_LEDS];                                    // Initializxe our array


// Initialize global variables for sequences
int thisdelay = 8;                                                 // A delay value for the sequence(s)


void setup(){
  LEDS.setBrightness(255);
  LEDS.addLeds<WS2812B, datapin, GRB>(ledsA, NUM_LEDS);
  memset(ledsA, 0,  NUM_LEDS * sizeof(struct CRGB)); 
}

unsigned long functionDuration = 10*60UL;//30*60000UL; 
unsigned long cycleStart;
int state;


void loop () {
  twinkle();
}

void twinkle() {
  int i = random8();                                           // A random number. Higher number => fewer twinkles. Use random16() for values >255.
  if (i < NUM_LEDS) ledsA[i] = CHSV(180, 234, 255);              // Only the lowest probability twinkles will do. You could even randomize the hue/saturation. .
  for (int j = 0; j < NUM_LEDS; j++) ledsA[j].fadeToBlackBy(8);
  
  LEDS.show();                                                // Standard FastLED display
  //show_at_max_brightness_for_power();                          // Power managed FastLED display

  delay(100);                                            // Standard delay
  //LEDS.delay(150);                                     // FastLED delay 
//  delay_at_max_brightness_for_power(thisdelay);              // Power managed FastLED delay
} // twinkle()

This is the Fade

void loop()
  {
    static uint8_t hue = 0;
    for (int px = NUM_LEDS - 1; px > 0; px--)
      {
        leds[px] = leds[px - 1];
      }
    leds[0] = CHSV(hue++, 255, 255);
    FastLED.show();
    delay(10);
  }

Thank you

An Arduino can only run one program (sketch) at a time.

That program can only have one setup() function and one loop() function.

You need to combine your sketches into one.

Something like this:

loop()
{
  twinkle();
  fade();
}

The above assumes you're using blocking code that uses delay() functions to make each block of code run from beginning to end before it returns.

If you're using non-blocking code then you'll need write a loop that keeps track of the current state and first twinkles, then fades. That's the right way to go but it's a little more complex to do.

What have done so far as far as combining your two different sketches?
Then your program will first call twinkle, then fade, then repeat the process.

1jovan2:
Hello. I've been trying this for a while and just can't get it to work no matter how I write the code. I want my twinkle program to run for around 3 minutes then loop into my fade program, that will run for 30 seconds. It should be easy, right. I must be missing something. Here are the codes I want to combine

Twinkle

#include <FastLED.h>                                           // FastLED library

#define datapin 4                                              // Data pin
#define NUM_LEDS 30                                            // Number of LED's
#define COLOR_ORDER GRB                                        // Change the order as necessary
#define LED_TYPE WS2812B                                        // What kind of strip are you using?
#define BRIGHTNESS  196                                        // How bright do we want to go

struct CRGB ledsA[NUM_LEDS];                                    // Initializxe our array

// Initialize global variables for sequences
int thisdelay = 8;                                                 // A delay value for the sequence(s)

void setup(){
  LEDS.setBrightness(255);
  LEDS.addLeds<WS2812B, datapin, GRB>(ledsA, NUM_LEDS);
  memset(ledsA, 0,  NUM_LEDS * sizeof(struct CRGB));
}

unsigned long functionDuration = 1060UL;//3060000UL;
unsigned long cycleStart;
int state;

void loop () {
  twinkle();
}

void twinkle() {
  int i = random8();                                           // A random number. Higher number => fewer twinkles. Use random16() for values >255.
  if (i < NUM_LEDS) ledsA[i] = CHSV(180, 234, 255);              // Only the lowest probability twinkles will do. You could even randomize the hue/saturation. .
  for (int j = 0; j < NUM_LEDS; j++) ledsA[j].fadeToBlackBy(8);
 
  LEDS.show();                                                // Standard FastLED display
  //show_at_max_brightness_for_power();                          // Power managed FastLED display

delay(100);                                            // Standard delay
  //LEDS.delay(150);                                     // FastLED delay
//  delay_at_max_brightness_for_power(thisdelay);              // Power managed FastLED delay
} // twinkle()





This is the Fade



void loop()
  {
    static uint8_t hue = 0;
    for (int px = NUM_LEDS - 1; px > 0; px--)
      {
        leds[px] = leds[px - 1];
      }
    leds[0] = CHSV(hue++, 255, 255);
    FastLED.show();
    delay(10);
  }




Thank you

something like this, instead...

#include <FastLED.h>                                           // FastLED library
 
#define datapin 4                                              // Data pin
#define NUM_LEDS 30                                            // Number of LED's
#define COLOR_ORDER GRB                                        // Change the order as necessary
#define LED_TYPE WS2812B                                        // What kind of strip are you using?
#define BRIGHTNESS  196                                        // How bright do we want to go

struct CRGB ledsA[NUM_LEDS];                                    // Initializxe our array


int thisdelay = 8;  
unsigned long functionDuration = 3*60*1000UL;
unsigned long totalDuration = 3*60*1000UL + 30000UL;
unsigned long cycleStart;
int state;

void setup()
{
  LEDS.setBrightness(255);
  LEDS.addLeds<WS2812B, datapin, GRB>(ledsA, NUM_LEDS);
  memset(ledsA, 0,  NUM_LEDS * sizeof(struct CRGB)); 
}



void loop () 
{
  if (millis() - cycleStart < functionDuration)
  {
    twinkle();
  }
  else if (millis() - cycleStart < totalDuration)
  {
    fade();
  }
  else
  {
    cycleStart = millis();
  }
}

void twinkle() 
{
  int i = random8();                                           
  if (i < NUM_LEDS) ledsA[i] = CHSV(180, 234, 255);             
  for (int j = 0; j < NUM_LEDS; j++) ledsA[j].fadeToBlackBy(8);
  LEDS.show();                                                
  delay(100);
}
//
void fade()
{
  static uint8_t hue = 0;
  for (int px = NUM_LEDS - 1; px > 0; px--)
  {
    leds[px] = leds[px - 1];
  }
  leds[0] = CHSV(hue++, 255, 255);
  FastLED.show();
  delay(10);
}