I try to do a summary of the problem I still have
My void loop() and void setup() are the following :
void setup()
{
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deseleziono il lettore SD card
pinMode(FRPIN , INPUT);
pinMode(PIRCAMERAPIN , INPUT);
pinMode(PIRSALAPIN , INPUT);
pinMode(PIRMAGAZZINOPIN , INPUT);
pinMode(MODALLARME, INPUT);
pinMode(MODGIORNO, INPUT);
pinMode(MODNOTTE, INPUT);
pinMode(MODEFFETTI, INPUT);
Blynk.begin(auth, IPAddress(xxx,xxx,x,x));
dht.begin();
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
timer.setInterval(1L, modalitaNotturna);
timer.setInterval(1L, modalitaEffettiLuminosi);
}
void loop()
{
unsigned long inizioloop = millis();
Blynk.run();
timer.run();
unsigned long fineloop = millis();
unsigned long tempesec = fineloop-inizioloop;
Serial.print("Tempo esecuzione Blynk.run = ");
Serial.println(tempesec);
}
The 2 functions called using timer.setInterval() are the following
-
void modalitaNotturna()
{
modnotte = digitalRead(MODNOTTE);
pirCamera = digitalRead(PIRCAMERAPIN);
fotoresval = analogRead(FRPIN);
frvalperc = map(fotoresval, 0, 1000, 0, 100);
if (modnotte == HIGH)
{
Serial.println("MODALITA NOTTURNA ATTIVATA");
if (frvalperc < 50 && pirCamera == HIGH && contnotturno == 1)
{
unsigned long tempoinizio = millis();
Serial.println("ACCENDO LED CAMERA MOVIMENTO RILEVATO");
for (int i = 0; i <= NUM_LEDS; i++)
{
if (i %10 == 0)
{
Blynk.run();
}
Serial.println(i);*/
leds[i] = CRGB::Gold;
}
unsigned long tempofine = millis();
Serial.print("Tempo uscita For Mod Notte = ");
Serial.println(tempofine - tempoinizio);
FastLED.show();
contnotturno = 0;
}
else if ((pirCamera == LOW && contnotturno == 0) || (frvalperc >= 50 && contnotturno == 0))
{
Serial.println("SPENGO LED CAMERA MOVIMENTO TERMINATO");
for (int i = 0; i <= NUM_LEDS; i++)
{
leds[i] = CRGB::Black;
}
contnotturno = 1;
FastLED.show();
FastLED.clear();
}
}
}
-
void modalitaEffettiLuminosi()
{
bottoneeffetti = digitalRead(MODEFFETTI);
if (bottoneeffetti == HIGH)
{
Serial.println("MODALITA EFFETTI LUMINOSI");
/*
unsigned long tin = millis();
if (effetto == 1)
{
//effettoMultiRGB();
}
if (effetto == 2)
{
effettoGradiente3Colori();
}
if (effetto == 3)
{
effettoScontroaMetaLungoRGB();
}
if (effetto == 4)
{
effettoScontroaMetaRGB();
}
if (effetto == 5)
{
effettoSerpenteMonocolore();
}
if (effetto == 6)
{
effettoSerpenteRGB();
}
if (effetto == 7)
{
effettoSerpenteMisto();
}
/*
unsigned long tfin = millis();
Serial.print("Tempo Esecuzione IF = ");
Serial.println(tfin-tin);
*/
conteffettiluminosi = 0;
}
else if (bottoneeffetti == LOW && conteffettiluminosi == 0)
{
Serial.println("SPENGO MODALITA EFFETTI LUMINOSI");
for (int i = 0; i <= 100; i++)
{
leds[i].CRGB::Black;
}
conteffettiluminosi = 1;
FastLED.show();
FastLED.clear();
}
}
As you can see in this last function I have the group of nested if-condition commented and in this case I dont have timeout problem.
When i remove the comment by eliminating this character
*/ /*
at the beginning and at the end of if-conditions code stop running and I perform timeout.
The for-loop monitored with millis takes 450ms to be executed and the void loop in this case(so when the for is executed) takes 520ms
If i remove from this for-loop
if (i %10 == 0)
{
Blynk.run();
}
Serial.println(i);
It takes 40ms to be executed and the void loop() takes 108ms.
I was able to do all of this try only because the if-conditions were commented otherwise the code stop running inside the for-loop monitored by millis and I don’t know what happens.
So in the end I need this group of nested if-condition but their causing me timeout.
Can someone please help me solving this problem?