Control LED Strip functions using SINGLE Push Button Help!

Hello, I am attempting to modify my code, which currently has two functions that I use:
RunningLightsAll() and FadeInOutAll(). I am trying to have the button alternate between the two. I don't want to hold the button down for some time but rather have a simple push of the button change the current function that is running. I currently have the button being "read" during the While loops of the function to change between the two. However, this only works if timed correctly(@ location in code). I need help to change between the two. Any guidance or tools in the right direction are greatly appreciated.



<br>[size=0.8em]Code: [url=https://arduinogetstarted.com/tools/arduino-code-highlighter]see how to post code[/url] [/size]<br>




---




```
[size=0.8em]#include "FastLED.h"
#define[nobbc] NUM_LEDS 15[/nobbc]
CRGB leds[NUM_LEDS];
#define[nobbc] PIN 6             [/nobbc][nobbc]// the pin that the LED is attached t[/nobbc]

const int[nobbc] buttonPin = 2;  [/nobbc][nobbc]// the pin that the pushbutton is attached to[/nobbc]

[nobbc]// Variables that will chane[/nobbc]
int[nobbc] buttonPushCounter = 1;    [/nobbc][nobbc]// Counter for button Presses[/nobbc]
int[nobbc] buttonState = 1;          [/nobbc][nobbc]// Current state of button[/nobbc]
int[nobbc] lastButtonState = 0;      [/nobbc][nobbc]// Previous state of button[/nobbc]
void setupnobbc[/nobbc]
{
 FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

pinMode[nobbc](buttonPin, [/nobbc]INPUT[nobbc]);[/nobbc]
 pinMode[nobbc](PIN, [/nobbc]OUTPUT[nobbc]);[/nobbc]

[nobbc]  [/nobbc]Serial[nobbc].[/nobbc]beginnobbc;[/nobbc]

}

[nobbc]// *** REPLACE FROM HERE ***[/nobbc]
void loopnobbc {[/nobbc]

FadeInOutAll(5);
 RunningLightsAll(100);

}

[nobbc]// ---> here we define the effect function <---[/nobbc]

void[nobbc] FadeInOutAll([/nobbc]int[nobbc] SpeedDelay) {[/nobbc]
 float[nobbc] r, g, b;[/nobbc]

[nobbc]/*********** PART 1 ************/[/nobbc]

[nobbc]// Square Turns OFF[/nobbc]

delaynobbc;[/nobbc]

while[nobbc] (1) {[/nobbc]

[nobbc]// Triangle Turns ON[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 0; k < 256; k = k + 1) {[/nobbc]
     r = (k / 256.0) * 0x00;
     g = (k / 256.0) * 0xff;
     b = (k / 256.0) * 0x00;
     setPixel(0, r, g, b);
     setPixel(1, r, g, b);
     setPixel(2, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

[nobbc]// Square Turns OFF[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 255; k >= 0; k = k - 2) {[/nobbc]
     r = (k / 256.0) * 0xff;
     g = (k / 256.0) * 0x00;
     b = (k / 256.0) * 0xff;
     setPixel(11, r, g, b);
     setPixel(12, r, g, b);
     setPixel(13, r, g, b);
     setPixel(14, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

[nobbc]//Circle Turns ON[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 0; k < 256; k = k + 1) {[/nobbc]
     r = (k / 256.0) * 0xff;
     g = (k / 256.0) * 0x00;
     b = (k / 256.0) * 0x00;
     setPixel(3, r, g, b);
     setPixel(4, r, g, b);
     setPixel(5, r, g, b);
     setPixel(6, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

[nobbc]/****  PART 2 ****************/[/nobbc]
   [nobbc]// Triangle Turns OFF[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 255; k >= 0; k = k - 2) {[/nobbc]
     r = (k / 256.0) * 0x00;
     g = (k / 256.0) * 0xff;
     b = (k / 256.0) * 0x00;
     setPixel(0, r, g, b);
     setPixel(1, r, g, b);
     setPixel(2, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

[nobbc]// X Turns ON[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 0; k < 256; k = k + 1) {[/nobbc]
     r = (k / 256.0) * 0x00;
     g = (k / 256.0) * 0x00;
     b = (k / 256.0) * 0xff;
     setPixel(7, r, g, b);
     setPixel(8, r, g, b);
     setPixel(9, r, g, b);
     setPixel(10, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }
   [nobbc]/*********   PART 3 *********/[/nobbc]

[nobbc]// Circle Turns OFF[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 255; k >= 0; k = k - 2) {[/nobbc]
     r = (k / 256.0) * 0xff;
     g = (k / 256.0) * 0x00;
     b = (k / 256.0) * 0x00;
     setPixel(3, r, g, b);
     setPixel(4, r, g, b);
     setPixel(5, r, g, b);
     setPixel(6, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

[nobbc]// Square Turns ON[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 0; k < 256; k = k + 1) {[/nobbc]
     r = (k / 256.0) * 0xff;
     g = (k / 256.0) * 0x00;
     b = (k / 256.0) * 0xff;
     setPixel(11, r, g, b);
     setPixel(12, r, g, b);
     setPixel(13, r, g, b);
     setPixel(14, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

[nobbc]/*********   PART 4 *********/[/nobbc]

[nobbc]// X Turns OFF[/nobbc]
   for[nobbc] ([/nobbc]int[nobbc] k = 255; k >= 0; k = k - 2) {[/nobbc]
     r = (k / 256.0) * 0x00;
     g = (k / 256.0) * 0x00;
     b = (k / 256.0) * 0xff;
     setPixel(7, r, g, b);
     setPixel(8, r, g, b);
     setPixel(9, r, g, b);
     setPixel(10, r, g, b);
     showStrip();
     delaynobbc;[/nobbc]
   }

if[nobbc] ([/nobbc]digitalReadnobbc == [/nobbc]HIGH[nobbc] ) {[/nobbc]

return;
   }
 }

}
[nobbc]/************************************************************/[/nobbc]
void[nobbc] RunningLightsAll([/nobbc]int[nobbc] WaveDelay) {[/nobbc]
 while[nobbc] (1) {[/nobbc]
   [nobbc]/* Triangle Green */[/nobbc]
   setPixel(0, 0x00, 0xff, 0x00);
   setPixel(1, 0x00, 0xff, 0x00);
   setPixel(2, 0x00, 0xff, 0x00);

[nobbc]/* Circle Red */[/nobbc]
   setPixel(3, 0xff, 0x00, 0x00);
   setPixel(4, 0xff, 0x00, 0x00);
   setPixel(5, 0xff, 0x00, 0x00);
   setPixel(6, 0xff, 0x00, 0x00);
   [nobbc]/* X Blue */[/nobbc]
   setPixel(7, 0x00, 0x00, 0xff);
   setPixel(8, 0x00, 0x00, 0xff);
   setPixel(9, 0x00, 0x00, 0xff);
   setPixel(10, 0x00, 0x00, 0xff);

[nobbc]/* Square Pink */[/nobbc]
   setPixel(11, 0xff, 0x00, 0xff);
   setPixel(12, 0xff, 0x00, 0xff);
   setPixel(13, 0xff, 0x00, 0xff);
   setPixel(14, 0xff, 0x00, 0xff);
   showStrip();
   delaynobbc;[/nobbc]

if[nobbc] ([/nobbc]digitalReadnobbc == [/nobbc]HIGH[nobbc] ) {[/nobbc]

return;
   }
 }
}
[nobbc]/***********************************************************************/[/nobbc]

[/td][/tr][/table]

playstation_code_phase.ino (7.86 KB)
```

|

Read Adafruit’s multitasking discussions 1, 2 and 3.


BTW

Use code tags.

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Thank you larryd , it seems i have quite a bit of reading to do. Will get back if any issues occur.
Also, thank you for the info on posting code. I thought i did CTRL T but I will recheck the next time I post ANY code.
Also, by 'complete' sketch, do you mean the code that I am having problems with/have questions about? And not the actual complete sketch?

We ask that when large sketches have a problem, you reduced it in size to the point where the problem still is seen.

Hello again everyone. Question, I am attempting to set up a button with 3 functions. OFF/ALL/PHASE. off and all are pretty self explanatory. For phase, I want the phase code to run forever until the button is pressed again. My issue arises with the fact that I don't know how to keep the "phase" code running without inserting a while(1) loop, which results in it being stuck forever in that state unless i restart the program. Should I use a timer interrupt for this or is there a better way to write my code for the PHASE.

void Phase(uint8_t interval) {
      ActivePattern = PHASE;
      Interval = interval;

      float r, g, b;

      /*********** PART 1 ************/


      // Square Turns OFF



While(1){

      // Triangle Turns ON
      for (int k = 0; k < 256; k = k + 1) {
        r = (k / 256.0) * 0x00;
        g = (k / 256.0) * 0xff;
        b = (k / 256.0) * 0x00;
        setPixelColor(0, r, g, b);
        setPixelColor(1, r, g, b);
        setPixelColor(2, r, g, b);
        show();

      }

      // Square Turns OFF
      for (int k = 255; k >= 0; k = k - 2) {
        r = (k / 256.0) * 0xff;
        g = (k / 256.0) * 0x00;
        b = (k / 256.0) * 0xff;
        setPixelColor(11, r, g, b);
        setPixelColor(12, r, g, b);
        setPixelColor(13, r, g, b);
        setPixelColor(14, r, g, b);
        show();

      }

      //Circle Turns ON
      for (int k = 0; k < 256; k = k + 1) {
        r = (k / 256.0) * 0xff;
        g = (k / 256.0) * 0x00;
        b = (k / 256.0) * 0x00;
        setPixelColor(3, r, g, b);
        setPixelColor(4, r, g, b);
        setPixelColor(5, r, g, b);
        setPixelColor(6, r, g, b);
        show();

      }

      /****  PART 2 ****************/
      // Triangle Turns OFF
      for (int k = 255; k >= 0; k = k - 2) {
        r = (k / 256.0) * 0x00;
        g = (k / 256.0) * 0xff;
        b = (k / 256.0) * 0x00;
        setPixelColor(0, r, g, b);
        setPixelColor(1, r, g, b);
        setPixelColor(2, r, g, b);
        show();

      }


      // X Turns ON
      for (int k = 0; k < 256; k = k + 1) {
        r = (k / 256.0) * 0x00;
        g = (k / 256.0) * 0x00;
        b = (k / 256.0) * 0xff;
        setPixelColor(7, r, g, b);
        setPixelColor(8, r, g, b);
        setPixelColor(9, r, g, b);
        setPixelColor(10, r, g, b);
        show();

      }
      /*********   PART 3 *********/

      // Circle Turns OFF
      for (int k = 255; k >= 0; k = k - 2) {
        r = (k / 256.0) * 0xff;
        g = (k / 256.0) * 0x00;
        b = (k / 256.0) * 0x00;
        setPixelColor(3, r, g, b);
        setPixelColor(4, r, g, b);
        setPixelColor(5, r, g, b);
        setPixelColor(6, r, g, b);
        show();

      }


      // Square Turns ON
      for (int k = 0; k < 256; k = k + 1) {
        r = (k / 256.0) * 0xff;
        g = (k / 256.0) * 0x00;
        b = (k / 256.0) * 0xff;
        setPixelColor(11, r, g, b);
        setPixelColor(12, r, g, b);
        setPixelColor(13, r, g, b);
        setPixelColor(14, r, g, b);
        show();

      }

      /*********   PART 4 *********/

      // X Turns OFF
      for (int k = 255; k >= 0; k = k - 2) {
        r = (k / 256.0) * 0x00;
        g = (k / 256.0) * 0x00;
        b = (k / 256.0) * 0xff;
        setPixelColor(7, r, g, b);
        setPixelColor(8, r, g, b);
        setPixelColor(9, r, g, b);
        setPixelColor(10, r, g, b);
        show();

      }
}
    }
NeoPatterns Stick(15, 7, NEO_GRB , &StickComplete);

// Initialize everything and prepare to start
void setup()
{
  Serial.begin(9600);

  pinMode(8, INPUT_PULLUP);

  // Initialize all the pixelStrips
  Stick.begin();
  // Kick off a pattern
  //Stick.Fade(Stick.Color(255,0,0),Stick.Color(0,0,255), 150,50);
  Stick.ColorAll();

}
long counter = 0;
// Main loop
void loop()
{

  // Update the rings.
  Stick.Update();


  // Switch patterns on a button press:

  if (digitalRead(8) == LOW && counter % 3 == 0) // Back to normal operation
  { counter++;
    Stick.ActivePattern = ALL;
    Stick.ColorAll();
    Stick.Update();

    Serial.println('counter');
    Serial.println("ALL");
  }
  if (digitalRead(8) == LOW && counter % 3 == 1) // Button #1 pressed
  { counter++;

    Stick.ActivePattern = PHASE;

    // Set stick to all red
    Stick.Phase(100);
    Stick.Update();

    Serial.println('counter');
    Serial.println("FADE");
  }
  if (digitalRead(8) == LOW && counter % 3 == 2) {
    counter++;
    Stick.ActivePattern = NONE;
    Stick.ColorSet(Stick.Color(0, 0, 0));
    Stick.Update();
    Serial.println('counter');
    Serial.println("OFF");

  }

}

I hope I posted my code right :frowning:

Also, by 'complete' sketch, do you mean the code that I am having problems with/have questions about? And not the actual complete sketch?

No all the sketch is needed. It is surprising how often the mistake is not in the place where the problems manifest themselves.

My issue arises with the fact that I don't know how to keep the "phase" code running without inserting a while(1) loop,

Simple you don’t have a while statement at all. Did you read those links?

You need to implement your code as a state machine, that is each animation function must do a small bit of the function and then return. So no while statements and no for loops, with show or delay in them and no delays at all.

If I give you some links will you read them?
Read
The blink without delay example in the Arduino IDE.
Read this link Short Range Wireless Communication - #7 by pito - Other Hardware Development - Arduino Forum

And read my link :- http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html