Multiplexing with SoftPWM

Hi, guys!!

I’m trying to sort out a problem. Maybe you could help me…

I’ve been using an Arduino Mega 2560, trying to get an effect of simultaneity in two actions (two different programs) with 54 LEDs in total.

First action: 30 LEDs disposed in circle, running as a ‘‘loading’’ icon (each LED turns on and starts to fade out in a cycle gradually).
Here is the code I used for this:

#include <SoftPWM.h>

#define DELAY 160
uint8_t leds[30] = {14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43};

void setup()
{
  SoftPWMBegin();

  for (int i = 0; i < 30; i++)
    SoftPWMSet(leds[i], 0);
}


void loop() //Repeat Loops ONE and TWO
{
  int i = 0;
    
  delay(0);
  
  if (i < 100) {
  loop1();
  i++;
  }
  
  if (i < 100) {
  loop1();
  i++;
  }
  
  delay(4500); //Delay between Loops
  
  if (i < 100) {
  loop2();
  i++;
  } 
  
  if (i < 100) {
  loop2();
  i++;
  } 
  
  if (i < 100) {
  loop2();
  i++;
  } 
  
}

void loop1()
{
  SoftPWMSetFadeTime(ALL, 100, 3000);
  for (int i = 0; i < 30; i++)
  {
    SoftPWMSet(leds[i], 255);
    if (i != 0)
    SoftPWMSet(leds[i-1], 0);
    delay(DELAY);
  }
SoftPWMSet(leds[29], 0);
}

void loop2() 
{
  SoftPWMSetFadeTime(ALL, 700, 3000);
    SoftPWMSet(ALL, 255);
    delay(600);
    SoftPWMSet(ALL, 0);
    delay(3000);
  }

Second Action: 24 LEDs that just have to fade on and fade out together:
Here is the code:

#include <SoftPWM.h>

#define DELAY 160
uint8_t leds[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};

void setup()
{
  SoftPWMBegin();

  for (int i = 0; i < 24; i++)
    SoftPWMSet(leds[i], 0);
}


void loop() //Repeat Loops ONE and TWO
{
  int i = 0;
    
  delay(0);
  
  if (i < 100) {
  loop1();
  i++;
  }
  
  if (i < 100) {
  loop1();
  i++;
  }
  
  delay(4500); //Delay between Loops
  
  if (i < 100) {
  loop2();
  i++;
  } 
  
  if (i < 100) {
  loop2();
  i++;
  } 
  
  if (i < 100) {
  loop2();
  i++;
  } 
  
}

void loop1()
{
  SoftPWMSetFadeTime(ALL, 100, 3000);
  for (int i = 0; i < 24; i++)
  {
    SoftPWMSet(leds[i], 255);
    if (i != 0)
    SoftPWMSet(leds[i-1], 0);
    delay(DELAY);
  }
SoftPWMSet(leds[24], 0);
}

void loop2() 
{
  SoftPWMSetFadeTime(ALL, 700, 3000);
    SoftPWMSet(ALL, 255);
    delay(600);
    SoftPWMSet(ALL, 0);
    delay(3000);
  }

Of course, these codes look strange because they were modified for my needs here but each of them works great individually! :slight_smile:
My real question is: Would it be possible to make these two programs run at the same time since I have so many LEDs fading out using ‘‘SoftPWM’’?

Do you see any way out with (maybe) other kind of program?

Lots of thanks in advance,
Raffael