Freeze after unspecified amount of time

Hey, I’ve been messing around with this LED strip that I have. I’ve been trying to make something where there are 4 leds that move along the strip, getting to the end, and then continuing to the beginning. If you loop the strip, they should smoothly move around the loop indefinitely. However, I seem to be hitting a snag, where everything freezes after a certain amount of time. This time period changes whenever I change anything with my code. I added a timer in to check how long the runtime was before it froze, and I got 42.1 seconds, but after deleting that code, I got 36 seconds by my phone stopwatch.

My first thought is that something was overflowing, but I couldn’t find a source of that anywhere.

A second set of eyes on this would be very helpful. Thanks.

#include <FastLED.h>
#define LED_PIN     22
#define NUM_LEDS    30
#define TICK 1
#define LED_LENGTH 4

CRGB leds[NUM_LEDS];
void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);;
}

int on = 0;
int RED = 255;
int GREEN = 0;
int BLUE = 0;
unsigned long counter = 1;
unsigned long now = millis();

void loop() {
  if(millis() > now + TICK){
    return;
    }

  //Handle colour changes every millisecond
  if(counter%1 == 0){
  if(BLUE == 0){
    RED--;
    GREEN++;
    
    }
  if(RED == 0){
    GREEN--;
    BLUE++;
    }
  if(GREEN == 0){
    BLUE--;
    RED++;
    }
  }

  //Move LED forward by 1
  if(counter%100 == 0){
    
    int off = on - (LED_LENGTH);
     if(off<0){
      off = off + NUM_LEDS;
      }
    if(on > NUM_LEDS-1){
      on = 0;
      }
      
      leds[on] = CRGB(RED,GREEN,BLUE);
      leds[off] = CRGB(0,0,0);
      FastLED.show();
      

      on++;
      off++;
      counter = 0;
  }
      counter++;
      FastLED.show();
      now = millis();

}

This is no good:

if(millis() > now + TICK){
    return;
    }

If you want to delay by a millisecond, use delay(1) or "millis() - now > TICK".

Hey, thanks for the reply. You seem to be right that my delay method is causing problems, but your solution of using "millis() - now > TICK" didn't work. The freeze happened as before. Using delay(1) worked, but ideally I would like to avoid using delay, as I want to be able to check other things between the wait times.

try this

#include <FastLED.h>
#define LED_PIN     22
#define NUM_LEDS    30
#define LED_LENGTH 4



CRGB leds[NUM_LEDS];
void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);;
}

int on = 0;
int RED = 255;
int GREEN = 0;
int BLUE = 0;
unsigned long counter = 1;
unsigned long now = millis();
unsigned long TICK = 1;

void loop() {
  if( millis() - now >  TICK ){

  //Handle colour changes every millisecond
  if(counter%1 == 0){
  if(BLUE == 0){
    RED--;
    GREEN++;
   
    }
  if(RED == 0){
    GREEN--;
    BLUE++;
    }
  if(GREEN == 0){
    BLUE--;
    RED++;
    }
  }

  //Move LED forward by 1
  if(counter%100 == 0){
   
    int off = on - (LED_LENGTH);
     if(off<0){
      off = off + NUM_LEDS;
      }
    if(on > NUM_LEDS-1){
      on = 0;
      }
     
      leds[on] = CRGB(RED,GREEN,BLUE);
      leds[off] = CRGB(0,0,0);
      FastLED.show();
     

      on++;
      off++;
      counter = 0;
  }
      counter++;
      FastLED.show();
      now = millis();

}

}

Didn't look closely but I noticed you're using an array. Add in a couple of print statements to see if you may be exceeding the array bounds - which can cause strange behavior.

KASSIMSAMJI:
try this

Hey, your modification to the code gave me a thought, and I think I'm an idiot.

I managed to find my problem. The code I was using to try to induce timing was

 if(millis() - now > TICK){
    return;
    }

The problem is the way I have the greater than sign pointing. It's basically saying if the time is greater than a millisecond, then stop doing things. Because once it gets above a millisecond, it's always going to be there, then it will stop at that point. I guess if there's ever lag, or something that causes it to get to that point, then it's going to stop. By changing the sign around, the problem was solved. However, I'm still surprised that that particular code ever found the right conditions to run...

Thanks guys for your help. You people are amazing :slight_smile: