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)
```
|