SoftPWM stops millis() when controlling more than 4 LED on a Liliypad 328

Hi

I like the SoftPWM-library for arduino a lot. Just today I found out, that millis() stops working, as soon as more than 4 LEDs are controlled using our library. I wasn't able to find a solution to that problem. Do you have an idea?

I just realized that the problem only occurs when I use the Lilypad ATMEGA 328.

On a Teensy++ 2.0, a Duemilanove with ATMEGA 328 or a ATMEGA 32U4, it works as expected, millis() increment. Any idea, what is different with the processor on the Lilypad?

Best regards,
Dani

I used your example-code (SoftPWM_LEDHead_Bounce), which uses 8 LEDs:
When checking with the serial monitor, I get constantly

26
26
26

which shows, that millis() is not incrementing.

Here is the code that doesn't work:

#include <SoftPWM.h>

#define DELAY 100
uint8_t leds[8] = {11,12,13,18,17,16,15,10};

void setup()
{
  Serial.begin(57600);
  SoftPWMBegin();
  
  for (int i = 0; i < 8; i++)
    SoftPWMSet(leds[i], 0);

  SoftPWMSetFadeTime(ALL, 50, 400);
}

void loop()
{
  int i;
  Serial.println(millis());
  for (i = 0; i < 7; i++)
  {
    SoftPWMSet(leds[i+1], 255);
    SoftPWMSet(leds[i], 0);
    delay(DELAY);
  }
  
  delay(400);
  
  for (i = 7; i > 0; i--)
  {
    SoftPWMSet(leds[i-1], 255);
    SoftPWMSet(leds[i], 0);
    delay(DELAY);
  }
  delay(400);
}

As soon as I change the code, so that it uses only 4 LEDs, it works as expected:

3014
5851
8695

...millis() is incrementing as expected.

Here is the code that works:

#include <SoftPWM.h>

#define DELAY 100
uint8_t leds[8] = {11,12,13,18,17,16,15,10};

void setup()
{
  Serial.begin(57600);
  SoftPWMBegin();
  
  for (int i = 0; i < 4; i++)
    SoftPWMSet(leds[i], 0);

  SoftPWMSetFadeTime(ALL, 50, 400);
}

void loop()
{
  int i;
  Serial.println(millis());
  for (i = 0; i < 3; i++)
  {
    SoftPWMSet(leds[i+1], 255);
    SoftPWMSet(leds[i], 0);
    delay(DELAY);
  }
  
  delay(400);
  
  for (i = 3; i > 0; i--)
  {
    SoftPWMSet(leds[i-1], 255);
    SoftPWMSet(leds[i], 0);
    delay(DELAY);
  }
  delay(400);
}

Post a link to where you downloaded the SoftPWM library from. There is an excellent library, arduino-softpwm: GitHub - Palatis/arduino-softpwm: Software PWM library for Arduino that I have used with LEDs on 15 different PWM channels without experiencing that issue. Of course you will need to make sure to adjust your PWM levels and PWM frequency settings to not create too high of an interrupt load. This can be checked with printInterruptLoad().

I have a fork of arduino-softpwm that allows you to use Arduino pin numbers: GitHub - per1234/PalatisSoftPWM: Software PWM library for Arduino.

Thank you for the link.

I have used the «official» version from Brett Hagman under GitHub - bhagman/SoftPWM: A Wiring (and Arduino) Library to produce PWM signals on arbitrary pins.. I actually like the functionality of that library a lot, since it is possible to schedule a dim like

SoftPWMSetFadeTime(ALL, 50, 400);
SoftPWMSet(leds[i], 255);

and then forget about it and the ISR does the rest. With the library you suggest, I would have to do that sort of magic within my program, do I get this correctly?

Best regards, Dani