Neopixel with button

Hi I'm trying to make some neopixels run on a Nano whilst a button is pressed then stop when the button is released, I have located a program that does this but it does not display what I want, I also have a program that displays what I want it to but can't seem to put the two together.

Can anyone help please. Thanks Mark

This is the code that displays correctly on the neopixel leds:

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(25, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
// Some example procedures showing how to display to the pixels:

reverseColorWipe(strip.Color( 255, 255, 255), 50); // white
delay(10);
reverseColorWipe(strip.Color(0, 0, 0), 50); // blank
delay(10);
reverseColorWipe(strip.Color( 255, 255, 255), 50); //white
delay(10);
reverseColorWipe(strip.Color(0, 0, 0), 50); //blank
delay(10);

}
void reverseColorWipe(uint32_t c, uint8_t wait)
{
for(int16_t i=(strip.numPixels()-1); i>=0; i--)
{
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}

This code works fine ie, runs when a button is pressed and stops after the final loop when the button is released. I just cannot remove the unwanted part of the code and fit in the part I need.

#include <Adafruit_NeoPixel.h>

// Define pin to connect Neopixels to
#define PIN 6

//Define pin for pushbutton to connect to
const int buttonPin = 2;
const int ledPin = 13;

// Variables will change:
int buttonState = 0; // current state of the button

Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN);

uint8_t mode = 1, // Current animation effect
offset = 0; // Position of spinny eyes
uint32_t color1 = 0xF8F8FF; // White 0x006688; // Light Blue
uint32_t prevTime;

boolean turn_on=false;

void setup() {
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
strip.begin();
prevTime = millis();
}

void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
turn_on = true;
}
else {
}

uint8_t i;
uint32_t t;

if (turn_on == true) {
digitalWrite(ledPin, HIGH);
switch(mode) {

case 0: // Spinny wheels (8 LEDs on at a time)
for(i=0; i<30; i++) {
uint32_t c = 0;
if(((offset + i) & 6) < 2) c = color1; // 2 pixels on...
// if(((offset + i) & 5) < 2) c = color1; // 4 pixels on...
strip.setPixelColor( i, c); // First eye
// strip.setPixelColor(31-i, c); // Second eye (flipped)
}

strip.show();
offset++;
delay(40);
break;

case 1: // Black Out
strip.show();
turn_on = false;
digitalWrite(ledPin, LOW);
}

t = millis();
if((t - prevTime) > 1000) { // Every 8 seconds...
mode++; // Next mode
if(mode > 1) { // End of modes?
mode = 0; // Start modes over
}
for(i=0; i<32; i++) strip.setPixelColor(i, 0);
prevTime = t;
}
}
}

This code works fine ie, runs when a button is pressed and stops after the final loop when the button is released. I just cannot remove the unwanted part of the code and fit in the part I need.

The code where you tried to do that is?
That code actually does?

sorry meant to add that to

#include <Adafruit_NeoPixel.h>

#define PIN 6 // Define pin to connect Neopixels to

//Define pin for pushbutton to connect to
const int buttonPin = 2;
const int ledPin = 13;

// Variables will change:
int buttonState = 0; // current state of the button

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(25, PIN, NEO_GRB + NEO_KHZ800);

uint8_t mode = 1, // Current animation effect
uint32_t prevTime;

boolean turn_on=false;

void setup() {
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
turn_on = true;
}
else {
}

if (turn_on == true) {
digitalWrite(ledPin, HIGH);
switch(mode) {

case 0: // turn on leds
reverseColorWipe(strip.Color( 255, 255, 255), 50); // white
delay(10);
reverseColorWipe(strip.Color(0, 0, 0), 50); // blank
delay(10);
reverseColorWipe(strip.Color( 255, 255, 255), 50); //white
delay(10);
reverseColorWipe(strip.Color(0, 0, 0), 50); //blank
delay(10);

void reverseColorWipe(uint32_t c, uint8_t wait)
for(int16_t i=(strip.numPixels()-1); i>=0; i--)
{
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
break;

case 1: // Black Out
strip.show();
turn_on = false;
digitalWrite(ledPin, LOW);
}

And that code does what? How does that differ from what you want?

I need to produce an arrow pointer when a button is pressed and want to use the reverse colour wipe in stead of the spinny wheel effect.
Thanks

Does that last code even compile?

Hi thats where I think I getting them mixed up and need a little help as it's coming up with errors
Thanks

thats where I think I getting them mixed up and need a little help as it's coming up with errors

It's easier pulling hens teeth than to get you to volunteer anything. I'm about to give up. What errors?

Use Tools + Auto Format (if you can) to see that you have a function defined inside of loop(). That is not allowed.

I think you want your loop() function in the code that displays correctly on the LEDs to look like this:

void loop() {

  int wipeNum = 0;

  // while button down
  while (digitalRead(buttonPin) == HIGH) {

    // perform wipe
    switch( wipeNum )
    {
    case 0:
      {

        reverseColorWipe(strip.Color( 255, 255, 255), 50); // white
        delay(10);
      }

    case 1:
      {
        reverseColorWipe(strip.Color(0, 0, 0), 50); // blank
        delay(10);
      }
    case 2:
      {
        reverseColorWipe(strip.Color( 255, 255, 255), 50); //white
        delay(10);
      } 
    case 3: 
      {
        reverseColorWipe(strip.Color(0, 0, 0), 50); //blank
        delay(10);
      }

    default:
      break;

    }

    // next wipe
    ++wipeNum %= 4;

  } 

}


}

You will need to add the definition of buttonPin. It should perform each wipe in turn as long as the button is held down.

it probably will not, because of this:

int wipeNum = 0;

@Blue Eyes, you can learn about variable scope here

you want:

static int wipeNum = 0;

@ BulldogLowell Huh? The variable "wipeNum" is declared in loop() and incremented inside a while loop inside loop() as long as buttonPin is HIGH.