Looking for help parsing a function to RGB [Solved]

Hello,

I have from time to time dropped in and have learned quite a bit from this forum. @juano2310 was my first inspiration for some of the integrations I have done.

I have integrated the Photon/Core with Smartthings and created a fully functioning window blind running directly off of the photon.

The integration I am looking for some assistance with is an RGB integration with smartthings and Particle Photon. I can get the RGB to turn on and can set it to specific colors, but I am looking to integrate a color wheel on Smartthings and to do so I have to parse RGB in the Photon. This is where I am struggling. I can pass R,G,B as (255,255,255) but I can’t seem to parse this from the function into individual ints. Here is my code so far, any help is appreciated:

/*
 * hook up your pixels and all of the pixel types that are supported.
 *
 */

#include "application.h"
#include "neopixel.h"
#include "math.h"

SYSTEM_MODE(AUTOMATIC);

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 88
#define PIXEL_TYPE WS2812B
#define brightness



Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);



void setup(){
    
    RGB.control(true);
    RGB.color(0, 0, 0);
    strip.begin();
    strip.show();
    Particle.function("led", led);
    Particle.variable("checkStatus1", "off");

   
}

void loop()
{
}


int led(String command){

if(command == "0"){
        colorWipe(strip.Color(0,0,0), 50);
         Particle.variable("checkStatus1", "off");
    }
    
    else if(command == "1"){
        colorWipe(strip.Color(255, 180, 40), 50);
        Particle.variable("checkStatus1", "on");
    }
    
    else if(command == "2"){
        colorWipe(strip.Color(255,0,0), 50);
        Particle.variable("checkStatus1", "on");
            
    }  
    else if(command == "3") {
        colorWipe(strip.Color(120,80,20), 50);
        Particle.variable("checkStatus1", "on");
    }  
        
    else if(command == "4"){
        colorWipe(strip.Color(30, 20, 5), 50);
        brightness(1);
        Particle.variable("checkStatus1", "on");
    } 
    else if(command == "5"){
        GoND(50,11,40);//30 sets, 8 pixels wide, 50us delay
        colorWipe(strip.Color(120,80,20), 50);
        Particle.variable("checkStatus1", "on");
    }
    else if(command == "6"){
        GoBears(50,11,40);//30 sets, 8 pixels wide, 50us delay
        colorWipe(strip.Color(120,80,20), 50);
        Particle.variable("checkStatus1", "on");
    }
    
    else {
        String aux1 = getValue(command, ',', 0);
        String aux2 = getValue(command, ',', 1);
        String aux3 = getValue(command, ',', 2);
    
        int r = atoi(aux1.c_str());
        int g = atoi(aux2.c_str());
        int b = atoi(aux3.c_str()); 
        colorAll(strip.Color(r,g,b), 50);
        Particle.variable("checkStatus1", "on");
     
    }
   
   
}
  
// Set all pixels in the strip to a solid color, then wait (ms)
void colorAll(uint32_t c, uint8_t wait) {
  uint16_t i;
  
  for(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(wait);
}

void colorWipe(uint32_t c, uint8_t wait) {
    
    
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.

void CandyCane  (int sets,int width,int wait) {
  int L;
  for(int j=0;j<(sets*width);j++) {
    for(int i=0;i< strip.numPixels();i++) {
      L=strip.numPixels()-i-1;
      if ( ((i+j) % (width*2) )<width)
        strip.setPixelColor(L,255,0,0);
      else
        strip.setPixelColor(L,0,255,0);
    };
    strip.show();
    delay(wait);
  }
}


void StaticCandyCane  (int width) {
    int L;
    for(int i=0;i< strip.numPixels();) {
      L=strip.numPixels()-i-1;
      if ( ((i) % (width*2) )<width)
        strip.setPixelColor(L,255,0,0);
      else
        strip.setPixelColor(L,0,255,0);
    };
    strip.show();
}    

void GoND  (int sets,int width,int wait) {
  int L;
  for(int j=0;j<(sets*width);j++) {
    for(int i=0;i< strip.numPixels();i++) {
      L=strip.numPixels()-i-1;
      if ( ((i+j) % (width*2) )<width)
        strip.setPixelColor(L,0,0,102);
      else
        strip.setPixelColor(L,255,153,0);
    };
    strip.show();
    delay(wait);
  }
}

void GoBears  (int sets,int width,int wait) {
  int L;
  for(int j=0;j<(sets*width);j++) {
    for(int i=0;i< strip.numPixels();i++) {
      L=strip.numPixels()-i-1;
      if ( ((i+j) % (width*2) )<width)
        strip.setPixelColor(L,0,0,102);
      else
        strip.setPixelColor(L,255,90,10);
    };
    strip.show();
    delay(wait);
  }
}  

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

A quick perusal reveals at least one issue...

This function should be called one time only in setup() and then you need to simply update checkStatus1, which you should declare as a GLOBAL variable. Refer to the docs....

1 Like

Thanks I will fix that. I don’t think that fixes my main issue, but I appreciate the help. I like clean code.

I wrote this some time ago.

BTW, the wheel in Smartthings returns a hex value, basically the first 2 digits are R then G and the final 2 are B. Just look for a hex to RGB function in groovy and sent RGB to the photon.

That’s what I’m doing. I can verify in logs on ST that I am setting the (255,255,255) value correctly.

Oh and I actually used your getValue in my code, but for some reason it isn’t parsing.

I can tell the value is changing because if I have the strip on it goes off when I pass the r,g,b.

I’m definitely stuck on this though. I feel like the code should work at this time.

Ok I got this sorted. My code was correct. My problem lies in ST where I used single quotes where i should have used double quotes for my put statement. Thanks everyone for the input. My next hurdle will be setting a global variable.

1 Like