Help with neopixel code

Very new to programming. Wanted to make a Minecraft lamp for my son, with all the cool colors of the "ore" blocks. Colors are suppose to cycle thru, but sometimes it will reset back to red. Or display other colors not intended and not added in the code.

Totally lost where to start, Thanks in advance!!!

Code:

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
#define BUTTON_PIN 2

#define PIXEL_PIN 9 // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 1 // Number of NeoPixels

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

boolean oldState = HIGH;
int mode = 0; // Currently-active animation mode, 0-9

void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
// Get current button state.
boolean newState = digitalRead(BUTTON_PIN);

// Check if state changed from high to low (button press).
if((newState == LOW) && (oldState == HIGH)) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if(newState == LOW) { // Yes, still low
if(++mode > 6) mode = 0; // Advance to next mode, wrap around after #8
switch(mode) { // Start the new animation...
case 0:
colorWipe(strip.Color( 0, 0, 0), 50); // Black/off
break;
case 1:
colorWipe(strip.Color(204, 0, 9), 50); // Red Stone
break;
case 2:
colorWipe(strip.Color(127, 201 , 184), 50); // Diamond
break;
case 3:
colorWipe(strip.Color( 41, 195, 49), 50); // Emerald
break;
case 4:
colorWipe(strip.Color( 204, 189, 64), 50); // Gold
break;
case 5:
colorWipe(strip.Color( 176, 150, 133), 50); // Iron
break;
case 6:
colorWipe(strip.Color( 28, 62, 123), 50); // Lapis
break;
}
}
}

// Set the last-read button state to the old state.
oldState = newState;
}

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
}
}

What do your debug prints tell you is happening?

Please remember to use code tags when posting code.

Do NOT wire the button to the data line of the pixels.

-jim lee

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.

void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
  }
}

This is not doing what you seem to expect. Your wait time is ignored. So it'll happen lickity split.

-jim lee

jimLee:
Do NOT wire the button to the data line of the pixels.

-jim lee

Wire button to GND!

What everyone else said - one side of the button to ground, one side to pin 2.

Your color wipe is not using the wait time, but that doesn't matter because your "strip" is only one pixel long, anyway.

You know, you are on the right track. Just wire that button correctly and the code should work.

Oh - I prefer to power the pixel strip from Vin rather than +5v, that way the on-board regulator is not trying to supply all that current. But this is only ok if power is coming from USB - NOT if it's coming from a 9v wall wart.

  // Get current button state.
  boolean newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if((newState == LOW) && (oldState == HIGH)) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if(newState == LOW) {      // Yes, still low

Time for some obfuscated C, featuring the comma operator!

  // Get current button state.
  byte newState;

  // Check if state changed from high to low (button press).
  if((oldState == HIGH) 
      && ((newState = digitalRead(buttonPin))== LOW) 
      &&  (delay(20), (newState = digitalRead(buttonPin))== LOW)) {

  }
  oldState = newState;
[code]

Thanks for all the help. I did originally have it wired to ground. So now that's back on ground. I must have gotten confused on the wire placement at one point and accidentally put wire for pin2 on pin9 and vice versa. Now it doesn't show weird colors, but is turning off the light ~3 seconds.

so i'm thinking my laptop usb output wasn't enough power. plugged into a usb power block and it seems to be working fine. I'm testing now to see how long it will stay lit for. Thanks again everyone for all the feedback!