I'd like to be able to change pattern sequences with a button press.

I’d like to be able to change pattern sequences with a button press.

Using the DemoReel100 example sketch could anyone tell me what code to change so as to change patterns on a button press rather than after an amount of time?

I’ll add:
int buttonPin = 5;
to the setup.

I’m not sure about what to change this code to though:
EVERY_N_SECONDS( 10 ) { nextPattern(); }

Hi Danny if you’re using arduino and let’s say LPD8806 strip you can get a really responsive button press with an interrupt service routine (https://www.arduino.cc/en/Reference/AttachInterrupt) which increments a counter (e.g. seq_mode++), the counter then defines which of the effects you get. If you’re interested i can share some similar code when i get home

First off, I’d find a button library and here’s one that I like:

15 minutes later (and after a couple of drinks), I got:

Your challenge now is to figure out how to do it, but without using a button library.

@Chris_Thodey
Thank you for the reply.
I’m using WS2811 strings and strips.
I’m definitely interested in checking out your code.

Cheers.

@Andrew_Tuline
Wow, a button library… I hadn’t thought of that. Seemed too simple a thing to have a library for, time for me to get schooled.

Thanks for the reply.

Hey Danny,
This routine is designed to work with LPD8806 strips and uses adafruit’s library. (hint: it’s not as good as fastLED, i’ve just used it for ages…)
But it’ll give you the idea of how to do this switch hopefully

#include “LPD8806.h”
#include “SPI.h”

const int dataPin = 9;
const int clockPin = 8;
const int numLeds = 6;
int modeValue = 3; //mode variable
int colourValue = 0; //colour Variable
LPD8806 strip = LPD8806(numLeds, dataPin, clockPin);

void setup()
{
strip.begin();
Serial.begin(9600);
strip.show();
attachInterrupt(0,ISRButton,LOW); // this is the interrupt service routine i’m talking about.
digitalWrite(2,HIGH);
}

// function prototypes
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
void rainbowCycle(uint8_t wait);
uint32_t Wheel(uint16_t WheelPos);

void loop() {
if (modeValue == 0){ scanner(127,0,0, 100); }
if (modeValue == 1){ rainbowCycle(0); }
Serial.print("modeValue = "); Serial.println(modeValue);
delay(10);
// Clear strip data before start of next effect
for (int i=0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0);
}
}

void ISRButton() //here we call the ISR to run. Note we put no delays here.
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it’s a bounce and ignore
if (interrupt_time - last_interrupt_time > 200)
{
modeValue++;
if(modeValue>3) modeValue=0;
}
last_interrupt_time = interrupt_time;
}

// Cycle through the color wheel, equally spaced around the belt
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for (j=0; j < 384 * 5; j++) { // 5 cycles of all 384 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 384-color
// wheel (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 384 is to make the wheel cycle around
strip.setPixelColor(i, Wheel(((i * 384 / strip.numPixels()) + j) % 384));
}
strip.show(); // write all the pixels out
delay(wait);
}
}

// “Larson scanner” = Cylon/KITT bouncing light effect
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
int i, j, pos, dir;

pos = 0;
dir = 1;

for(i=0; i<((strip.numPixels()-1) * 8); i++) {
// Draw 5 pixels centered on pos. setPixelColor() will clip
// any pixels off the ends of the strip, no worries there.
// we’ll make the colors dimmer at the edges for a nice pulse
// look
strip.setPixelColor(pos - 1, strip.Color(r/3, g/3, b/3));
strip.setPixelColor(pos, strip.Color(r, g, b));
strip.setPixelColor(pos + 1, strip.Color(r/3, g/3, b/3));

strip.show();
delay(wait);
// If we wanted to be sneaky we could erase just the tail end
// pixel, but it's much easier just to erase the whole thing
// and draw a new one next time.
for(j=-2; j<= 2; j++) 
    strip.setPixelColor(pos+j, strip.Color(0,0,0));
// Bounce off ends of strip
pos += dir;
if(pos < 0) {
  pos = 1;
  dir = -dir;
} else if(pos >= strip.numPixels()) {
  pos = strip.numPixels() - 2;
  dir = -dir;
}

}
}
/* Helper functions */

//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g - b - back to r

uint32_t Wheel(uint16_t WheelPos)
{
byte r, g, b;
switch(WheelPos / 128)
{
case 0:
r = 127 - WheelPos % 128; // red down
g = WheelPos % 128; // green up
b = 0; // blue off
break;
case 1:
g = 127 - WheelPos % 128; // green down
b = WheelPos % 128; // blue up
r = 0; // red off
break;
case 2:
b = 127 - WheelPos % 128; // blue down
r = WheelPos % 128; // red up
g = 0; // green off
break;
}
return(strip.Color(r,g,b));
}

Cheers
Chris