How do I stop this?

How do I make this just run once and not repeat over and over?

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 150

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define LED_PIN 4
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup () {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  delay(20);
 }
 void loop () {
      
  for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CRGB::White;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;
      
  } 

 
  
 }

Hello vallka

Add this to the loop() function at the end:

while(1);
3 Likes

Great that worked.
I'm just figuring out how to do all this so, if I want to add a button to trigger these leds (instead of pushing the Arduino reset) can I do that?

Hello vallka

Take a view to examples provided by the IDE to find a button handler.

2 Likes

Also... place this in setup() and make loop() empty

  for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CRGB::White;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;   
  }
3 Likes

Replace that new

while(1);

with:

while(digitalRead(BUTTON_PIN));

You would also need to add something like:

#define BUTTON_PIN 5

near the start, and:

pinMode(BUTTON_PIN, INPUT_PULLUP);

in setup().

Your button needs to be connected between BUTTON_PIN and GND.

2 Likes

Thanks to you all, It works like a charm!
(I am starting to understand this now)
In this code there is a #define CLOCK_PIN 13
What is this for?
Also if I wanted to change every other led to yellow can I do that?

Ohhh, two questions, @Delta_G doesn't do those... :laughing:

Yes
but please, show us your current version of the code, and someone will help you modify it to do that.

2 Likes
#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 150

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define LED_PIN 4
#define BUTTON_PIN 5

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup () {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  delay(20);
   for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CRGB::White;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;   
 }

  }
 void loop () {
      
  for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CRGB::White;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;
      
  } 
while(digitalRead(BUTTON_PIN));
 
  
 }
type or paste code here

Absolutely allowed, and as many ways to do it as there are to make hamburger.

Instead of

   leds[i] = CRGB::White;

try this maths hack that just checks if the index is odd or even:

   if (i % 2 == 0)
     leds[i] = CRGB::White;
   else
     leds[i] = CRGB::Yellow;

HTH

a7

1 Like

Thank you

Ok so I have this code and want to have the first 20 leds flash for 1 sec then the next 20 to flash for 1 sec and so on, how do I do this? (I'm learning alot from all of you right now,,,,thanks)

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 150

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define LED_PIN 4
#define BUTTON_PIN 5

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup () {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
  delay(20);
   for (int i = 0; i < NUM_LEDS; i++) {
    if (i % 2 == 0)
      if (i % 2 == 0)
     leds[i] = CRGB::White;
   else
     leds[i] = CRGB::Yellow;
   else
     leds[i] = CRGB::Yellow;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;   
 }

  }
 void loop () {
      
  for (int i = 0; i < NUM_LEDS; i++) {
     if (i % 2 == 0)
     leds[i] = CRGB::White;
   else
     leds[i] = CRGB::Yellow;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;
      
  } 
while(digitalRead(BUTTON_PIN));
 
  
 }

Let's see what you've tried to make this happen. So far I see a sketch that doesn't seem to attempt to do anything involve 20 LEDs or one second. What's your best attempt so far?

PS: I understand/recognize that the code you posted is simply what you were handed here: How do I stop this? - #11 by vallka
However, if this is a continuation of that thread, why not post your follow-up question there?
And if this is a new/separate question, why copy the unrelated code from that thread here?

@vallka ,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

https://www.google.com/search?as_q=arduino+fastled+light+up+leds+in+groups

I kinda understand this but have no idea how to add this to the code I have. The way I learn is to have a code and play with it, I did this with a code this site gave me for a motor.

to me ist seems that you want code that is 99% ready and then "play" with the last 1%.

Here is a tutorial that teaches you the basic usage of neopixels with the fastled-library

It covers various things.
Now. How did I find this?---?---?--?

using standard keywords
https://www.google.com/search?q=arduino+fastled+tutorial

crossreading the google-hits until I saw

I pretty much got this

Just want to thank you all for the help on this one, I'm not going to add the groupings because I can't figure it out and am on a time constraint. Will look into this at a later date.
Thanks again.

Is there a reason this code runs twice when powered and then multiplied times when button is pushed. `


#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 150

#define LED_PIN 4
#define BUTTON_PIN 5

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup () {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  delay(20);
   for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CRGB::White;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;   
 }

  }
 void loop () {
      
  for (int i = 0; i < NUM_LEDS; i++) {
   leds[i] = CRGB::White;
   FastLED.show();
   delay(5);
   leds[i] = CRGB::Black;
      
  } 
while(digitalRead(BUTTON_PIN));
 
  
 }