Global Delay

Hi everyone - noob question time.

I have a small led RGB strip program where a led chases up and down the strip changing colour each time how can I implement a global delay so I dont have to change each delay in the program

[code]
//Leds chase backwards and forwards along strip
//

#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 5

CRGB leds[NUM_LEDS];

void setup() { 
       FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
   }

void loop()
  
{
        for(int dot = 0; dot < NUM_LEDS; dot++) { 
            //fadeToBlackBy( leds, NUM_LEDS, 20);
            leds[dot] = CRGB::Yellow;
            FastLED.show();
            // clear this led for the next time around the loop
            //fadeToBlackBy( leds, NUM_LEDS, 100);
            leds[dot] = CRGB::Black;
            delay(50);
        }

    // chase backward Orange
      for(int dot=NUM_LEDS ; dot >=0 ; dot--){ 
          leds[dot] = CRGB::Red;
          FastLED.show();
          //fadeToBlackBy( leds, NUM_LEDS, 100);
          leds[dot] = CRGB::Black;
          delay(50);

        }
        // chase forward blue
      for(int dot = 0; dot < NUM_LEDS; dot++) { 
            leds[dot] = CRGB::Orange;
            FastLED.show();
            // clear this led for the next time around the loop
            leds[dot] = CRGB::Black;
            delay(50);
       }

// chase backward Orange
      for(int dot=NUM_LEDS ; dot >=0 ; dot--){ 
          leds[dot] = CRGB::Purple;
          FastLED.show();
          leds[dot] = CRGB::Black;
          delay(50);
      }
    // chase forward blue
      for(int dot = 0; dot < NUM_LEDS; dot++) { 
            leds[dot] = CRGB::Blue;
            FastLED.show();
            // clear this led for the next time around the loop
            leds[dot] = CRGB::Black;
            delay(50);
       }
       // chase backward Purple
      for(int dot=NUM_LEDS ; dot >=0 ; dot--){ 
          leds[dot] = CRGB::Green;
          FastLED.show();
          leds[dot] = CRGB::Black;
          delay(50);
     
}}

[/code]

how can I implement a global delay so I dont have to change each delay in the program

Make the delay() parameter a global variable
Changing the value of the global variable will then change all of the delay()s

unsigned long delayPeriod = 50;
//later in the code
delay(delayPeriod);

UKHeliBob:
Make the delay() parameter a global variable
Changing the value of the global variable will then change all of the delay()s

unsigned long delayPeriod = 50;

//later in the code
delay(delayPeriod);

Thankyou very much.

When you get this sorted and understand it, you may want to do more complicated things while the chase is running...

Delaying 50ms isn’t long, but when they’re jammed back-to-back continuously, you’ll discover the processor is tied up unable to do anything else while your program is running.

See if you can figure out how to implement the delays using millis(), and perhaps using simple arrays to store the LED info.

Take a loop at my tutorial How to write Timers and Delays in Arduino which has examples of Global timer settings.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.