ChainableLED functions



I’m working on a new project which uses the Grove_ChainableLED library and noticed that the library doesn’t have any “easy/handy” methods for simply turning the LED on or off. If I call the setColorHSB() function with values it will turn on. What would be the correct way to turn the light off…digitalWrite()?

This is my code so far, but the turnOn() turnOff() functions aren’t working:

#include "clickButton.h"
#include <Grove_ChainableLED.h>

#define BUTTON_PIN D2
#define NUM_LEDS 1

int device_state = 0;               // initial device state is off
int button = D0;                    // button is connected to D0
int LED = D4;                       // LED is connected to D4
double hue = 0.5;                   // default hue
double saturation = 1.0;            // default saturation
double brightness = 0.5;            // default brightness
int buttonFunction = 0;             // button results

// the LED
ChainableLED leds(D4, D5, NUM_LEDS);
// the Button
ClickButton theButton(BUTTON_PIN, HIGH);

void setup()

  //cloud variables
  Particle.variable("state", device_state);
  Particle.variable("hue", hue);
  Particle.variable("saturation", saturation);
  Particle.variable("brightness", brightness);
  //cloud functions
  Particle.function("turnOn", turnOn);
  Particle.function("turnOff", turnOff);
  Particle.function("setColor", setColor);
  Particle.function("setBrightness", setBrightness);
  pinMode(LED, OUTPUT);                 // sets pin as output
  pinMode(button, INPUT_PULLDOWN);      // sets pin as input

  // Setup button timers (all in milliseconds / ms)
  // (These are default if not set, but changeable for convenience)
  theButton.debounceTime   = BUTTON_DEBOUNCE_TIME;   // Debounce timer in ms
  theButton.multiclickTime = BUTTON_MULTICLICK_TIME;  // Time limit for multi clicks
  theButton.longClickTime  = BUTTOM_LONGCLICK_TIME; // time until "held-down clicks" register

void loop()
  // Update button state
   // Save click codes in LEDfunction, as click codes are reset at next Update()
  if(theButton.clicks != 0) buttonFunction = theButton.clicks;
  if(buttonFunction == 1) {
      if(device_state == 0){
           //turn the light on
          device_state = 1;
          Particle.publish("EVENT: SINGLE CLICK ON", String(device_state));
          //turn the light off
          device_state = 1;
          Particle.publish("EVENT: SINGLE CLICK OFF", String(device_state));

  if(buttonFunction == 2) Particle.publish("EVENT: DOUBLE CLICK");

  if(buttonFunction == 3) Particle.publish("EVENT: TRIPLE CLICK");

  if(buttonFunction == -1) Particle.publish("EVENT: SINGLE LONG CLICK");

  if(buttonFunction == -2) Particle.publish("EVENT: DOUBLE LONG CLICK");

  if(buttonFunction == -3) Particle.publish("EVENT: TRIPLE LONG CLICK");
  buttonFunction = 0;

int turnOn(String args){
    device_state = 1;
    digitalWrite(LED, HIGH);
    Particle.publish("EVENT: ON");
    return 1;

int turnOff(String args){
    device_state = 0;
    digitalWrite(LED, LOW);
    Particle.publish("EVENT: OFF");
    return 1;

int setColor(String args){
    hue = args.toFloat();
    leds.setColorHSB(0, hue, saturation, brightness);
    return 1;

int setBrightness(String args){
    brightness = args.toFloat();
    leds.setColorHSB(0, hue, saturation, brightness);
    return 1;

Note: I know this code can likely be cleaned up to simplify some of the functions.



Since the LEDs are chainable you cannot just turn the communication pin on or off.
These LEDs are expecting a specific bit stream to act upon.
Consequently you need to set the the “value” of the LED via a function that addresses the specific LED and tells it to turn off - either by setting the brightness to zero or the RGB value to 0,0,0.

If you had the supply voltage of the LED(s) conneted to a GPIO (via a transistor to provide the required current) you could switch all of the LEDs off at once, but for swtiching them back on, you’d still need to inform each individual LED about the colour it ought to emit.

BTW, browsing the implementation of provided libraries is a good practice to learn how things work.