Thank you in advance for any help you can give. I am pulling my hair out at this point.
I have watched and read countless tutorials on how to use a button to switch modes on an LED strip, but no matter what code or board configuration I use, the button does nothing. Sometimes I can see the button is registering because the LED strip will very slightly fade or the small LED on the Arduino will blink, but the LED strip doesn't change modes. I have read about the delay function and that millis is better, but I have seen code work with a button and delay and would prefer it because I can't get millis to work with my code either.
Here is the main code I am trying to get working (but I have tried other working posted ones with no success, so I'm thinking it's my board setup). I am using the FastLED library and the OneButton library. All of these modes have been tested independently of a button and work as intended.
//bring up libraries
#include <FastLED.h>
#include <OneButton.h>
// set number of strips, leds per strip, total leds, and brightness
#define NUM_STRIPS 3
#define NUM_LEDS_A 33
#define NUM_LEDS_B 15
#define NUM_LEDS_C 12
#define NUM_LEDS NUM_LEDS_A+NUM_LEDS_B+NUM_LEDS_C
#define BRIGHTNESS 50
// set parameters for patterns later
int fadeAmount = 1;
int brightness = 0;
uint8_t colorIndex[NUM_LEDS];
// create integer for pattern counter, keeps track of which pattern showing
uint8_t patternCounter = 0;
DEFINE_GRADIENT_PALETTE( greentwinkle_gp )
{
0, 0, 0, 0,
64, 21, 249, 7,
128, 80, 255, 67,
197, 21, 249, 7,
255, 0, 0, 0
};
//Activate the palette
CRGBPalette16 greentwinkle = greentwinkle_gp;
// set button to data pin 12
#define BTN_PIN 12
// create an array of all the leds
CRGB leds[NUM_LEDS];
// push button connected to pin 12 and GND; 2nd false = add 10k resistor to ground, true = internal arduino resistor
OneButton btn = OneButton(BTN_PIN, true, false);
void setup() {
// tell FastLED there's 33 NEOPIXEL leds on pin 6, starting at index 0 in the led array
FastLED.addLeds<NEOPIXEL, 6>(leds, 0, NUM_LEDS_A);
// tell FastLED there's 15 NEOPIXEL leds on pin 7, starting at index 33 in the led array
FastLED.addLeds<NEOPIXEL, 7>(leds, NUM_LEDS_A, NUM_LEDS_B);
// tell FastLED there's 12 NEOPIXEL leds on pin 8, starting at index 48 in the led array
FastLED.addLeds<NEOPIXEL, 8>(leds, NUM_LEDS_A+NUM_LEDS_B, NUM_LEDS_C);
// set brightness level
FastLED.setBrightness(BRIGHTNESS);
// call next pattern when button is clicked
btn.attachClick(nextPattern);
}
void loop()
{
switch (patternCounter) {
case 0:
SteadyOn(); // all leds steady green
break;
case 1:
ThreeSteadyOn(); // segments of 3 leds steady green
break;
case 2:
Chase3(); // chase 3 pixels at a time
break;
case 3:
Chase3Fast(); // chase 3 pixels faster
break;
case 4:
Glow(); // glow green
break;
case 5:
Twinkle(); // twinkle green
break;
case 6:
Blackout(); // all leds off
break;
}
FastLED.show();
btn.tick(); // checks the button each loop, MUST HAVE
}
void nextPattern() {
patternCounter = (patternCounter + 1) %7; // Advance the pattern; Change number after % to number of patterns
}
void SteadyOn() {
for(int i = 0; i < NUM_LEDS; i++ )
{
leds[i].setRGB(0,255,0); // Set Color here
}
}
void ThreeSteadyOn() {
leds[44] = CRGB::Green;
leds[43] = CRGB::Green;
leds[42] = CRGB::Green;
leds[38] = CRGB::Green;
leds[37] = CRGB::Green;
leds[36] = CRGB::Green;
leds[32] = CRGB::Green;
leds[31] = CRGB::Green;
leds[30] = CRGB::Green;
leds[26] = CRGB::Green;
leds[25] = CRGB::Green;
leds[24] = CRGB::Green;
leds[20] = CRGB::Green;
leds[19] = CRGB::Green;
leds[18] = CRGB::Green;
leds[14] = CRGB::Green;
leds[13] = CRGB::Green;
leds[12] = CRGB::Green;
leds[8] = CRGB::Green;
leds[7] = CRGB::Green;
leds[6] = CRGB::Green;
leds[2] = CRGB::Green;
leds[1] = CRGB::Green;
leds[0] = CRGB::Green;
}
void Chase3() {
for(int i = 32; i > -1; i--) {
leds[i] = CRGB::Green;
leds[i-1] = CRGB::Green;
leds[i-2] = CRGB::Green;
FastLED.delay(250);
leds[i] = CRGB::Black;
leds[i-1] = CRGB::Black;
leds[i-2] = CRGB::Black;
}
for(int j = 33; j <48; j++) {
leds[j] = CRGB::Green;
leds[j+1] = CRGB::Green;
leds[j+2] = CRGB::Green;
FastLED.delay(250);
leds[j] = CRGB::Black;
leds[j+1] = CRGB::Black;
leds[j+2] = CRGB::Black;
}
for(int k = 48; k <60; k++) {
leds[k] = CRGB::Green;
leds[k+1] = CRGB::Green;
leds[k+2] = CRGB::Green;
FastLED.delay(250);
leds[k] = CRGB::Black;
leds[k+1] = CRGB::Black;
leds[k+2] = CRGB::Black;
}
}
void Chase3Fast() {
for(int i = 32; i > -1; i--) {
leds[i] = CRGB::Green;
leds[i-1] = CRGB::Green;
leds[i-2] = CRGB::Green;
FastLED.delay(100);
leds[i] = CRGB::Black;
leds[i-1] = CRGB::Black;
leds[i-2] = CRGB::Black;
}
for(int j = 33; j <48; j++) {
leds[j] = CRGB::Green;
leds[j+1] = CRGB::Green;
leds[j+2] = CRGB::Green;
FastLED.delay(100);
leds[j] = CRGB::Black;
leds[j+1] = CRGB::Black;
leds[j+2] = CRGB::Black;
}
}
void Glow() {
for(int i = 0; i < NUM_LEDS; i++ )
{
leds[i].setRGB(0,255,0); // Set Color HERE!!!
leds[i].fadeLightBy(brightness);
}
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if(brightness == 0 || brightness == 255)
{
fadeAmount = -fadeAmount ;
}
delay(9); // This delay sets speed of the fade. I usually do from 5-75 but you can always go higher.
}
void Twinkle() {
//Fill the colorIndex array with random numbers
for (int i = 0; i < NUM_LEDS; i++) {
colorIndex[i] = random8();
}
//color each pixel from the palette using the index from colorIndex
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette(greentwinkle, colorIndex[i]);
}
EVERY_N_MILLISECONDS(2){
//switch on an led at random, choosing a random color from the palette
//leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(greentwinkle, random8(), 255, LINEARBLEND);
for (int i = 0; i < NUM_LEDS; i++){
colorIndex[i]++;
//}
}
//fade all LEDS down by 1 in brightness each time this is called
fadeToBlackBy(leds, NUM_LEDS, 10);
}}
void Blackout() {
fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
}
Here is my board setup. I am using an Arduino Uno and Neopixel LED strips: