Blynk timer with same frequency

Hello,

I got into some trouble with the Blink timer. I hooked up some Ws2812B to my Esp8266 and I want to control them with Blynk. I am using FastLED for my project as I am familiar with the functions.

So this is my Code:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "";
char pass[] = "";

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <FastLED.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

#define LED_PIN 14
#define NUM_LEDS 16

CRGB leds[NUM_LEDS];

uint8_t check0, check1;

void setup()
{
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(115200);
  FastLED.setBrightness(50);
  
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(33, purpleBlink);
  timer.setInterval(33, blueBlink);
}

CRGBPalette16 purplePalette = CRGBPalette16 (
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    
    CRGB::Magenta,
    CRGB::Magenta,
    CRGB::Linen,
    CRGB::Linen,
    
    CRGB::Magenta,
    CRGB::Magenta,
    CRGB::DarkViolet,
    CRGB::DarkViolet,

    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::Linen,
    CRGB::Linen
);

CRGBPalette16 bluePalette = CRGBPalette16 (
    CRGB::Red,
    CRGB::Green,
    CRGB::Blue
);
void loop()
{
  Blynk.run();
  timer.run();
}

BLYNK_WRITE(V0){
  check0 = param.asInt();
}

BLYNK_WRITE(V2){
  check1 = param.asInt();
}



void purpleBlink(){
  if(check0 == 1){
    EVERY_N_MILLISECONDS(50){
      leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(purplePalette, random8(), 255, LINEARBLEND);
      fadeToBlackBy(leds, NUM_LEDS, 125);
    }
    FastLED.show();
  }else{
    FastLED.clear();
    FastLED.show();
  }
}

void blueBlink(){
  if(check1 == 1){
    EVERY_N_MILLISECONDS(50){
      leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(bluePalette, random8(), 255, LINEARBLEND);
      fadeToBlackBy(leds, NUM_LEDS, 125);
    }
    FastLED.show();
  }else{
    FastLED.clear();
    FastLED.show();
  }
}

First of all. When I call function purpleBlink it blinks like crazy as if my time Intervall is 1ms. The blueBlink is very similar but at another brightness level… I don’t know why but that doesn’t matter at the moment.

I tried something that confuses me even more. When I just call one timer like only timer.setInterval(33, purpleBlink); it works like it supposed to do.

I’ve read that you can call multiple timers with just one timer. Is that right?
And is there an issue when I want to have the same frequency twice? (Like 33ms for purpleBlink and blueBlink)

I made 2 videos where you can see the LED strip
“1.gif” is how it is supposed to be “2.gif” is my error

Hope I expressed precisely enough.
Hope to hear from you guys. Thanks

J

It’s always a better idea to stage timers, so that the timer has enough room to fit in the function and complete the cycle.

Something like
33, purpleblink
55, blueblink
77, orangeblink
Etc etc.

If this doesn’t serve your purpose, then create another instance of the timer n see if that solves the problem. One Blynk timer can run 16 instances.

You should read this, especially the “Staggering Timers” section…

Pete.

1 Like

hey,

i came up with this Idea which works for me.

  if(check0 == 1){
    EVERY_N_MILLISECONDS(50){
      leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(purplePalette, random8(), 255, LINEARBLEND);
      fadeToBlackBy(leds, NUM_LEDS, 125);
    }
    FastLED.show();
  }else if(check1 == 1){
    EVERY_N_MILLISECONDS(50){
      leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(bluePalette, random8(), 255, LINEARBLEND);
      fadeToBlackBy(leds, NUM_LEDS, 125);
    }
    FastLED.show();
  }else{
    FastLED.clear();
    FastLED.show();
  }
}

I just put both functions in one function. I don’t know if there is a problem but it works fine right now. Thank you for the link. Helped a lot to understand timers a little more.

J

1 Like