Neopixel led working on Arduino and not on esp32

I'm having an issue using Neopixel library, my code work perfectly on Arduino Uno or Mega and not on ESP32, on esp32 only the countdown part works not the "rabbit" part, but because i would like to have even a webserver for change the settings, i would like to have it running on esp32.

#include <Adafruit_NeoPixel.h>

#define PIN 5
#define NUM_LEDS 150
#define BRIGHTNESS 64
#define BUTTON 15
#define UPDATES_PER_SECOND 100
boolean oldState = HIGH;
int BUTTONstate = 0;

int started = 0;

int vaschePercorse = 0;
unsigned long startingMillis = 0;
int delayLepri = 2000;

int ledWidth = 3;
int ledJump = 1;

int tipFromStart = ledWidth - 1;
int tipFromEnd = NUM_LEDS - ledWidth;

int lepriColors[4][3] = {{255,0,0}, {255,255,255}, {0,255,0}, {0,0,255}};
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

int DELAY = 1;
String Nlepri = "3";
String Nvasche = "2";
String Intervallo = "3000";
String Nripetizioni = "3";
String Blocco = "1"; // 1 parti dal blocco

int Nlepri_int = Nlepri.toInt();
int Nvasche_int = Nvasche.toInt();
unsigned long Intervallo_int = Intervallo.toInt();
int Nripetizioni_int = Nripetizioni.toInt();
int Blocco_int = Blocco.toInt();
int Nterminated = 0;

int getStartingTip(int blocco)
{
    if (blocco == 1)
    {
        return tipFromStart;
    }
    else
    {
        return tipFromEnd;
    }
}

// Lepre state [
//    0 - Led Tip,
//    1 - direction (1 go right, 0 go left)
//    2 - vasche percorse,
//    3 - ripetizioni effettuate,
//    4 - ferma / partita (0, 1)
//    5 - millis inizio intervallo
//    6 - terminate tutte le ripetizioni - non ripartire
//    ]
unsigned long lepri[4][7] = {
    {getStartingTip(Blocco_int), Blocco_int, 0, 0, 1, 0, 0},
    {getStartingTip(Blocco_int), Blocco_int, 0, 0, 1, 0, 0},
    {getStartingTip(Blocco_int), Blocco_int, 0, 0, 1, 0, 0},
    {getStartingTip(Blocco_int), Blocco_int, 0, 0, 1, 0, 0},
};


void setup()
{
    pinMode(BUTTON, INPUT);
    pinMode(LED_BUILTIN, OUTPUT);
    strip.begin();
    // Serial.begin(9600);
}

void loop()
{

    BUTTONstate = digitalRead(BUTTON);
    if (BUTTONstate == HIGH && started == 0)
    {
        ledCountDown();
        started = 1;
        startingMillis = millis();
    }

    if (started == 1)
    {
        for (int i = 0; i < Nlepri_int; i++)
        {
            if (
                millis() > startingMillis + i * delayLepri && // tempo di attesa tra le lepri
                lepri[i][4] == 1 &&                           // lepre attiva
                lepri[i][6] == 0                              // ripetizioni non terminate
            )
            {
                updateLepreState(lepri[i]);
                setLeds(lepri[i], lepriColors[i]);
            }

            if (lepri[i][4] == 0 && lepri[i][6] == 0)
            {
                handleRecuperi(lepri[i]);
            }
        }
        strip.show();
        delay(DELAY);
    }
}

void updateLepreState(unsigned long lepreState[])
{
    // Movimento
    if (lepreState[1] == 1)
    {
        lepreState[0] = lepreState[0] + ledJump;
    }
    else
    {
        lepreState[0] = lepreState[0] - ledJump;
    }

    // Cambio Direzione
    if (lepreState[0] >= NUM_LEDS - 1 || lepreState[0] <= 0)
    {
        handleBordoVasca(lepreState);
    }
}

void handleRecuperi(unsigned long lepreState[])
{
    // Gestione Recuperi
    // sblocca le lepri che hanno finito il recupero
    if (
        (lepreState[4] == 0) &&                  // lepre ferma
        (millis() > lepreState[5] + Intervallo_int) // finito il tempo di recupero
    )
    {
        lepreState[4] = 1;
    }

    // Blocco definitivo lepre
    if (
        lepreState[3] == Nripetizioni_int // finite le ripetizioni da fare
    )
    {
        lepreState[6] = 1;
        Nterminated++;
        Nterminated = stopOnAllTerminated(Nterminated, Nlepri_int);
    }
}

int stopOnAllTerminated(int Nterminated, int Nlepri)
{
    if (Nterminated == Nlepri)
    {
        started = 0;
        setLepriInitialState(lepri);
        Nterminated = 0;
    }
    return Nterminated;
}

void handleBordoVasca(unsigned long lepreState[])
{
    // cambia direzione di percorrenza
    lepreState[1] ^= 1;
    if (lepreState[1] == 0)
    {
        lepreState[0] = tipFromEnd;
    }
    else
    {
        lepreState[0] = tipFromStart;
    }

    // incrementa vasche percorse
    lepreState[2]++;
    if (lepreState[2] >= Nvasche_int)
    {
        finishRepetition(lepreState);
    }
}

void finishRepetition(unsigned long lepreState[])
{
    lepreState[2] = 0;        // resetta le vasche percorse
    lepreState[3]++;          // aumenta le ripetizioni fatte
    lepreState[4] = 0;        // ferma la lepre
    lepreState[5] = millis(); // il recupero inizia in questo istante
}

void ledCountDown()
{
    strip.setPixelColor(0, strip.Color(255,0,0));
    strip.setPixelColor(1, strip.Color(255,0,0));
    strip.setPixelColor(2, strip.Color(255,0,0));
    strip.setPixelColor(3, strip.Color(255,0,0));
    strip.setPixelColor(4, strip.Color(255,0,0));
    strip.show();
    for (int i = 4; i >= 0; i--)
    {
        strip.setPixelColor(i, strip.Color(0,0,0));
        strip.show();
        delay(1000);
    }
}

int setLeds(unsigned long lepreState[], int color[])
{
    int position = lepreState[0];
    if (lepreState[1] == 1)
    {
        for (int i = 0; i < ledWidth; i++)
        {
            if (position - i >= 0)
            {
                strip.setPixelColor(position - i, strip.Color(color[0], color[1], color[2]));
            }
        }
        for (int i = 0; i < ledWidth; i++)
        {
            if (position - i - ledWidth >= 0)
            {
                strip.setPixelColor(position - i - ledWidth, strip.Color(0,0,0));
            }
        }
    }
    else
    {
        for (int i = 0; i < ledWidth; i++)
        {
            if (position + i < NUM_LEDS)
            {
                strip.setPixelColor(position + i, strip.Color(color[0], color[1], color[2]));
            }
        }
        for (int i = 0; i < ledWidth; i++)
        {
            if (position + i + ledWidth < NUM_LEDS)
            {
                strip.setPixelColor(position + i + ledWidth, strip.Color(0,0,0));
            }
        }
    }
}

void setLepriInitialState(unsigned long lepri[][7])
{
    for (int i = 0; i < Nlepri_int; i++) {
        lepri[i][0] = getStartingTip(Blocco_int);
        lepri[i][1] = Blocco_int;
        lepri[i][2] = 0;
        lepri[i][3] = 0;
        lepri[i][4] = 1;
        lepri[i][5] = 0;
        lepri[i][6] = 0;

    }
}

Thanks in advance

Are you using the ESP32 to power the LED strip?

Describe the problem in a different way.

mixing delay(x) and millis() is an issue.

no they are both powered by external 12 volt, on arduino countdown and rabbits works perfectly, on esp32 only the countdown start and not the rabbits.

just a comment:

your lepreState[] defined as unsigned, so the last comparision has undefined meaning

Sorry but i don't get it... my code as it is working perfectly on arduino mega, but on esp32 the same code, only the countdown is running then no other led are working... if as you said "your lepreState[] defined as unsigned" it should not work on both mcu or am I wrong?

An ESP32 is not a MEGA or Uno. Thing can work differently on a ESP32.

Can you post a schematic and an image of your project all wired up to the ESP32?

Supplying 12V to the ESP32 is a very large waste of energy is at the upper limit of a typical ESP32's developer boards regulator.

Looking over your posted code, I do not see any reason why the code should not work on a ESP32.

Post a schematic.

Post an image.

I never said that I'm giving 12v to esp32 I'm giving 12 volts to the led, esp32 and Arduino mega are powered with USB cable...

Did you connect together the GND of the ESP, led and power supply?

cool not 12V's to the ESP32.

Now how about that image?

Schematic?

I don't get why you are asking to me the schematic.. if the countdown is working it means the connections are good .... by the way here it is the picture... yes esp32 gnd is connected to gnd 12v power supplier and the other connection is on pin gpio 5 as in the code...

Same connections on arduino mega and all the code works.. i tried even with a level shifter on esp32 but nothing changed

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