Okay need to take a step back to get it right.
To be specific, what i want my buttons to do are show different patterns on addressable leds - WS2812B. Ive been using the FastLED library before with great succes.
Until then, it all went well, and I found the Pushbutton library to have the functions i needed and a good debounce.
I also got the functions using the FastLED library to work, and the buttons to load them.
I the ran into the two crucial problems: that a press on a button stops the running function when pressed, and that the function activated by the button runs until another button is pressed. None of which happens.
This is my original code:
#include <Pushbutton.h> //include Pushbutton library
#include "FastLED.h" //include FastLED library, which drives the led-strip
#define NUM_LEDS 4 //number of leds on the strip
#define DATA_PIN 6 //led data pin
CRGB leds[NUM_LEDS]; // An array of leds
#define BUTTON1 2 //button1 pin
#define BUTTON2 3 //button2 pin
#define BUTTON3 4 //button3 pin
Pushbutton pbutton1(BUTTON1); //create button1
Pushbutton pbutton2(BUTTON2); //create button2
Pushbutton pbutton3(BUTTON3); //create button3
void setup() {
Serial.begin(9600); //initialise serial port
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); //set up the leds
}
void loop() {
if (pbutton1.getSingleDebouncedPress()){ //if button1 is pressed
Serial.println("Button1"); //serial monitor shows button1 pressed
Strobe(0xff, 0xff, 0xff, 10, 50, 1000); //execute Strobe function with parameters (1,2,3: color, 4: how many flashes, 5: flash delay, 6: end pause)
}
if (pbutton2.getSingleDebouncedPress()){ //if button2 is pressed
Serial.println("Button2"); //serial monitor shows button2 pressed
RGBLoop(); //execute RGBLoop function
}
if (pbutton3.getSingleDebouncedPress()){ //if button2 is pressed
Serial.println("Button3"); //serial monitor shows button3 pressed
//some function
}
}
//functions used by FastLED functions and library
void showStrip() {
FastLED.show();
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
//functions executed by the buttons
void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
for(int j = 0; j < StrobeCount; j++) {
setAll(red,green,blue);
showStrip();
delay(FlashDelay);
setAll(0,0,0);
showStrip();
delay(FlashDelay);
}
delay(EndPause);
}
void RGBLoop(){
for(int j = 0; j < 3; j++ ) {
// Fade IN
for(int k = 0; k < 256; k++) {
switch(j) {
case 0: setAll(k,0,0); break;
case 1: setAll(0,k,0); break;
case 2: setAll(0,0,k); break;
}
showStrip();
delay(3);
}
// Fade OUT
for(int k = 255; k >= 0; k--) {
switch(j) {
case 0: setAll(k,0,0); break;
case 1: setAll(0,k,0); break;
case 2: setAll(0,0,k); break;
}
showStrip();
delay(3);
}
}
}
So from what you said it's the delays in the functions, that gives me problems?
Also why doesn't the functions keep looping when called in loop?
Also i've been trying different state machines, but not been able to solve how to use multiple buttons. Maybe pressing one button will take you to specific states and pressing another button takes you to different states?