I'm trying to get a button to turn my light strip on,

I’m trying to get a button to turn my light strip on, and have it fade in and out endlessly, until I push another button to turn it off. I can get it to turn on and fade in and out once, but that’s it. I can also turn it off. I just can’t get it to loop endlessly. Any help would be appreciated. I’m new to this so I’m not the best with the code. I’m using windows, and here is my code.

#include <FastLED.h>

#define NUM_LEDS 32
#define ledPin 3

CRGB led[NUM_LEDS];
int d = 10;
int repeat = 0;
int buttonApin = 9;
int buttonBpin = 7;

byte leds = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
FastLED.addLeds<NEOPIXEL, ledPin>(led, NUM_LEDS);
for (int r = 0; r < NUM_LEDS; r++) {
led[r] = CRGB(255, 0, 0);
}

FastLED.show();

for (int o = 0; o < NUM_LEDS; o++) {
led[o] = CRGB(0, 0, 0);
}

FastLED.show();
}

void setBlack(int val) {
for (int o = 0; o < NUM_LEDS; o++) {
led[o] = CRGB(0, 0, 0);
}
FastLED.show();
}

void setRed(int val) {
for (int r = 0; r < NUM_LEDS; r++) {
led[r] = CRGB(val, 0, 0);
}
FastLED.show();
}

void loop() {
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
red();

}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
for (int o = 0; o < 256; o++) {
setBlack(o);
delay(5);
}

}
}
}

void red(){
if (repeat <=10){
fade();
}
else {
for (int o = 0; o < 256; o++) {
setBlack(o);
delay(5);
}
}
repeat = 0;
return repeat;
setup();
}

void fade() {
for (int r = 0; r < 256; r++) {
setRed(r);
delay(d);
}
for (int r = 255; r > 0; r–) {
setRed(r);
delay(d);
}
repeat = repeat + 1;
return repeat;

}

First of all, try to get rid of all these delays. Blocking code is never a good solution. And in this case there are several other ways to solve this.

And your leds will only fade in and out if you press the button. As soon as you release it, it will just finish the (blocking) loop and then stop.

Again, get rid of the blocking code and use I. E. a trigger variable instead of the button state to run your fade function. Toggle the variable with the buttons.

@Sven_Eric_Nielsen Thank’s for the help, I’ll do some research to see if I can figure out how to make that happen.

@Ben_Bauer ​ Also, when posting code please use http://gist.github.com so it’s easier to read the code and line numbers can be referenced.

@marmil Will do, new to all this so any help is appreciated. Thanks!

So, I’ve been trying to figure this out and I think I’ve gotten it to a point that I like except for one problem. I’m not sure how to get it to reset/restart from the beginning. I can get it to turn on the fade, then stay solid, and then off. I just don’t know how to get it to restart after that.