Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. General Discussion
  3. Switch from hex to normal RGB

Switch from hex to normal RGB

Scheduled Pinned Locked Moved General Discussion
6 Posts 3 Posters 1.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moskovskiy82
    wrote on last edited by
    #1

    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);
    }
    
    Boots33B 1 Reply Last reply
    0
    • M moskovskiy82

      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);
      }
      
      Boots33B Offline
      Boots33B Offline
      Boots33
      Hero Member
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moskovskiy82
        wrote on last edited by
        #3

        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?

        Boots33B 1 Reply Last reply
        0
        • M Offline
          M Offline
          mortommy
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M moskovskiy82

            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?

            Boots33B Offline
            Boots33B Offline
            Boots33
            Hero Member
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              moskovskiy82
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              24

              Online

              11.7k

              Users

              11.2k

              Topics

              113.1k

              Posts


              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • MySensors
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular