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) functionOLD 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); } -
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) functionOLD 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); }@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 valuevoid 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?
-
-
Not exactly...
Here we are awaiting a hex valuevoid 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?
@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,73So the message payload can be passed directly to arduino as PWM values
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login