Salutations! Hoping this will be a quick simple answer. I am controlling a WS2812B using the FastLED library, and I am trying to add a push button. Here is the code I have so far, how to I add a push button and still have the same effect:
#include <FastLED.h>
#define LED_PIN 9
#define NUM_LEDS 64
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
for (uint8_t ledCnt = 0; ledCnt < NUM_LEDS; ledCnt++)
{
switch (ledCnt)
{
case 3:
leds[ledCnt] = CRGB(150, 0, 255);
break;
case 10:
leds[ledCnt] = CRGB(150, 0, 255);
break;
default:
leds[ledCnt] = CRGB(0, 0, 255);
}
FastLED.show();
delay(100);
}
}/code]
Experimenting, and I got it to work, however now curious, would it be practical in the future to perhaps use a boolean? and is there a way for make them all go off at the same time, not a chase effect?
#include <FastLED.h>
#define LED_PIN 9
#define NUM_LEDS 64
int toggleSwitch = 2;
CRGB leds[NUM_LEDS];
void setup() {
pinMode (toggleSwitch, INPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
toggleSwitch = digitalRead(2);
if (toggleSwitch = HIGH) {
for (int i = 0; i<64; i++) {
leds = CRGB (0,0,255);
- FastLED.show();*
- delay (100);*
- }*
- }*
- else {*
- for (int i = 0; i<64; i++){*
_ leds = CRGB (0,0,0);_
* FastLED.show();*
* delay(0);*
* }*
* }*
}
Why didn't you use code tags in your second post?
You also seem to have forgotten everything that I demonstrated in Controlling multiple leds with less code - Programming Questions - Arduino Forum
Your code has some mistakes
int toggleSwitch = 2;
is supposed to declare the pin of the switch. You should not assign a value to it later on as in below
toggleSwitch = digitalRead(2);
What saved you is the fact that you used a hard-coded pin number in the digitalRead.
Better would be
#include <FastLED.h>
#define LED_PIN 9
#define NUM_LEDS 64
#define toggleSwitch 2
CRGB leds[NUM_LEDS];
or
#include <FastLED.h>
const byte LED_PIN = 9;
#define NUM_LEDS 64
const byte toggleSwitch = 2;
CRGB leds[NUM_LEDS];
I further suggest that you put all pins together; easier to find
#include <FastLED.h>
// pins
const byte LED_PIN = 9;
const byte toggleSwitch = 2;
#define NUM_LEDS 64
CRGB leds[NUM_LEDS];
Using const will cause the compiler to throw an error when you try to assign a new value to the variable.
loop() will then change
void loop()
{
int buttonStatus = digitalRead(toggleSwitch);
if (buttonStatus == HIGH) {
...
...
}
Further
if (toggleSwitch = HIGH) {
A '=' is an assignment, a '==' is a compare (see example above).
Thanks for the suggestions. I have modified the code shown below, however the push button function is not working. however, it may be how I have it wired.
#include <FastLED.h>
#define LED_PIN 8
#define NUM_LEDS 64
#define toggleSwitch 4
CRGB leds[NUM_LEDS];
void setup() {
pinMode (toggleSwitch, INPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
int buttonStatus = digitalRead(toggleSwitch);
if (buttonStatus = 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);
}
}
}/code]
Yog1Girl:
if (buttonStatus = HIGH) {
See reply #2
Yog1Girl:
however, it may be how I have it wired.
Don't you think that it would be useful to tell us how you wired it?
that was my bad I misread your previous coding selection and added a '==' in that spot. and it is successful. thank you again for the help!