Simultaneous Button Control

Salutations! I have 2 separate buttons to control the different LED's for my project. When the toggleSwitch is ON, the WATT_LED does not work how I intend. In a nutshell, I want the LED matrix to light and 5 LED chase effect when the toggleSwitch is ON. When the pushButton is ON, I want the WATT_LED to blink how I have it programmed. So at the moment, when the toggleSwitch is on, the WATT_LED does not blink. When the toggleSwitch is off, it works. How do I get them both to work at the same time? Should I write one in a separate function? Here is the code:

#include <FastLED.h>
#define LED_PIN     8
#define NUM_LEDS    64
#define toggleSwitch 2
#define pushButton 9
#define WATT_LED 10
CRGB leds[NUM_LEDS];
int ledPins[] = {3,4,5,6,7};




void setup() {
 pinMode (toggleSwitch, INPUT);
  int index;
  for (index = 0; index <= 5; index++){
  pinMode(ledPins, OUTPUT);
  }
 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); 
    pinMode(WATT_LED, OUTPUT);
  pinMode(pushButton, INPUT);
 

}

void loop() {
  int switchState = digitalRead(toggleSwitch);
  int buttonStatus = digitalRead (pushButton);
if (buttonStatus == HIGH) {
    digitalWrite (WATT_LED, HIGH);
    delay(250);
    digitalWrite (WATT_LED, LOW);
    delay (250);
   }
   else {
    digitalWrite(WATT_LED, LOW);
    delay(0);
   }
  
if (switchState == HIGH)  {
    digitalWrite(ledPins[0], HIGH);
    delay(100);
    digitalWrite(ledPins[1], HIGH);
    delay(100);
    digitalWrite(ledPins[2], HIGH);
    delay(100);
    digitalWrite(ledPins[3], HIGH);
    delay(100);
    digitalWrite(ledPins[4], HIGH);
    delay(100);
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);
    delay(250);
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[4], HIGH);
}
  if (switchState == HIGH){
  for (int i = 0; i<64; i++) {
    leds [i] = CRGB (0,0,255);
    FastLED.show();
    delay (100);
  
  }
  
  }
  else {
    for (int i = 0; i<64; i++){
      leds[i] = CRGB (0,0,0);
      FastLED.show();
      delay(0);
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);
       
    
    }
  }
}/code]

alright, I am doing a little research, I think the problem is I am using delay. trying to figure out how to modify the code in order to use millis. Thoughts and/or suggestions? As always, thank you in advance for the help.

thanks for the link! now, how do I now replace the delay with millis in the code I currently have? this is my first project from scratch, so I appreciate you bearing with me

oh I read the link, I guess I was just wondering if I needed to start again from scratch. experimenting now, if I hit a roadblock(which I most likely will) I will post again

reading it and have it up. again, I am still a beginner. if someone can help me with a code example as to how to replace what I have with millis instead of delay, I would greatly appreciate it. not sure how to accomplish the same thing as far as the FastLED

I appreciate your input. starting to get the hang of it looking through all of the links. I may need help with the fastLED, but here is what I go so far, and it works, so going to take it from there.

int ledPins[] = {3,4,5,6,7};

int ledDelay(100);
int leddirection = 1;
int ledcurrentLED = 0;
unsigned long changeTime;

void setup() {
   int index;
  for (index = 0; index <= 5; index++){
  pinMode(ledPins, OUTPUT);
  changeTime = millis();
  }
}

void loop() {
  if ((millis() - changeTime) > ledDelay) { changeLED(); 
  changeTime = millis();
  }

}

void changeLED() {
  for (int x=0; x<5; x++) {
digitalWrite(ledPins[x], HIGH);
}
 
digitalWrite(ledPins[ledcurrentLED], LOW);           
ledcurrentLED += leddirection; 
 

if (ledcurrentLED == 4) {leddirection = -1;}
if (ledcurrentLED == 0) {leddirection = 1;}
 
}/code]

unfortunatly you will have to rewrite.

the delay function is kind of worthless for any program that will do more than one thing at a time.

when you call delay it basically freezes your entire thread and prevents anything else from happening.
this includes stopping pins from updating and not allowing buttons to be read.

so if you ever intend to do more than make a light blink...

always use a millis() timer.

I would recommend setting up a function that's called in a nice even time frame, then you can countdown variables within that function for timers to control the duration of things.

also using the millis() method wont stop your main loop section so you can still detect thing like buttons and swiches on a constant basis.

after you get used to this method it makes things easier and cleaner and you can code what you are trying to do in a lot less lines.

i posted before i seen you last post. I think you got the right idea!

yes, I am finding that out about the delay function the hard way unfortunately. looking up how to use millis to control my ws2812b matrix, but as far as the chase and blink effect, I think I have that under control. any suggestions as far as the matrix tho? thank you SO much

I gave you the ingredients for a millis() based timing of the strip already in Controlling multiple leds with less code - Programming Questions - Arduino Forum (post #6).

You will also need to understand how things work; from your opening post

    for (int i = 0; i < 64; i++) {

leds[i] = CRGB (0, 0, 0);
      FastLED.show();
      delay(0);
      digitalWrite(ledPins[0], LOW);
      digitalWrite(ledPins[1], LOW);
      digitalWrite(ledPins[2], LOW);
      digitalWrite(ledPins[3], LOW);
      digitalWrite(ledPins[4], LOW);

}

Why would you set the 5 led pins 64 times low in that for-loop? You only need to do that once outside the for-loop.
2)
Why would you update the led-strip 64 times in that for-loop? It would make sense if you want some form of delay but it does not make sense with a delay of zero.
The normal approach is to set the leds in the strip that you want to set (in this case all 64) and once the values are set, you call the show() method.

So, I wanted to control the LED matrix and the 5 leds with the same button, so that is what I was trying to do with that code. So with the code that I have, the leds go on, but with the led matrix added to it, the LED chase effect is now going slower. Does that make sense? Here is the code:

#include <FastLED.h>
#define LED_PIN     8
#define NUM_LEDS    64
#define pushButton 2
CRGB leds[NUM_LEDS];
int ledPins[] = {3,4,5,6,7};
int ledDelay(100);
int leddirection = 1;
int ledcurrentLED = 0;
unsigned long changeTime;

void setup() {
  int index;
  for (index = 0; index <= 5; index++){
  pinMode(ledPins, OUTPUT);
  }
  pinMode (pushButton, INPUT);
  changeTime = millis();
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
  int buttonStatus = digitalRead (pushButton);
  if ((buttonStatus == HIGH) && (millis() - changeTime) > ledDelay) {
     LEDmatrix();
    changeTime = millis();
  }
  if (buttonStatus == LOW) {
   for (int i = 0; i<64; i++){
      leds[i] = CRGB (0,0,0);
      FastLED.show();
 }
  }
   if ((buttonStatus == HIGH) && (millis() - changeTime) > ledDelay) { changeLED(); 
  changeTime = millis();
  }
if (buttonStatus == LOW) {
  digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);
}
}

void changeLED() {
  for (int x=0; x<5; x++) {
digitalWrite(ledPins[x], HIGH);
}
 
digitalWrite(ledPins[ledcurrentLED], LOW);           
ledcurrentLED += leddirection; 
 

if (ledcurrentLED == 4) {leddirection = -1;}
if (ledcurrentLED == 0) {leddirection = 1;}
 
}
  

void LEDmatrix (){
  for (int i = 0; i<64; i++) {
    leds [i] = CRGB (0,0,255);
    FastLED.show();
  }
}
/code]

This is not how you set a ledDelay

int ledDelay(100);

Change that to

int ledDelay = 100;

Next think about your code

void loop() {
  int buttonStatus = digitalRead (pushButton);
  if ((buttonStatus == HIGH) && (millis() - changeTime) > ledDelay) {
    LEDmatrix();
    changeTime = millis();
  }

So if the conditions are satisfied, you update changeTime.

Next, a few lines later

  if ((buttonStatus == HIGH) && (millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
  }

Same condition; do you think that changeTime will have changed that much that the condition will be satisfied?

You will need two changeTime variables, e.g. matrixChangeTime and ledChangeTime.

Lastly, as tried to explain earlier

void LEDmatrix () {
  for (int i = 0; i < 64; i++) {
    leds [i] = CRGB (0, 0, 255);
    FastLED.show();
  }
}

The above will update the strip 64 times in a row. Why would you do that?

leds [i] = CRGB (0, 0, 255);

The above will set the value for one led, next you send the data for all leds to the strip with belowFastLED.show();

The below makes makes more sense; first set the values for all leds and next send it to the strip

void LEDmatrix () {
  // set the values for all leds
  for (int i = 0; i < 64; i++) {
    leds [i] = CRGB (0, 0, 255);
  }
  // send to the strip
  FastLED.show();
}

sterretje, looking at that post you sited now, thank you! I will try and apply that on my matrix. I have cleaned up the code a bit using millis and got it to work better for the project. after I am done with this project I plan on playing with all of this some more, it has tickled my curiosity. here is my code now:

#include <FastLED.h>
#define LED_PIN     8
#define NUM_LEDS    64
#define toggleSwitch 2
#define pushButton 9
#define WATT_LED 10
bool blinking = false;
unsigned long blinkInterval = 100;
CRGB leds[NUM_LEDS];
int ledPins[] = {3,4,5,6,7};
const long ledDelay1 = 100;
const long ledDelay2 = 100;
int leddirection = 1;
int ledcurrentLED = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;

void setup() {
  int index;
  for (index = 0; index <= 5; index++){
  pinMode(ledPins, OUTPUT);
  }
  pinMode (pushButton, INPUT);
  pinMode (toggleSwitch, INPUT);
  pinMode (WATT_LED, OUTPUT);
 
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
  unsigned long currentMillis = millis();
  int buttonStatus = digitalRead (toggleSwitch);
  int switchState = digitalRead(pushButton);
if (blinking) {
  currentMillis = millis(); 
  if ((unsigned long)(currentMillis - previousMillis3) >= blinkInterval) { 
   digitalWrite(WATT_LED, !digitalRead(WATT_LED)); 
   previousMillis3 = currentMillis; 
  }
 } else {
  digitalWrite(WATT_LED, LOW); 
 int reading = digitalRead(pushButton);
 delay(50); // crude de-bouncing
 
 if (reading==HIGH) 
  blinking=true; 
 else
  blinking=false; 
  
  if ((buttonStatus == HIGH) && (currentMillis - previousMillis1) > ledDelay1) {
     LEDmatrix();
    
  }

  if (buttonStatus == LOW) {
   for (int i = 0; i<64; i++){
      leds[i] = CRGB (0,0,0);
      FastLED.show();
 }
  }
   if ((buttonStatus == HIGH) && (currentMillis - previousMillis2) > ledDelay2) { 
    changeLED(); 
  
  }
if (buttonStatus == LOW) {
  digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);
}

}

void changeLED() {
  for (int x=0; x<5; x++) {
digitalWrite(ledPins[x], HIGH);
}
 
digitalWrite(ledPins[ledcurrentLED], LOW);           
ledcurrentLED += leddirection; 
 

if (ledcurrentLED == 4) {leddirection = -1;}
if (ledcurrentLED == 0) {leddirection = 1;}
 
}
  

void LEDmatrix (){
  for (int i = 0; i<64; i++) {
    leds [i] = CRGB (0,0,255);
    FastLED.show();
  }
}/code]

sterretje, I posted the last post before I read your previous post, just so you don't think I wasn't listening. for the record I very much appreciate all of your input and you bearing with the novice!