jandelgado / jled

Non-blocking LED controlling library for Arduino and friends.
MIT License
331 stars 55 forks source link

How to switch LED's current effect inside loop() #64

Closed StarterCraft closed 3 years ago

StarterCraft commented 3 years ago

If I try writing code without dynamic LED effect switching like it's written below, it works normally:

#include "jled.h"
// do the parentheses actually break the library import?

#define myLED 13
JLed led(myLED);
//JLed led = JLed(myLed).On() - this does work too, but I don't see the difference

void setup() {
    led.On();
}

void loop() {
    led.Update();
}

But if I want to switch the LED's effect at the runtime according to a condition, nothing happens, the LED just ignores the instructions:

#include "jled.h"

JLed led = JLed(13).On().DelayAfter(2000);
bool _st;

void setup() {}

void loop() {
  if (someCondition) _st = true; //for example, switching the effect using a button
  if (_st) led.Blink(360, 360);
  led.Update();
}

How do I get rid of this problem?

jandelgado commented 3 years ago

You need make sure only to call led.Blink(360,360)once. In your example, it will be called over and over again, resetting the effect again and again. In your code e.g. you could do:

bool _st = false;

void loop() {
  if (someCondition && !_st) { 
      _st = true; 
      led.Blink(360, 360).Forever();
  }
  led.Update();
}
StarterCraft commented 3 years ago

Thank you! It works! :>