Use button to step through program

Hello all,

It's been a while since my last project, and I'm feeling a bit rusty on my coding. I have some neopixels connected to an arduino, and I want to connect a button to simply cycle through the different colors.

Something like (psudo code):

ColorWipe Red
button press
ColorWipe Green
button press
ColorWipe Blue
button press

Any thoughts on how best to code the button press without using a bunch of nested if statements? Thanks!

You need a variable to keep track of what stage you are at.

It starts at 0 First press increases it to 1 Second press increases it to 2 etc Fourth push sends it back to 0 (perhaps)

Put the code for the different sequences into functions - something like

void greenWipe() {
   if  (btnCount == 1) {
       // whatever makes a green wide
    }
}

Then loop() would be something like this

void loop() {
   readButton();
   greenWipe();
   redWipe();
   blueWipe();
}

...R

EDIT: I was using == instead of =. All is working now!

Great! Thanks for the help, I have it working now, all but the reset part. I tried using this:

	if (buttonPushCounter == 7)
	{
		buttonPushCounter == 1;
	}

to reset it after 7 pushes, but it just ignores it and keeps on counting. I tried placing it at several places throughout the code, but same result. Any idea how to loop it back to the beginning? Thanks again for the help!

#include <Adafruit_NeoPixel.h>

#define PINA 6    //ws2812 strip a
#define FADESPEED 5     // make this higher to slow down
#define NUMPIX 6  // number of pixels
#define buttonPin 4 // Input for the button

// Parameter 1 = number of pixels in strip
// Parameter 2 = 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_a = Adafruit_NeoPixel(NUMPIX, PINA, NEO_RGB + NEO_KHZ800);
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup()
{
	pinMode(buttonPin, INPUT_PULLUP);
	strip_a.begin();
	strip_a.show();     // Initialize all pixels to 'off'
	Serial.begin(9600);
	colorWipe(255, 255, 255);
	delay(1000);

}

void loop()
{

	buttonState = digitalRead(buttonPin);  // read the pushbutton input pin:

	if (buttonState != lastButtonState)		// compare the buttonState to its previous state
	{
		// if the state has changed, increment the counter
		if (buttonState == HIGH)
		{
			buttonPushCounter++;
			Serial.println("on");			// if the current state is HIGH then the button	went from off to on:
			Serial.print("number of button pushes:  ");
			Serial.println(buttonPushCounter);
		}
		else
		{
			Serial.println("off");				// if the current state is LOW then the button went from on to off
		}
	}
		if (buttonPushCounter == 7)
	{
		buttonPushCounter == 1;
	}
	lastButtonState = buttonState;	// save the current state as the last state for next time through the loop

	purpleWipe();
	blueWipe();
	whiteWipe();
	redWipe();
	aquaWipe();
	greenWipe();
	reset();
}

void purpleWipe()
{
	if  (buttonPushCounter == 1)
	{
		colorWipe(255, 0, 255);  //wipe to purple
	}
}


void blueWipe()
{
	if  (buttonPushCounter == 2)
	{
		colorWipe(0, 0, 255);  //wipe to blue
	}
}

void whiteWipe()
{
	if (buttonPushCounter == 3)
	{
		colorWipe(255, 255, 255);
	}
}

void redWipe()
{
	if  (buttonPushCounter == 4)
	{
		colorWipe(255, 0, 0);  //wipe to red
	}
}

void aquaWipe()
{
	if  (buttonPushCounter == 5)
	{
		colorWipe(0, 255, 255);  //wipe to aqua
	}
}

void greenWipe()
{
	if  (buttonPushCounter == 6)
	{
		colorWipe(0, 255, 0);  //wipe to green
	}
}
void reset()
{
	if (buttonPushCounter == 7)
	{
		buttonPushCounter == 1;
	}
}


void colorWipe(int r, int g, int b)
{
	for (int x = 0; x < NUMPIX; x++)        //incriment counter
	{
		strip_a.setPixelColor(x, r, g, b);      //turn on pixel
		delay(100);
		strip_a.show();
	}
}

OK nevermind! I was using == instead of =.

All is well! Thanks again!

int dimmerMode() {

int counter, setMode;

++counter;

delay(500);
switch (counter) {
case 1:
setMode = 1;
break;
case 2:
setMode = 2;
break;
case 3:
setMode = 3;
break;
case 4:
setMode = 4;
counter = 0;
break;
}

return setMode;
}

if (buttonPushCounter == 7)

I would use

if (buttonPushCounter >= 7)

That way if it somehow misses 7 it will still reset your counter.