WS2812B+HC-SR04+ Arduino Mega 2560, light up each LED to display distance

Hi There. I am trying to create an LED strip that lights up as you move closer and further away. I have taken the code from a video on you tube.

I am using a Elegoo Mega 2560 R3, an HC-SR04, and a WS2812B 5meter strip from BTF-lighting. in the diagram below the LED strip is powered off the breadboard where i have an external 5v 40A power supply connected, and there is a capacitor in line. the DATA to the LED strip is from PWN 6.
Sorry I have no idea why it would not work, and no idea how to get it to work so any advice would be helpful. I feel like i may just be missing something really simple.
Many thanks


#include <Adafruit_NeoPixel.h>
#include <HCSR04.h>

#define PIN        6 // Which pin on the Arduino is connected to the NeoPixels?

#define NUMPIXELS 29 // How many NeoPixels are attached to the Arduino?

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

UltraSonicDistanceSensor distanceSensor(9, 10);  // Initialize sensor that uses digital pins 9 and 10.


int current_Last_LED=0;

void setup() 
{  
  pixels.begin(); 
}

void loop() 
{
  
  pixels.clear();                               // Set all pixel colors to 'off'
  current_Last_LED=distanceSensor.measureDistanceCm()*0.8;
 
 
  for(int i=0; i<current_Last_LED; i++)                      // For each pixel in strip...
  { 
    pixels.setPixelColor(i, 0,0,255); 
  }
  
  pixels.show();                                  // Send the updated pixel colors to the hardware.
  
}


Those are addressable LEDs - you don't control them with PWM!

There is a link to code in the video - why don't you just use that?

I did
the code above
is the from the link.

OH are you saying they should be from a Digital PIN.
on the elegoo R3 it looks like digital pins start at 21.

No. All pins are digital pins (starting from 0). Analog pins can also be used as digital pins.

thank you

I mean it looks fairly simple when all the code is there and basically it seems hes provided everything but it just does not work.
do i need to enter any details into the code where he has "()"

current_Last_LED=distanceSensor.measureDistanceCm()*0.8;

should that have the number of LEDs in it "(100)*0.8; ?

the only difference I can tell is that my LED is powered from the breadboard 5V supply.
and not the 5V PIN on the Elegoo.

Do you have the ground of your external supply connected to the Arduino ground?

yes. the GND of the power supply is in the breadboard and it comes off the BBoard into GND 1 and the HC-SR04 GND into the GND2 on the Elegoo. There are 2 GND PINS on the Elegoo.

imagine the button is the power supply, and the LED is the strip.

only GNDs in this pic.

it all looks like this.
again the button is the power supply, and the LED is the strip.
also the is a capacitor where the5v and GND come off the board into the strip.

Can you just leave out getting the entire thing to work all at once, and just get one pixel on the strip to turn on?

Have you connected the right end of the strip to the digital output that is to control it? Sounds like a dumb mistake, until you make it. Ppl do…

All you need is begin, setPixelColor and show, maybe even turn on pixel numbers 1, 2 and 3.

All in setup() with an empty loop() function.

Also, after you

pixels.show();    

Stick in a delay(300); in case the code is so fast you don't even see the pixels for the very very very brief time between show() and clear().

a7

1 Like

thank you so much.
would that look like this?


#include <Adafruit_NeoPixel.h>
#include <HCSR04.h>

#define PIN        6 // Which pin on the Arduino is connected to the NeoPixels?

#define NUMPIXELS 29 // How many NeoPixels are attached to the Arduino?

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

UltraSonicDistanceSensor distanceSensor(9, 10);  // Initialize sensor that uses digital pins 9 and 10.


int current_Last_LED=0;

void setup() 
{  
  pixels.begin(); pixels.
  setPixelColor(i, 0,0,255);
  pixels.show();
  delay (300)
  pixels.clear(); 
}

void loop() 
{
  
}

sorry like i said i am new to this i dont know where to set the color or which pixels.
Got a lot to learn.

delay(300);

oh yes.

Close.

void setup() {
  pixels.begin();

  pixels.setPixelColor(1, 0, 0, 255);    // blue
  pixels.setPixelColor(2, 0, 255, 0);    // green
  pixels.setPixelColor(3, 255, 0, 0);    // red

  pixels.show();
  delay (1000);
}

I usually add a white pixel at the last pixel, and leave that in the code forever as a startup check on the strip.

HTH

a7

oh ok cool.
i was thinking
this

void setup() 
{  
  pixels.begin(); pixels.
  setPixelColor(i, 0,150,255);
  pixels.show(1, 2, 3);
  delay (300);
  pixels.clear(); 
}

void loop() 
{
  
}
`

Sry, check my last post, I made a mistake and edited it before 60 seconds had elapsed. :expressionless:

You have to name the object when you call a method on it, like

    pixels.clear();

And

   pixels.

is just incomplete and will throw you an error.

a7

yep iv had that a few times.