Complete sketch:
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 18 // number of LEDs in the strip
#define LED_PIN 4 // pin to which the Neopixel strip is connected
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int brightness = 0; // current brightness of the LEDs
int fadeAmount = 5; // how much to fade the LEDs by
int currentPixel = 0;
volatile int mode = 3;
int startMode = 3;
unsigned long timesince;
int nextRando = 0;
int lastRando = 0;
bool bright = true;
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t yellow = strip.Color(255, 255, 0);
uint32_t white = strip.Color(255, 255, 255);
uint32_t cyan = strip.Color(0, 255, 255);
uint32_t orange = strip.Color(255, 165, 0);
uint32_t purple = strip.Color(200, 3, 255);
uint32_t colors[] = {red, green, blue, yellow, white, cyan, orange, purple};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, INPUT);
strip.begin();
strip.setBrightness(255);
strip.show();
attachInterrupt(digitalPinToInterrupt(2), onOff, HIGH);
attachInterrupt(digitalPinToInterrupt(3), changeMode, HIGH);
timesince = millis();
}
void loop() {
if (mode == 1) {
while (nextRando == lastRando) {
nextRando = random(8);
}
chase(colors[nextRando]);
lastRando = nextRando;
}
else if (mode == 2) { // SLOW-CHANGE RAINBOW FLASH
rainbow(40);
}
else if (mode == 3) {
rainbow(5);
}
else if (mode == 4) { // SLOW MOVING RAINBOW WHEEL
rainbowCycle(20);
}
else if (mode == 5) { // FASTER RAINBOW WHEEL
rainbowCycle(1);
}
}
void onOff() {
if (millis() - timesince > 1000) {
timesince = millis();
if (bright == false) {
strip.setBrightness(255);
Serial.println("Brightness set to max.");
bright = true;
} else if (bright == true) {
strip.setBrightness(0);
Serial.println("Brightness set to 0.");
bright = false;
}
strip.show();
}
}
void changeMode() {
if (millis() - timesince > 1000) {
timesince = millis();
mode += 1;
if (mode == 9) {
mode = 1;
}
Serial.print("I have just run the changeMode routine. The state is now: ");
Serial.print(mode);
Serial.println(".");
}
}
static void chase(uint32_t c) {
startMode = mode;
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.show();
delay(50);
if (startMode != mode){
break;
}
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
startMode = mode;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
if (startMode != mode) {
break;
}
}
}
void rainbowCycle(uint8_t wait) {
startMode = mode;
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
if (startMode != mode) {
break;
}
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
What happens when mode reaches 10
I have a debouncing routine built in. The reason I didn't include the entire sketch was because I only needed help on one specific part, and my question was answered without needing the entire sketch. Nevertheless, I see the importance of copying the whole thing, I just wanted a quick response and didn't want to overwhelm potential answerers with the 200+ lines, when my question only revolved around maybe 10.