Something as 8LED snake

I simply try to create a short LED wave that circularly switch 3LED´s with diferent intensity. Can somebody explain why this routine don´t work as I want?

run light.ino - Wokwi Arduino and ESP32 Simulator

analogWrite() requires a PWM pin. only those with a ~ next to their number qualify

There are software PWM libraries.
I tried a software PWM:

// For: https://forum.arduino.cc/t/something-as-8led-snake/1039552
// Using the SoftPWM library: https://github.com/bhagman/SoftPWM
// There is a other one: https://github.com/Palatis/arduino-softpwm
//

#include <SoftPWM.h>

void setup()
{
  SoftPWMBegin();

  for (int i = 2; i <= 9; i++)         // pin 2 up to 9
    SoftPWMSet(i, 0);

  SoftPWMSetFadeTime(ALL, 50, 100);
}

void loop()
{
  for (int i=2; i<9; i++)
  {
    SoftPWMSet(i, 255);
    delay(100);
    SoftPWMSet(i, 0);
  }
  for (int i=9; i>=2; i--)
  {
    SoftPWMSet(i, 255);
    delay(100);
    SoftPWMSet(i, 0);
  }
}

The sketch in Wokwi: 8LED-snake.ino - Wokwi Arduino and ESP32 Simulator

You could copy my diagram.json to your Wokwi project.

I don't know if my sketch uses the library correctly. You have to check that yourself.

Watch your sketch in the wokwi.

I see you have a few special cases, but consider what happens when thisPin gets up near highestPin.

Oh, actually I see your sketch works perfectly.

Of course your sketch is doing exactly what you said.

So never mind.

a7

I used the library @Koepel mentioned and just used it in @tombugkh's sketch, just replacing the analogWrite() calls:

The wokwi isn't that good at LED display, soI had to adjust the dimness numbers. And I believe I cancelled the library fade out idea, which is entirely exploitable but I wanted to see what the OP's original logic turned out.

But to really see Imma have to build a little one IRL.

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.