Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




LED Effect - push O...
 
Share:
Notifications
Clear all

LED Effect - push ON and OFF

2 Posts
1 Users
0 Likes
1,586 Views
 Hans
(@hans)
Famed Member Admin
Joined: 10 years ago
Posts: 2548
Topic starter  

From one of the comments under one of the LED Effect articles.


 

what is matter with this cod 

I want to do the strip show wen I push the Butten an then end the strip show 

who can help me:(((

 

#include “FastLED.h”
#define NUM_LEDS 11
CRGB leds[NUM_LEDS];
#define PIN D9
#define buttonPin D2

int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

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

void loop() {
buttonState = digitalRead(buttonPin);
 // compare the buttonState to its previous state
if (buttonState != lastButtonState) {
  // if the state has changed, increment the counter
  if (buttonState == HIGH) {
    // if the current state is HIGH then the button went from off to on:
    buttonPushCounter++;
    Serial.println(“on”);
    Serial.print(“number of button pushes: “);
    Serial.println(buttonPushCounter);
  } else {
    // if the current state is LOW then the button went from on to off:
    Serial.println(“off”);
  }
  // Delay a little bit to avoid bouncing
  delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}

  colorWipe(0xff,0x00,0x00, 50);
colorWipe(0x00,0x00,0x00, 50);
 }

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
for(int i=NUM_LEDS; i>=0; i– ) {
  if (buttonPushCounter % 4 == 0){
    setPixel(i, red, green, blue);
    showStrip();
    delay(SpeedDelay);
}
}

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
}

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 10 years ago
Posts: 2548
Topic starter  

Since you're using FastLED, I'd focus on that an remove irrelevant code from AdaFruit (like setPixel and shwoStrip).
This will shorten the code a little.

I've modified your code a little but I have not been able to test the code (don't have my Arduino near me).

Hope this helps

#include “FastLED.h”
#define NUM_LEDS 11
CRGB leds[NUM_LEDS];
#define PIN D9
#define buttonPin D2

int buttonPushCounter = 0; // counter for the number of button presses
bool effectON = false; // current state of the button

void setup()
{
  FastLED.addLeds<ws2811, pin,="" grb="">(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  pinMode(buttonPin, INPUT);
}

void loop() {
  bool buttonPressed = ( digitalRead(buttonPin) == HIGH );
  
  // Button pressed 
  if(ButtonPressed) {
    
    // flip effect ON/OFF    
    effectON = !effectON; // flip the effect
  
    // output the effect of pressing the button  
    if(effectON) {
      Serial.println("switching ON");
    } else {
      Serial.println("switching OFF");
    }
    
    // display button pushes
    buttonPushCounter++;
    Serial.print("number of button pushes: ");
    Serial.println(buttonPushCounter);
  }
    
  // Delay a little bit to avoid bouncing
  delay(50);
  
  // if Effect should be shown: do it now
  if(effectON) {
    colorWipe(0xff,0x00,0x00, 50);
    colorWipe(0x00,0x00,0x00, 50); // not sure how effective a black swipe would be?
  }
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(int i=NUM_LEDS; i>=0; i– ) {
    if (buttonPushCounter % 4 == 0){
      leds[i] = CRGB(red, green, blue);
      FastLED.show();
      delay(SpeedDelay);
  }
}</ws2811,>

   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: