Millis(); How does it determine the active time of the "Glitter" function?

I have written a code a long time ago and cannot remember what I did. Can anyone help explain to me how the program determines for how long Glitter(100); is being executed before stop animation is being executed? I took a stopwatch and measured roughly 3 minutes, but I don't understand how this value is being calculated?? Ubuntu Pastebin

//***********************
//### WS2912B Stuff #####
//***********************
#include "FastLED.h"
FASTLED_USING_NAMESPACE
#define NUM_LEDS 150
CRGB leds[NUM_LEDS];
#define PIN 6
#define FRAMES_PER_SECOND  120
#define BRIGHTNESS         80
#define SEC  10
//***********************
//### Motion Sensor Stuff Current Thing as of 10.2.2020
//***********************
const int motionSensor = 2;
int state = LOW;
int val = 0;
const int buttonPin = 3;

long lastActivation = 0;
bool showingRainbow = false;
int sleep = SEC * 1000;


CRGBPalette16 currentPalette;
TBlendType    currentBlending;
unsigned int  ledLoc = 0;
/// ARRAYYSS
int evrThird[] = {  105, 108, 111, 114, 117,
120, 123, 126, 129, 132, 135, 138, 141,
144, 147 }; //Summe = 15
int e;

int backBaby[] = {};

int allOther[] = {0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                  17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
                  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
                  47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
106, 107, 109, 110, 112, 113, 115, 116,
118, 119, 121, 122, 124, 125, 127, 128,
130, 131, 133, 134, 136, 137, 139, 140,
142, 143, 145, 146, 148, 149   }; //Summe = 135
/// ARRAYYSS

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup(){
//***********************
//### Motion Sensor Stuff
//***********************
  delay(200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(motionSensor, INPUT);
  //Serial.begin(9600);
  FastLED.setBrightness(BRIGHTNESS);
  // initialize the pushbutton pin as an input:
 // pinMode(buttonPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
//***********************
//### WS2912B Stuff #####
//***********************
FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

currentBlending = LINEARBLEND;
currentPalette = PartyColors_p;

}

uint8_t gHue = 0;



void loop() {

  long timeSinceLastActivation = millis() - lastActivation;


//buttonTest
buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(LED_BUILTIN, LOW);
  }
  else {
    // turn LED off:
    digitalWrite(LED_BUILTIN, HIGH);
  }

//ButtonTestEnd

  ///MainPorgram I
  if (showingRainbow)
  {
    FastLED.setBrightness(BRIGHTNESS);
    //confetti();
    Glitter(100);
    if (timeSinceLastActivation > sleep)
    {
      // stop animation
      curtainEffect();
      everyThirdRed();
      showingRainbow = false;
    }
  }
  if (digitalRead(motionSensor) == HIGH)
  {
    // start rainbow
    showingRainbow = true;
    lastActivation = millis();
  }
///End Main Program I


} //loop


//***********************
//### WS2912B Functions #
//***********************
void Glitter( fract8 chanceOfGlitter)
  {
    fill_rainbow( leds, NUM_LEDS, gHue, 255/NUM_LEDS);
    EVERY_N_MILLISECONDS( 300 ) { gHue++; }
    fadeToBlackBy( leds, NUM_LEDS, 10);
    if( random8() < chanceOfGlitter) {
    leds[ random16(NUM_LEDS) ] += CRGB::White;
    }
FastLED.show();
  }

  void rainbow()
  {
    // FastLED's built-in rainbow generator
    fill_rainbow( leds, NUM_LEDS, gHue, 460);
    EVERY_N_MILLISECONDS( 20 ) { gHue++; }
    FastLED.show();
  }



  void curtainEffect() {
  // curtain effect

  for(int k=0, m=NUM_LEDS-1; k<=NUM_LEDS/2 && m>=NUM_LEDS/2; k++, m--) {
             leds[k] = CRGB::Blue;
             leds[m] = CRGB::Red;
             FastLED.show();
             delay(70);
         }                               // the 75th LED will be Blue

  for(int i=NUM_LEDS/2, j=NUM_LEDS/2; i>=0, j<=NUM_LEDS; i--, j++) {
             leds[i] = CRGB::Black;
             leds[j] = CRGB::Black;
             FastLED.show();
             delay(90);               // starting from LED #75, they will turn off oneoyone until the start
         }
         //FastLED.show();
         FastLED.delay(1000/FRAMES_PER_SECOND);
         EVERY_N_MILLISECONDS( 20 ) { gHue++; }
 }

 void everyThirdRed()
 {
       FastLED.setBrightness(10);
       for ( byte f = 0; f <135; f++)
       {
       leds[allOther[f]] = CRGB::Black;
       }
       for ( byte e = 0; e <15; e++)
       {
       leds[evrThird[e]] = CRGB::Salmon;
       }
       FastLED.show();

   }



 void juggledark() {
   // eight colored dots, weaving in and out of sync with each other
   fadeToBlackBy( leds, NUM_LEDS, 20);
   byte dothue = 0;
   for( int i = 0; i < 8; i++) {
     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 0, 0);
     dothue += 32;
   }
   FastLED.show();
   FastLED.delay(1000/FRAMES_PER_SECOND);
   EVERY_N_MILLISECONDS( 20 ) { gHue++; }
 }

 void juggle() {
   // eight colored dots, weaving in and out of sync with each other
   fadeToBlackBy( leds, NUM_LEDS, 20);
   byte dothue = 0;
   for( int i = 0; i < 8; i++) {
     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
     dothue += 32;
   }
   FastLED.show();
   FastLED.delay(1000/FRAMES_PER_SECOND);
   EVERY_N_MILLISECONDS( 20 ) { gHue++; }
 }

 void confetti()
{
  // random colored speckles that blink in and fade smoothly
  fadeToBlackBy( leds, NUM_LEDS, 3);
  int pos = random16(NUM_LEDS);
  leds[pos] += CHSV( gHue + random8(64), 200, 255);
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  FastLED.show();
}

void sinelon()
{
  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy( leds, NUM_LEDS, 20);
  EVERY_N_MILLISECONDS( 20 ) { gHue++; }
  int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  leds[pos] += CHSV( gHue, 255, 192);
  FastLED.show();
}

void bpm()
{
  // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  uint8_t BeatsPerMinute = 62;
  CRGBPalette16 palette = PartyColors_p;
  uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  for( int i = 0; i < NUM_LEDS; i++) { //9948
    leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  }
}



// Start adding stuff from https://github.com/atuline/FastLED-Demos/blob/master/every_n_example/every_n_example.ino
void everyNMillisI() {
  EVERY_N_MILLIS_I(thisTimer,100) {                           // This only sets the Initial timer delay. To change this value, you need to use thisTimer.setPeriod(); You could also call it thatTimer and so on.
    uint8_t timeval = beatsin8(10,5,100);                     // Create/modify a variable based on the beastsin8() function.
    thisTimer.setPeriod(timeval);                             // Use that as our update timer value.
    ledLoc = (ledLoc+1) % (NUM_LEDS-1);                       // A simple routine to just move the active LED UP the strip.
    leds[ledLoc] = ColorFromPalette(currentPalette, ledLoc, 255, currentBlending);     // Pick a slightly rotating colour from the Palette
  }
  fadeToBlackBy(leds, NUM_LEDS, 8);                           // Leave a nice comet trail behind.

  FastLED.show();
}

A little editing makes it somewhat easier to see what is going on.
When the Motion Sensor input is HIGH, turn on the effect and note the time the effect was started.

If the effect has been on for 'sleep' milliseconds since the last time the Motion Sensor input was HIGH, turn off the effect.

void loop()
{
  if (digitalRead(motionSensor) == HIGH)
  {
    // start Glitter
    showingGlitter= true;
    lastActivation = millis();
  }
  
  ///MainPorgram I
  if (showingGlitter)
  {
    FastLED.setBrightness(BRIGHTNESS);

    Glitter(100);

    unsigned long timeSinceLastActivation = millis() - lastActivation;
    if (timeSinceLastActivation > sleep)
    {
      // stop animation
      showingGlitter = false;
    }
  }
} //loop

Thanks. Yes on your editing, thanks. :slightly_smiling_face: So after I posted this question I dissected the code once again and remembered that there is a potentiometer on the PIR, the motion sensor. So in effect I control the time via the physical sensor, right? The code basically makes it so that once the sensor input is LOW the stop animation will run after what is defined in #define SEC. So the PIR sensor is put to 60 seconds, the code adds 10 seconds to that, totaling 70 seconds? However thinking about what you said it should add exactly double the time of the sensor? The sensor puts out HIGH for 60 seconds. the program makes not of that value and stores the number. now the input is LOW, and the program counts up to that number before it switches to the Stop animation... ? Sorry, I'm a little confused

Sometimes when you are looking at code you wrote a long time ago, it looks like someone else must have written it!

In this circumstance I usually just write the code again. It often comes out better than the first time.

a7

No. It notes the LAST time the signal was HIGH and, when the input goes LOW, waits 10 seconds (SEC = 10; sleep = SEC * 1000;) before turning off the effect. It adds 10 seconds to however long the input was HIGH.

1 Like