# Something as 8LED snake

**URL:** <https://forum.arduino.cc/t/something-as-8led-snake/1039552>\
**Category:** Programming\
**Created:** [October 7, 2022, 8:33pm UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552 "2022-10-07T20:33:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tombugkh](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@tombugkh](https://forum.arduino.cc/u/tombugkh)\
**Post date:** [October 7, 2022, 8:33pm UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552/1 "2022-10-07T20:33:35Z")

</div>

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](https://wokwi.com/projects/344881630386586196)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 7, 2022, 8:47pm UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552/2 "2022-10-07T20:47:58Z")

</div>

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

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/e/c/bec43f59ba71451adb90bef4d8746c27274b12e3.png)

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [October 7, 2022, 9:33pm UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552/3 "2022-10-07T21:33:01Z")

</div>

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

```cpp
// 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](https://wokwi.com/projects/344895784657879636)

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.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 7, 2022, 10:21pm UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552/4 "2022-10-07T22:21:20Z")

</div>

> [@tombugkh](#):
>
> explain why this routine don´t work as I want?

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 7, 2022, 10:42pm UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552/5 "2022-10-07T22:42:43Z")

</div>

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

> **[saveACopy.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/344900635140817491)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 27, 2023, 9:42am UTC](https://forum.arduino.cc/t/something-as-8led-snake/1039552/7 "2023-05-27T09:42:14Z")

</div>

Closed on May 27, 2023, 9:42am UTC

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