Using Blynk with FastLED sketch and Blynk Timer

(Reposting this from the FastLED forum because I’m not getting much traction there)

I am struggling to understand how to get Blynk to work with one of the FastLED example ColorPalette? What I would like to do is to replace ChangePalettePeriodically(); with Blynk buttons instead so that I can control the mode that it is currently in. I understand how to read the state of my Virtual Pin on my Huzzah Feather by the following:

BLYNK_WRITE(V14)
{
  int RandomColorspin = param.asInt();   // assigning  value pin V14 
if(RandomColorspin == 1) {
      SetupTotallyRandomPalette(); 
      currentBlending = LINEARBLEND;
      Serial.println("RandomColorspin Mode ON");
  }
}

I have verified that Blynk is successfully controlling my Arduino Feather using the Serial monitor.

But Blynk warns users not to put anything into the void loop that would cause delay and the current example ColorPalette does this. Could someone help me adopt the ColorPalette example to be operated by Blynk. I think it will probably involve using the BlynkTimer.

My current sketch is here:

#define BLYNK_PRINT Serial // Defines the object that is used for printing

#include "FastLED.h"  //include FastLED library
#define DATA_PIN 3
#define CLOCK_PIN 1
#define NUM_LEDS 9   // Number of P9813 boards 
#define BRIGHTNESS  128
#define ButtonA 0
#define UPDATES_PER_SECOND 100
CRGB leds[NUM_LEDS];

CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

//---------------------------------------------------------------
#include <BlynkSimpleEsp8266.h>

// Blynk App. Auth Token goes here:
char auth[] = "*******************"; //Removed from sketch for security reasons
char ssid[] = "******"; //Removed from sketch for security reasons
char pass[] = "*****"; //Removed from sketch for security reasons



//---------------------------------------------------------------
void setup()
{
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.clear();  //blank out all pixel data
}

BLYNK_WRITE(V2)
{
  int val = param.asInt();           //  Blynk Random button on
  if (val == 1) {
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;
    Serial.print("Random Color Button is ON ");
  }
  else
  FastLED.clear();
}

BLYNK_WRITE(V3)
{
  int val = param.asInt();           //  Blynk Purple and Green Stripes button on
  if (val == 1) {
    SetupPurpleAndGreenPalette();
      currentBlending = LINEARBLEND;
    Serial.print("Purple and Green Stripes is ON ");
  }
  else
  FastLED.clear();
}

// will assign more buttons in Blynk once I get the above two buttons workings

void loop()
{
  // ChangePalettePeriodically();

  static uint8_t startIndex = 0;
  startIndex = startIndex + 1; /* motion speed */

  FillLEDsFromPaletteColors( startIndex);

  FastLED.show();
  FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
  uint8_t brightness = 255;

  for ( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    colorIndex += 3;
  }
}


// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.  All are shown here.

void ChangePalettePeriodically()
{
  uint8_t secondHand = (millis() / 1000) % 60;
  static uint8_t lastSecond = 99;

  if ( lastSecond != secondHand) {
    lastSecond = secondHand;
    if ( secondHand ==  0)  {
      currentPalette = RainbowColors_p;
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 10)  {
      currentPalette = RainbowStripeColors_p;
      currentBlending = NOBLEND;
    }
    if ( secondHand == 15)  {
      currentPalette = RainbowStripeColors_p;
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 20)  {
      SetupPurpleAndGreenPalette();
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 25)  {
      SetupTotallyRandomPalette();
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 30)  {
      SetupBlackAndWhiteStripedPalette();
      currentBlending = NOBLEND;
    }
    if ( secondHand == 35)  {
      SetupBlackAndWhiteStripedPalette();
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 40)  {
      currentPalette = CloudColors_p;
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 45)  {
      currentPalette = PartyColors_p;
      currentBlending = LINEARBLEND;
    }
    if ( secondHand == 50)  {
      currentPalette = myRedWhiteBluePalette_p;
      currentBlending = NOBLEND;
    }
    if ( secondHand == 55)  {
      currentPalette = myRedWhiteBluePalette_p;
      currentBlending = LINEARBLEND;
    }
  }
}

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
  for ( int i = 0; i < 16; i++) {
    currentPalette[i] = CHSV( random8(), 255, random8());
  }
}

// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
  // 'black out' all 16 palette entries...
  fill_solid( currentPalette, 16, CRGB::Black);
  // and set every fourth one to white.
  currentPalette[0] = CRGB::White;
  currentPalette[4] = CRGB::White;
  currentPalette[8] = CRGB::White;
  currentPalette[12] = CRGB::White;

}

// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
  CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  CRGB green  = CHSV( HUE_GREEN, 255, 255);
  CRGB black  = CRGB::Black;

  currentPalette = CRGBPalette16(
                     green,  green,  black,  black,
                     purple, purple, black,  black,
                     green,  green,  black,  black,
                     purple, purple, black,  black );
}


// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM.  A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
  CRGB::Red,
  CRGB::Gray, // 'white' is too bright compared to red and blue
  CRGB::Blue,
  CRGB::Black,

  CRGB::Red,
  CRGB::Gray,
  CRGB::Blue,
  CRGB::Black,

  CRGB::Red,
  CRGB::Red,
  CRGB::Gray,
  CRGB::Gray,
  CRGB::Blue,
  CRGB::Blue,
  CRGB::Black,
  CRGB::Black
};

Current video of my light project here:

https://app.box.com/s/quo4p9dzg3hll5mk74z5cs4rcbqm4mii

When formatting code on this site, you type the backtick (```)button 3 times, then the letters “cpp”. Then click enter and paste your sketch, then enter again and type 3 more backticks. I’ll use apostrophes instead of backticks in an example;

‘’‘cpp
PASTE CODE HERE
‘’’

I am unable to open your link, that’s why I ask you to paste your sketch.

Apologize for that - edited my original post.

M

#include <Arduino.h>
#include <BlynkSimpleEsp8266.h>

// Declare void (probably not needed in Arduino IDE)
void timerThingie();
// Init timer
BlynkTimer timer1;
// Create timer
int timedStuff = timer1.setInterval(1000, timerThingie);

// This void now will run every 1s (1000 ms) without blocking code
void timerThingie()
{
 // ChangePalettePeriodically();
 // Commented out for clarity
}

void setup()
{
  // Disable timer
  timer1.disable(timedStuff);
}

BLYNK_WRITE(V1)
{
  int mode = param.asInt();
  if(mode)
    // Enable timer
    timer1.enable(timedStuff);
  }
}

void loop()
{
  // Run Blynk
  Blynk.run();
  // Run timer
  timer1.run();
}

Quick ‘n’ dirty, but it illustrates how to use, enable and disable the running timer.

2 Likes

@Lichtsignaal

Thanks will study and incorporate to see if I can get the rest to work.