Hello, i was wondering if anyone could take a look at this sketch and tell me why the functions front and rear turn the pixels off for a second or two then turn back on when the servo functions move. they usually complete their colour change but sometimes turn off before that. is it a case of too many millis timers, the servos blocking the front rear code, or something completly different. in a seperate sketch the servos work normal and the front rear functions work perfect in their own sketches.
`#define ESP32_RTOS // Uncomment this line if you want to use the code with freertos only on the ESP32
// Has to be done before including "OTA.h"
#include "OTA.h"
#include "credentials.h"
#include "NoiascaLedControl.h"
#include <pwmWrite.h>
#include <FastLED.h>
const int servoPin1 = 18; //pie holo
const int servoPin2 = 19; //front holo
const int servoPin3 = 21; // rear holo
Pwm pwm = Pwm(); //pwm for esp32
#define NUM_LEDS_PER_STRIP 7
CRGB leds1[NUM_LEDS_PER_STRIP]; //pie neo
CRGB leds2[NUM_LEDS_PER_STRIP]; //front neo
CRGB leds3[NUM_LEDS_PER_STRIP]; // rear neo
///////////////holo timers//////////////
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval1 = 3000;
unsigned long interval2 = 1000;
unsigned long interval3 = 1000;
/*
matrix logic pins
*/
const uint8_t LEDCS_PIN = 26; // 26 LED CS or LOAD software spi
const uint8_t LEDCLK_PIN = 25; // 25 LED Clock software spi
const uint8_t LEDDATA_PIN = 27; // 27 LED DATA IN software spi
const uint8_t LED_MODULES = 5;
LedControl lc = LedControl (LEDDATA_PIN, LEDCLK_PIN, LEDCS_PIN, LED_MODULES); // Software Bitbang - use this if you can't use Hardware SPI
/* we always wait a bit between updates of the display */
const unsigned long delaytime=100;
const int numCols = 8; // number of columns in LED matrix
const int numRows = 8; // number of rows in LED matrix
bool matrix[numCols][numRows]; // keep track of which LEDs are on or off
//////////////////psi////////////////
// Constants for LED strips
#define DATA_PIN_22 22 //front psi
#define DATA_PIN_23 23 //rear psi
#define NUM_LEDSpsi 21
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define DATA_RATE 800000
// psi objects
CRGB leds4[NUM_LEDSpsi];
CRGB leds5[NUM_LEDSpsi];
// Timing variables
unsigned long previousMillisf = 0;
unsigned long previousMillisr = 0;
unsigned long interval4 = 0;
unsigned long interval5 = 0;
bool isRed = true;
bool isGreen = true;
void setup() {
Serial.begin(115200);
Serial.println("Booting");
setupOTA("R2 HEAD test board", mySSID, myPASSWORD);////CHANGE FOR BOARD
// Your setup code
/* The MAX72XX needs to initialize hardware */
lc.begin();
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
lc.shutdown(1,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
lc.setIntensity(1,8);
/* and clear the display */
lc.clearDisplay(0);
lc.clearDisplay(1);
FastLED.addLeds<NEOPIXEL, 4>(leds1, NUM_LEDS_PER_STRIP); // pie neo pin
FastLED.addLeds<NEOPIXEL, 17>(leds2, NUM_LEDS_PER_STRIP); // front neo pin
FastLED.addLeds<NEOPIXEL, 16>(leds3, NUM_LEDS_PER_STRIP); // rear neo pin
////////// holo servo initilisation/////////
pwm.attach(servoPin1, 0, 1000, 2000);
pwm.attach(servoPin2, 1, 1000, 2000);
pwm.attach(servoPin3, 2, 1000, 2000);
// pwm.attach(servoPin1, 0, 1000, 2000, 0, 1, true); ////inverse pwm
//pwm.attach(servoPin2, 1, 1000, 2000, 0, 1, true); ////inverse pwm
//pwm.attach(servoPin3, 2, 1000, 2000, 0, 1, true); ////inverse pwm
pwm.writeServo(servoPin1, 90); // Stop
pwm.writeServo(servoPin2, 90); // Stop
pwm.writeServo(servoPin3, 90); // Stop
//////////////////psi setup//////////////////
FastLED.addLeds<LED_TYPE, DATA_PIN_22, COLOR_ORDER>(leds4, NUM_LEDSpsi).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE, DATA_PIN_23, COLOR_ORDER>(leds5, NUM_LEDSpsi).setCorrection(TypicalLEDStrip);
theaterChaseRainbow();
setRandomInterval();
//////////////////// matrix lines on startup////////////////
for (int row=0; row<8; row++)
{
for (int col=0; col<8; col++)
{
lc.setLed(0,col,row,true); // turns on LED at col, row
lc.setLed(1,col,row,true); // turns on LED at col, row
delay(50);
}
}
for (int row=0; row<8; row++)
{
for (int col=0; col<8; col++)
{
lc.setLed(0,col,row,false); // turns off LED at col, row
lc.setLed(1,col,row,false); // turns off LED at col, row
delay(50);
//lc.clearDisplay(0);// clear screen
//lc.clearDisplay(1);// clear screen
}
}
}
void rando() {
// Turn on or off random LEDs
int count = 0;
while (count < 20) {
int x = random(numCols);
int y = random(numRows);
if (matrix[x][y] == false) {
lc.setLed(0, x, y, true);
lc.setLed(1, x, y, true);
matrix[x][y] = true;
count++;
}
else {
lc.setLed(0, x, y, false);
lc.setLed(1, x, y, false);
matrix[x][y] = false;
}
}
delay(50); // wait a short time before updating the LEDs
}
void loop() {
#ifdef defined(ESP32_RTOS) && defined(ESP32)
#else // If you do not use FreeRTOS, you have to regulary call the handle method.
ArduinoOTA.handle();
#endif
// Your code here
front();
rear();
rando();
ArduinoOTA.handle();
//servo1();
ArduinoOTA.handle();
// servo2();
ArduinoOTA.handle();
//servo3();
ArduinoOTA.handle();
}
void servo1(){
static byte state1 = 0;
unsigned long currentMillis = millis();
switch(state1) {
case 0:
fill_solid (leds1, 7, CRGB(179, 230, 255));
FastLED.show();
previousMillis1 = currentMillis;
state1++;
break;
case 1:
if (currentMillis - previousMillis1 >= interval1) {
pwm.writeServo(servoPin1, (random (140 ,180)));
previousMillis1 = currentMillis;
state1++;
}
break;
case 2:
if (currentMillis - previousMillis1 >= interval2) {
pwm.writeServo(servoPin1, 90); // Stop
previousMillis1 = currentMillis;
state1++;
}
break;
case 3:
if (currentMillis - previousMillis1 >= interval3) {
pwm.writeServo(servoPin1,(random (0 , 50)));
previousMillis1 = currentMillis;
state1++;
}
break;
case 4:
if (currentMillis - previousMillis1 >= interval2) {
pwm.writeServo(servoPin1, 90); // Stop
previousMillis1 = currentMillis;
state1++;
}
break;
case 5:
if (currentMillis - previousMillis1 >= interval3) {
FastLED.clear(true);
previousMillis1 = currentMillis;
state1 = 0;
interval1 = random(2000, 5000);
interval2 = random(100, 500);
interval3 = random(3000, 5000);
}
break;
}
}
void servo2(){
static byte state2 = 0;
unsigned long currentMillis = millis();
switch(state2) {
case 0:
fill_solid (leds2, 7, CRGB(179, 230, 255));
FastLED.show();
previousMillis2 = currentMillis;
state2++;
break;
case 1:
if (currentMillis - previousMillis2 >= interval1) {
pwm.writeServo(servoPin2, (random (140 ,180)));
previousMillis2 = currentMillis;
state2++;
}
break;
case 2:
if (currentMillis - previousMillis2 >= interval2) {
pwm.writeServo(servoPin2, 90); // Stop
previousMillis2 = currentMillis;
state2++;
}
break;
case 3:
if (currentMillis - previousMillis2 >= interval3) {
pwm.writeServo(servoPin2,(random (0 , 50)));
previousMillis2 = currentMillis;
state2++;
}
break;
case 4:
if (currentMillis - previousMillis2 >= interval2) {
pwm.writeServo(servoPin2, 90); // Stop
previousMillis2 = currentMillis;
state2++;
}
break;
case 5:
if (currentMillis - previousMillis2 >= interval3) {
FastLED.clear(true);
previousMillis2 = currentMillis;
state2 = 0;
interval1 = random(2000, 5000); //led time
interval2 = random(500, 1000); // moving time then stop
interval3 = random(1000, 5000); // wait between moves
}
break;
}
}
void servo3(){
static byte state3 = 0;
unsigned long currentMillis = millis();
switch(state3) {
case 0:
fill_solid (leds3, 7, CRGB(179, 230, 255));
FastLED.show();
previousMillis3 = currentMillis;
state3++;
break;
case 1:
if (currentMillis - previousMillis3 >= interval1) {
pwm.writeServo(servoPin3, (random (140 ,180)));
previousMillis3 = currentMillis;
state3++;
}
break;
case 2:
if (currentMillis - previousMillis3 >= interval2) {
pwm.writeServo(servoPin3, 90); // Stop
previousMillis1 = currentMillis;
state3++;
}
break;
case 3:
if (currentMillis - previousMillis1 >= interval3) {
pwm.writeServo(servoPin3,(random (0 , 50)));
previousMillis1 = currentMillis;
state3++;
}
break;
case 4:
if (currentMillis - previousMillis3 >= interval2) {
pwm.writeServo(servoPin3, 90); // Stop
previousMillis3 = currentMillis;
state3++;
}
break;
case 5:
if (currentMillis - previousMillis3 >= interval3) {
FastLED.clear(true);
previousMillis3 = currentMillis;
state3 = 0;
interval1 = random(1000, 5000);
interval2 = random(100, 500);
interval3 = random(1000, 5000);
}
break;
}
}
// Set all LEDs of a strip to a specified color
void setLEDsColor(CRGB* leds, int numLeds, const CRGB& color) {
for (int i = 0; i < numLeds; i++) {
leds[i] = color;
}
}
// Set a random interval between 1 and 5 seconds
void setRandomInterval() {
interval4 = random(5000, 10000);
interval5 = random(5000, 10000);
}
void theaterChaseRainbow() {
int firstPixelHue = 0; // First pixel starts at red (hue 0)
for (int a = 0; a < 30; a++) { // Repeat 30 times...
for (int b = 0; b < 3; b++) { // 'b' counts from 0 to 2...
// setLEDsColor(leds5, NUM_LEDSpsi, 0, 0, 0); // Set all LEDs to off
// setLEDsColor(leds4, NUM_LEDSpsi, 0, 0, 0); // Set all LEDs to off
// 'c' counts up from 'b' to end of strip in increments of 3...
for (int c = b; c < NUM_LEDSpsi; c += 3) {
// hue of LED 'c' is offset by an amount to make one full
// revolution of the color wheel (range 65536) along the length
// of the strip (NUM_LEDSpsi steps):
int hue = firstPixelHue + c * 65536L / NUM_LEDSpsi;
leds4[c] = CHSV(hue, 255, 255); // hue -> RGB
leds5[c] = CHSV(hue, 255, 255); // hue -> RGB
}
FastLED.show(); // Update both LED strips
delay(50); // Pause for a moment
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
}
}
}
void front(){
unsigned long currentMillisf = millis();
// Control the first set of LEDs on pin 22
if (currentMillisf - previousMillisf >= interval4) {
if (isRed) {
setLEDsColor(leds4, NUM_LEDSpsi, CRGB::Red);
isRed = false;
} else {
setLEDsColor(leds4, NUM_LEDSpsi, CRGB::Blue);
isRed = true;
}
FastLED.show(); // Update the first LED strip
setRandomInterval(); // Set a new random interval
previousMillisf = currentMillisf; // Update the previous millis
}
}
void rear(){
unsigned long currentMillisr = millis();
// Control the second set of LEDs on pin 23
if (currentMillisr - previousMillisr >= interval5) {
if (isGreen) {
setLEDsColor(leds5, NUM_LEDSpsi, CRGB::Green);
isGreen = false;
} else {
setLEDsColor(leds5, NUM_LEDSpsi, CRGB::Yellow);
isGreen = true;
}
FastLED.show(); // Update the second LED strip
setRandomInterval(); // Set a new random interval
previousMillisr = currentMillisr; // Update the previous millis
}
}
this is all being compiled on a esp32 wroom dev board