Millis per prolungare pressione tasto

Ciao a tutti, sto facendo un progetto per cui quando premo un pulsante si accendono dei led animati e quando lo rilascio si spengono, vorrei però avere la possibilità di lasciare accesi i led animati 2 secondi oltre il rilascio del pulsante, qual'è la strada migliore? pensavo ai millis ma sono un po confuso sul da farsi, ecco il codice:

#include <FastLED.h>


#define NUM_LEDS 64
#define DATA_PIN 7
#define CLOCK_PIN 13

CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(84);
  pinMode (4, INPUT_PULLUP);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(250);
  }
}

void loop()

{
  if (digitalRead(4) == LOW)


  {
    static uint8_t hue = 0;
    Serial.print("x");
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(hue++, 255, 255);
      FastLED.show();
      fadeall();
      delay(5);
    }

  }

  else

  {
    static uint8_t hue = 0;
    Serial.print("x");
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(hue++, 0, 0);
      FastLED.show();
      fadeall();
      delay(5);
    }
  }

}

Finché il pulsante è premuto, aggiorni una variabile unsigned long t con il valore di millis(); poi scrivi

if (digitalRead(4)==LOW || millis()-t<RITARDO)
{
...
}

dove RITARDO è una costante pari al ritardo desiderato in millisecondi.

ho provato così ma non sembra andare:

#include <FastLED.h>


#define NUM_LEDS 64
#define DATA_PIN 7
#define CLOCK_PIN 13

const int RITARDO = 2000;
unsigned long t = millis ();
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(84);
  pinMode (4, INPUT_PULLUP);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(250);
  }
}

void loop()

{
 
  if (digitalRead(4)==LOW || millis()-t<RITARDO)


  {
    static uint8_t hue = 0;
    Serial.print("x");
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(hue++, 255, 255);
      FastLED.show();
      fadeall();
      delay(5);
    }

  }

  else

  {
    static uint8_t hue = 0;
    Serial.print("x");
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(hue++, 0, 0);
      FastLED.show();
      fadeall();
      delay(5);
    }
  }

}
#include <FastLED.h>

#define NUM_LEDS 64
#define DATA_PIN 7
#define CLOCK_PIN 13

const int RITARDO = 2000;
unsigned long t = millis();
CRGB leds[NUM_LEDS];

void setup()
{
 Serial.begin(57600);
 Serial.println("resetting");
 LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
 LEDS.setBrightness(84);
 pinMode (4, INPUT_PULLUP);
}

void fadeall()
{
 for (int i=0; i<NUM_LEDS; i++) 
   {leds[i].nscale8(250);}
}

void loop()
{
 if (!digitalRead(4)) t=millis();
 if (digitalRead(4)==LOW || millis()-t<RITARDO)
 {
   static uint8_t hue = 0;
   Serial.print('x');
   for (int i=0; i<NUM_LEDS; i++)
   {
     leds[i] = CHSV(hue++, 255, 255);
     FastLED.show();
     fadeall();
     delay(5);
   }
 }
 else
 {
   static uint8_t hue = 0;
   Serial.print('x');
   for (int i=0; i<NUM_LEDS; i++)
   {
     leds[i] = CHSV(hue++, 0, 0);
     FastLED.show();
     fadeall();
     delay(5);
   }
 }
}

RITARDO può essere int o deve necessariamente essere unsigned long?... Uhmm...

Datman:
RITARDO può essere int o deve necessariamente essere unsigned long?... Uhmm...

Grazie Datman! meglio int visto che devo stare sotto i 32 secondi :slight_smile:

Unsigned int :wink: