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