Strange issue in simple Neopixel project

I'm working on a very simple project. I have 10 buttons and 10 Neopixels. All Neopixels start in the OFF position. When button A is pressed, Neopixel A turns ON. When button A is released, Neopixel A turns OFF.

At the moment, I am testing and troubleshooting with just 2 buttons and 2 Neopixels and an Arduino RedBoard.

I have a 1000uF cap and a 1uF cap attached to the 5V and GND pins connected to the Neopixels. I have a resistor between pin 12 on the RedBoard and the DIN pin and I have resistors on both of the button inputs. The buttons close the circuit when pressed and open it when released.

Here is my code

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIXEL_PIN    12 

#define PIXEL_COUNT 2

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGB + NEO_KHZ800);

void setup() {



pinMode(2, INPUT);
digitalWrite(2, HIGH);

pixel.begin();
  pixel.show();

}

void loop() {
  // put your main code here, to run repeatedly:

if (digitalRead(2) == LOW)
{
  digitalWrite(PIXEL_PIN, LOW);
}
else
{
 
  digitalWrite(PIXEL_PIN, HIGH);
}



    pixel.setPixelColor(0, pixel.Color(0,100,0));
pixel.setPixelColor(1, pixel.Color(0,0,0));
    pixel.show();


}

It works... kinda.
As soon as the code is uploaded to the RedBoard, the RED and GREEN diodes of the first Neopixel "0" immediately turn on.

When I press the button attached to pin 2, the first Neopixel turns green (as it should).

If I rewrite the code to turn OFF the Neopixel in all cases...

    pixel.setPixelColor(0, pixel.Color(0,0,0));
pixel.setPixelColor(1, pixel.Color(0,0,0));
    pixel.show();

... only the RED diode turns on (brighter now that the GREEN diode is not drawing current).
Now, when I press the button attached to pin 2, the Neopixel turns off.

I can't figure out what I'm doing wrong.

Help??

I get the impression that you are confusing which pixel you are addressing at each point, the first argument in the function call.

And you do not appear to have shown us each version of the code in question that corresponds to a given behaviour.

I get the impression that you are confusing which pixel you are addressing at each point, the first argument in the function call.

I'm not sure which function you are referring to but I don't think I am.

The first Neopixel is "0"

pixel.setPixelColor(0, pixel.Color(0,100,0))

And you do not appear to have shown us each version of the code in question that corresponds to a given behaviour.

And I believe I have shown both versions of the code representing the two behaviors I have described.

The first code window shows the entire code I have written. To be clear, all Neopixels should be off when the code is uploaded to the RedBoard.

digitalWrite(PIXEL_PIN, LOW)

When I press a button, the first Neopixel "0" should show GREEN. It should turn OFF when I release the button.

if (digitalRead(2) == LOW)
{
  digitalWrite(PIXEL_PIN, LOW);
}
else
{
 
  digitalWrite(PIXEL_PIN, HIGH);
}



    pixel.setPixelColor(0, pixel.Color(0,100,0));
pixel.setPixelColor(1, pixel.Color(0,0,0));
    pixel.show();

This is not what happens.
I see RED/GREEN immediately on the first Neopixel (Red and Green diodes of the first Neopixel are on). When I press the button, the first Neopixel turns GREEN (Red diode turns OFF and Green remains ON and becomes brighter).

The second code window refers to a small change I made. Rather than repost the entire program, I've simply posted the section I changed.

    pixel.setPixelColor(0, pixel.Color(0,0,0));
pixel.setPixelColor(1, pixel.Color(0,0,0));
    pixel.show();

Notice: All Neopixels are set to show NO COLOR when the button is pressed.

Instead, I see RED immediately on the first Neopixel (Red diode of the first Neopixel is on). When I press the button, the first Neopixel turns completely OFF (no diodes are lit).

Also, if I edit this code to work for a normal LED (I simply delete the Neopixel stuff at the bottom), it works perfectly. The regular LED will turn ON when the button is pressed and will turn OFF when the button is released. It seems to be an issue with my Neopixel code.

void setup() {



pinMode(2, INPUT);
digitalWrite(2, HIGH);

pixel.begin();
  pixel.show();

}

void loop() {
  // put your main code here, to run repeatedly:

if (digitalRead(2) == LOW)
{
  digitalWrite(PIXEL_PIN, LOW);
}
else
{
 
  digitalWrite(PIXEL_PIN, HIGH);
}
}

Also, also, I don't think I have destroyed any hardware. The Neopixel example code that comes with the library works just fine with my 2 Neopixels.

Well, let me see.

Here is your original code, with explanatory comments.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>  // No idea what this does.
#endif

#define PIXEL_PIN    12
#define PIXEL_COUNT 2

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGB + NEO_KHZ800);

void setup() {
  pinMode(2, INPUT);      // Define pin 2 as input
  digitalWrite(2, HIGH);  // Enable pullup

  pixel.begin();
  pixel.show();
}

void loop() {
  // put your main code here, to run repeatedly:

  if (digitalRead(2) == LOW)
  {
    digitalWrite(PIXEL_PIN, LOW); // Randomly set NeoPixel line to LOW
  }
  else
  {
    digitalWrite(PIXEL_PIN, HIGH); // Randomly set NeoPixel line to HIGH
  }

  pixel.setPixelColor(0, pixel.Color(0, 100, 0)); // Set first pixel to Green
  pixel.setPixelColor(1, pixel.Color(0, 0, 0));   // Set second pixel to dark
  pixel.show();                                   // Display the pixels
}

Rather than repost the entire program, I've simply posted the section I changed.

Please do not do this. It is not recommended in the forum rules.

Please read this:-
How to use this forum

Have you an external pull up resistor? If not then you are not enabling the internal pull up ones.