Triggering simultaneous LED pulse effect on button with Neopixel library

I have been looking for a solution for : "triggering simultaneous LED pulse effect with a push button" . the same problem was raised on: LED strip pulse/waterfall effect on input and solved by @PaulRB.
i am wondering if it is possible adapting the code for Neopixel library instead of the fastled library!
Also if it is possible to briefly explain what is going on in the loop!

Cheers!

#include<FastLED.h>
#define NUM_LEDS 30
#define SCROLL_SPEED 50

CRGBArray<NUM_LEDS> leds;

int INpin = 7;
int input = 0;
unsigned long lastUpdate;

void setup() {
  FastLED.addLeds<NEOPIXEL, A0>(leds, NUM_LEDS);
  pinMode(INpin, INPUT);
}

void loop() {

  input = digitalRead(INpin);
  if (input == HIGH)
    leds[0] = CRGB::Blue;
  else
    leds[0] = CRGB::Black;

  if (millis() - lastUpdate > SCROLL_SPEED) {
    lastUpdate += SCROLL_SPEED;
    for (int i = NUM_LEDS - 1; i > 0; i--)
      leds[i] = leds[i - 1];
    FastLED.show();
  }
}

Oops.

Edit: ...and now you've changed the code

Sorry my bad!
i pasted the wrong code!!
just fixed it now!

problem to download FastLed?

No, Actually, the library and the code are working great.
I would like to modify the code so the led light reverse direction when i = NUM_LEDS, so the light goes up and down!
i tried:

int dir = 1;**

  if (input == HIGH)
    leds[0] = CRGB::Blue;
  else
    leds[0] = CRGB::Black;

  if (millis() - lastUpdate > SCROLL_SPEED) {
    lastUpdate += SCROLL_SPEED;
    for (int i = NUM_LEDS - 1; i > 0; i--)
      leds[i] = leds[i - 1];

i += dir;
if (i =NUM_LEDS){**
 dir = 0 - dir;
}
    FastLED.show();
  }

of course it is not working!!
I am a newbie and used to the Neopixel library, and though maybe if it is written with this library, i could understand better the loop and be able to modify it!

Make "dir" static or global?

Oops

What AWOL is trying to say is this:

= is for assignments, == is for comparisons. In your if clause, you need to use ==.

If I understand correctly what you want, this code will not do it, it is too simple. It moves the led pattern always in the same direction.

I don't think that would help. The code would be different, but not easier or harder to understand.

Can you explain, perhaps with a diagram, what you want the LEDs to do.

Yes, maybe i am asking more from this code!

Previously i have been working with this code:

  #include <Adafruit_NeoPixel.h>
#include <EEPROM.h>

#define NUM_LEDS 60
#define PIN 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

const int BUTTON = 2;
byte selectedEffect = 0;

/////////////////
int funcState = 0;
unsigned long buttonState = 0;
///////////////////////
int pix;
unsigned long eventInterval = 0;
unsigned long previousTime = 0;
int dir = 1;
int spd = 150;
///////////////////////

void setup()
{
  Serial.begin(9600);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(100);
  pinMode(BUTTON, INPUT_PULLUP); // internal pull-up resistor
  attachInterrupt (digitalPinToInterrupt (BUTTON), changeEffect, CHANGE); // pressed
}

// *** REPLACE FROM HERE ***
void loop() {

  EEPROM.get(0, selectedEffect);
  if (selectedEffect > 1) {
    selectedEffect += 1;
    EEPROM.put(0, 0);
  }

  switch (selectedEffect) {
    

    case 0:
      R1();
      strip.show();
  

    case 1:
      //      Serial.println("case 1");
      R1();
      strip.show();
  

  }
  Serial.println(selectedEffect);

}

void R1() {

  unsigned long currentTime = millis();
  if (currentTime - previousTime >= eventInterval) {

    strip.setPixelColor(pix, strip.Color(0, 0, 0));
    strip.show();
    eventInterval += 10 ; // (per millis)
    pix += dir;
        if (pix >= NUM_LEDS) {
          dir = 0 - dir;
        }
        if (pix <= -1) {
          dir = 0 - dir;
        }
    strip.setPixelColor(pix, strip.Color(0, 255, 0));
    strip.show();


  }

  }


void changeEffect() {
  if (digitalRead (BUTTON) == HIGH) {
    selectedEffect++;
    EEPROM.put(0, selectedEffect);
    asm volatile ("  jmp 0");
  }
}

When i press the button, a single Led pixel moves across the Led strip back and forth.
What i wanted to do is to keep adding more Led pixel to it (similar to your code)!
And in my code, using the case switch is not a solution.

I hope that makes sense.

oh! i get it now!
Thanks for explaining!
Unfortunately, it is still not working
I added more explanation, hope you can help as well.

I can tell you that the code you posted is not good, it contains some bad parts, and much useless and unnecessary code. For example unnecessarily waring out the EEPROM, which has a limited life, using interrupts to detect button press, and use of assembler code for no clear reason. I would suggest you stop using it.

How many more moving LEDs do you want? What should happen when a second moving led is created and it collides with the first?

Probably because i gathered it from many sources in order to make it work in a certain way!

Basic Part is:
When the button is pressed, a single LED/pixel start moving: from 0 to NUM_LEDS (60) and when it reach 60 the direction change from 60 to 0 and disappear.
Whenever the button is pressed, more LED pixel that make the same loop follows.

More to do:
when two LED pixels collide, the previous one disappear, probably by changing the color to (0,0,0);

///////////////////////

I can think of a quite easy way. A slightly different blue could be used for LEDs that are moving back towards led 0, compared to LEDs that are moving toward led 59. So LEDs moving one way would be Color(0, 255, 0) and legs moving the opposite way could be Color(0, 254, 0). They would look the same to the eyes.

Each led position would be looped over. If this led is Color(0, 0, 0) and the led to the left is Color(0, 255, 0), then this led becomes Color(0, 255, 0). Otherwise if this led is Color(0, 0, 0) and the led to the right is Color(0, 255, 0) then this led becomes Color(0, 0, 0). Otherwise if this led is Color(0, 0, 0) and the led to the right is Color(0, 254, 0), this led becomes Color(0, 254, 0). And finally if the led to the left is Color(0, 254, 0), this led becomes Color(0, 0, 0). Do you think that might work? Can you attempt to code that?

No, it's because you mainly cut and pasted it instead of understanding it and duplicating the functionality provided by each component, while doing your own programming.

I really like this approach and i think it might work.
I would like to give it a try, might take sometime to code it, but i will get back to you both ways!
Cheers!

I have got this part so far and I want to make sure that i understood your approach before proceeding;

#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 64
#define PIN 4
#define SCROLL_SPEED 100

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

const int BUTTON = 2;
int input = 0;
unsigned long lastUpdate;

void setup()
{
  Serial.begin(9600);
  strip.begin();
  strip.show();
  strip.setBrightness(35);
  pinMode(BUTTON, INPUT_PULLUP);
}

// *** REPLACE FROM HERE ***
void loop() {

  input = digitalRead(BUTTON);
  if (input == HIGH) {
  

  if (millis() - lastUpdate > SCROLL_SPEED) {
    lastUpdate += SCROLL_SPEED;

    for (int currentLed = 0; currentLed < NUM_LEDS + 1; currentLed++) {
      int rightLed = 1;  //// RIGHT
      int leftLed = -1; //// LEFT
      int dir = 1;

//      currentLed += dir;
      rightLed += currentLed;
      leftLed += currentLed;
      strip.setPixelColor(rightLed, strip.Color(0, 0, 0));
      strip.show();
      strip.setPixelColor(currentLed, strip.Color(0, 255, 0));
      strip.show();
      strip.setPixelColor(leftLed, strip.Color(0, 0, 0));
      strip.show();

      //        if (currentLed >= NUM_LEDS) {
      //          dir = 0 - dir;
      //        }

      //      Serial.print("leftLed= ");
      //      Serial.println(leftLed);
      Serial.print("currentLed= ");
      Serial.println(currentLed);
      //      Serial.print("rightLed= ");
      //      Serial.println(rightLed);

    }
  }
}

}

That's hard to say from what you posted so far. Post when you have a little more coded.

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