Buttons and FastLED

Hey. Never posted before anything here before.
I have been messing around with FastLED and I am trying to make it so if you have a button a certain pattern is performed. If you press another button, then a different pattern is performed. ETC. I can get it to work with one button. But when I add the second button, nothing will work at all. Just the first few lights will strobe out uncontrollably. Any insight helps. Code is pasted below. As I said it works until I add the second button.

#include <FastLED.h>
#define NUM_LEDS 25
#define LED_PIN 6

CRGB leds[NUM_LEDS];


int buttonpress1 = 0;
int pattern1 = 0;

int buttonpress2 = 0;
int pattern2 = 0;

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN,GRB>(leds,NUM_LEDS);
  FastLED.setBrightness(25);
pinMode(2,INPUT);
}

void loop() {
buttonpress1 = digitalRead(2);
buttonpress2 = digitalRead(4);
if (buttonpress1 == HIGH){
  pattern1 = 1;
}
  if(pattern1 == 1){
  EVERY_N_MILLISECONDS(111) {
  //Creating a new pixel for led [0] (the first LED)
  //CHSV STRUCTURE
  // leds[i] = CHSV(Hue --> `colour`,saturation, value)
  leds[0] = CHSV(40, random8(), random8(100,225));

  //Copy each pixel to the next one, starting at last LED
  //This way the leds are "moving" along the entire strip
for (int i = NUM_LEDS - 1; i>0; i--){
  leds[i] = leds[i - 1];
}
FastLED.show();
}
}
if (buttonpress1 == LOW){
pattern1 = 0;
}
if(pattern1 ==0){
FastLED.clear(true);}


if (buttonpress2 == HIGH){
  pattern2 = 1;
}
  if(pattern2 == 1){
  EVERY_N_MILLISECONDS(100) {
  //Creating a new pixel for led [0] (the first LED)
  //CHSV STRUCTURE
  // leds[i] = CHSV(Hue --> `colour`,saturation, value)
  leds[0] = CHSV(90, random8(), random8(100,225));

  //Copy each pixel to the next one, starting at last LED
  //This way the leds are "moving" along the entire strip
for (int x = NUM_LEDS - 1; x>0; x--){
  leds[x] = leds[x - 1];
}
FastLED.show();
}
}
if (buttonpress2 == LOW){
pattern2 = 0;
}
if(pattern2 ==0){
FastLED.clear(true);}

}

How are your switches wired ?

I see

if(pattern1 ==0){
FastLED.clear(true);}

on one button and

if(pattern2 ==0){
FastLED.clear(true);}

on the other, I think they fighting each other?

Plus how are your switches wired? :expressionless:

BTW please try the Autoformat tool in the IDE. It's Tools/Auto Format and will put your code into one of the common formats that make it easier to read.

a7

So you are right in that they are fighting eachother. I removed the one "clear" just to see. If you have one clear function, then it will fight with all the other code in the other if statements.

Does anyone have any recommendations to have multiple buttons and the "patterns" are displayed while the button is being pressed and then the lights turn off when the button is released? For me right now, I can make the first part happen, but I can not have it turn off when the button is released. The lights just stay on.

I think that learning about "state change detection" would be just the ticket for you.

There's an examlpe in the IDE, and/or google

 arduino state change detection

Basically, you want to do things when the state of the button changes, not because the switch is in a state.

Or as put frequency here

act when the button becomes pressed, not when it is pressed.

So. Button one, when it gets pressed, sets mode 1. When it is released, turn off the lights.

Same same for button two. If button two becomes pressed while you still have button one down, the mode changes to mode 2.

That will make more sense when you have worked through the "state change detection" part of the learning curve.

a7