Switch from hex to normal RGB



  • Currently have the following code (at the bottom - removed everything irrelevant) - followed by the examples on this great board which receives hex. Not very strong at programming and need to switch to receiving values like /36/0/1/0/40 249,255,218
    Just do not know how to change the setColor(String hexstring) function

    OLD CODE:

    long RGB_values[3] = {0,0,0};
    String hexstring;
    
    int RValue;
    int GValue;
    int BValue;
    int CurrentLevel;
    
    void before() 
    {
    pinMode(RED_PIN, OUTPUT);
    pinMode(GREEN_PIN, OUTPUT);
    pinMode(BLUE_PIN, OUTPUT);
    TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
    }
    
    void receive(const MyMessage &message) {
    if (message.type==V_RGB)
    {
    	hexstring = message.getString();
    	Serial.print("RGB command: ");
    	Serial.println(hexstring);
    	setColor(hexstring);
    }
      else if (message.type==V_PERCENTAGE)
    {
    int dimLevel = message.getInt(); //0-100%
    Serial.print("Dim command: ");
    Serial.println(dimLevel);
    setDimLevel(dimLevel);
    saveState(SAVE_DIMMER_LEVEL, dimLevel);
    }
    else if (message.type==V_STATUS)
    {
    	if(message.getBool() == RELAY_ON)
    	{
    	setColor("FFF1E0");
    	saveState(SAVE_LIGHT_STATE, RELAY_ON);
    	}
    	if(message.getBool() == RELAY_OFF)
    	{
    	analogWrite(RED_PIN, 0);
    	analogWrite(GREEN_PIN, 0);
    	analogWrite(BLUE_PIN, 0);
    	saveState(SAVE_LIGHT_STATE, RELAY_OFF);
    	}
    }
    

    }

    void setDimLevel(int level) 
    {
    level = level > 100 ? 100 : level;
    level = level < 0 ? 0: level;
    int delta = ( level - CurrentLevel) < 0 ? -1 : 1;
    RValue = (int)(level / 100. * RValue);
    BValue = (int)(level / 100. * BValue);
    GValue = (int)(level / 100. * GValue);
    analogWrite(RED_PIN, RValue);
    analogWrite(GREEN_PIN, GValue);
    analogWrite(BLUE_PIN, BValue);
    CurrentLevel = level;
    }
    void setColor(String hexstring) 
    {
    long number = (long) strtol( &hexstring[0], NULL, 16);
    Serial.print("Color long: ");
    Serial.println(number);
    RValue = number >> 16;
    GValue = number >> 8 & 0xFF;
    BValue = number & 0xFF;
    Serial.print("Color: ");
    Serial.println(hexstring);
    Serial.print("Red: ");
    Serial.println(RValue);
    Serial.print("Green: ");
    Serial.println(GValue);
    Serial.print("Blue: ");
    Serial.println(BValue);
    analogWrite(RED_PIN, RValue);
    analogWrite(GREEN_PIN, GValue);
    analogWrite(BLUE_PIN, BValue);
    }

  • Hero Member

    @moskovskiy82 Not totally sure what you need but if you have a look at my setcolor function in this post it uses RGB values to change the colours. Is that what you meant?



  • Not exactly...
    Here we are awaiting a hex value

    void receive(const MyMessage &message) {
    if (message.type==V_RGB)
     {
    hexstring = message.getString();
    ....
    

    And than process those to rgb values. But if via MQTT we will receive RGB values like 243,145,156 and not hex - how to change the code accordingly?



  • Hi,
    you can try this:

    
    if (message.type==V_RGB)
    {
          char convBuffer[8];
          hexstring = strtol(message.getString(convBuffer),0,16);
          Serial.println(hexstring);
          r=(int)(hexstring>>16);
          g=(int)(hexstring>>8)& 0xFF;
          b=(int)(hexstring)& 0xFF;
    }
    

    Hope it helps.


  • Hero Member

    @moskovskiy82 I have not used MQTT so am not sure how it sends the data. Does it still send it as V_RGB ? if it does then you will need to parse the data to split it into the three separate values . Do a google search for: Arduino parse comma delimited string and you will see what is needed.



  • Sorry tp bump an old one but still haven't found a solution.
    MQTT passes it as
    mys-in/36/0/1/0/40 255,85,73

    So the message payload can be passed directly to arduino as PWM values


Log in to reply
 

Suggested Topics

  • 4
  • 3
  • 2
  • 933
  • 9
  • 1

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts