Looking for good tactile switch LED sketches

Hi,

Does anyone know of any good sketches for the arduino for Fastled or Neopixels with a tactile switch? I know there is one from Tweaking4all but I couldn't find any others. I'm still trying to learn how to understand the code, but in the meantime I'd love to experiment with sketches others have made.

Thanks!

you may be interested in this

// Keypad2
// recognize multiple button presses; tgl LED when pressed

byte butPins [] = { A1, A2, A3 };
byte ledPins [] = { 10, 11, 12 };

#define N_PINS sizeof(butPins)

byte butLst [N_PINS] = {};

// -----------------------------------------------------------------------------
void setup (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        digitalWrite (ledPins [n], OFF);
        pinMode      (ledPins [n], OUTPUT);

        pinMode      (butPins [n], INPUT_PULLUP);
        butLst [n]  = digitalRead (butPins [n]);
    }
}

// -----------------------------------------------------------------------------
void loop (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        byte but = digitalRead (butPins [n]);

        if (butLst [n] != but)  {
            butLst [n] = but;

            if (LOW == but)     // button pressed
                digitalWrite (ledPins [n], ! digitalRead (ledPins [n]));
        }
    }

    delay (10);         // debounce
}

Wow thanks! So just so I understand, is this declared in the beginning before void setup with the actual effect? For example like this code but after where I define the data pin?

For example your code added after #define PIN 6? Apologies again for my noob understanding.

#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6

void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

// *** REPLACE FROM HERE ***
void loop() {
  // ---> here we call the effect function <---
}

// ---> here we define the effect function <---
// *** REPLACE TO HERE ***

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

Combining the two sketches is the right idea, but does require some effort. First be sure you understand both sketches. Generally the way this will work, is that the key process and the pixel process run as 2 separate tasks in loop(). The key process sets variables that define the pixel action choices or controls. The pixel process just reads the variables and sets the pixels accordingly.

The sketch in reply #1 convolves the switches and LEDs. This won't work with animations, it's just designed as a simple demo. Instead of LEDs there, you can just be setting the variables I told you about.

Hi @aarg thanks for the clarification. So if I'm understanding correctly, I would have to understand where the pixel process is and include a button key process in there to tell the Arduino to toggle the next loop when pressed and released?

The provided program has no dynamic effects, it expects you to create them:

void loop() {
  // ---> here we call the effect function <---
}

// ---> here we define the effect function <---
// *** REPLACE TO HERE ***

…but, yes, once you have some effect in there, you would switch it on/off or condition it somehow by checking the variables that you set with the switches, e.g.

void loop() {
  readSwitches();
  if (rainbowEffectEnable == true)
    {
    // ---> here we call the effect function <---
    doRainbow();
    }
}

// ---> here we define the effect function <---
void doRainbow() {
...
// *** REPLACE TO HERE ***
}

void readSwitches() {
  if (rainbowStartKeyPressed ==  true)
    {
     rainbowEffectEnable = true;
    }
  if (rainbowStopKeyPressed ==  true)
    {
     rainbowEffectEnable = false;
    }
}

Important! A dynamic effect must be non-blocking if you expect the switches to respond. That means you can not spend more than a few milliseconds on each call. You get the job done by repeatedly calling it in loop(), and performing whatever actions you need to do only when they need to be done, otherwise you pass control to the next loop() process, which in your case is either reading switches, or running additional effects. You control effects timing with millis().

Thanks again. I think the logic is starting to make more sense. This is the best I could get with the code (I included the actual rainbow effect in the doRainbow function, but I kept getting a lot of errors of functions not being declared), however it now says “a function-definition is not allowed here before ‘{’ token.” I hope I didn’t mess it up too much.

#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6
#define Button 2

void 
  readSwitches(){
void loop() {
  readSwitches();
  if (rainbowEffectEnable == true)
    {
    // ---> here we call the effect function <---
    doRainbow();
    }
}
  }

// ---> here we define the effect function <---
void doRainbow() {
...
// *** REPLACE TO HERE ***
void loop() {
  rainbowCycle(20);
}

void rainbowCycle(int SpeedDelay) {
  byte *c;
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< NUM_LEDS; i++) {
      c=Wheel(((i * 256 / NUM_LEDS) + j) & 255);
      setPixel(i, *c, *(c+1), *(c+2));
    }
    showStrip();
    delay(SpeedDelay);
  }
}

byte * Wheel(byte WheelPos) {
  static byte c[3];
 
  if(WheelPos < 85) {
   c[0]=WheelPos * 3;
   c[1]=255 - WheelPos * 3;
   c[2]=0;
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   c[0]=255 - WheelPos * 3;
   c[1]=0;
   c[2]=WheelPos * 3;
  } else {
   WheelPos -= 170;
   c[0]=0;
   c[1]=WheelPos * 3;
   c[2]=255 - WheelPos * 3;
  }

  return c;
}
}
void 
  rainbowEffectEnable(){

void readSwitches() {
  if (rainbowStartKeyPressed ==  true)
    {
     rainbowEffectEnable = true;
    }
  if (rainbowStopKeyPressed ==  true)
    {
     rainbowEffectEnable = false;
    }
}
  }

You can’t define a function inside another function…

 void 
  readSwitches(){
void loop() {

Function definitions “void readSwitches () { … }” and “void loop() { … }” must be declared at the outermost level of your program and not inside any other functions.

Edit: you also really need to sort out your indentation. After every open brace { you should indent one tab. Every close brace } should outdent back to to column you starting in before the {.

There’s an auto format option in the IDE (Ctrl+T) to help you.

I said before, "Important! A dynamic effect must be non-blocking if you expect the switches to respond". You have placed time consuming code inside a for loop in your effects function. That is blocking code. You will never be able to read the switches properly if you do that. The switches are ignored while the for loop is running. You need to make non-blocking code. There are instructions for that in the sticky threads at the top of the forum.