Can someone help explain what Hex String & Hex in this code does?

#include <LEDStripDriver.h>

// use two available GPIO pins from your board
// DIN=GPIO16, CIN=GPIO14 in this example
LEDStripDriver led = LEDStripDriver(16, 14);


void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
  led.setColor(255, 0, 0); // RGB
  delay(1000);
  led.setColor("#00FF00"); // Hex String
  delay(1000);
  led.setColor(0x0000FF); // Hex
  delay(1000);
  led.setColor(); // turn off
  delay(1000);
}

Here is the source of the code if its any help: GitHub - MrKrabat/LED-Strip-Driver-Module: Arduino library for the full color RGB LED strip driver module shield

It looks like the setColor() function takes 3 values, one each for red, green and blue at a guess

So to set the colour of an LED you need to send either 6 characters such as "00FF00" to set red to zero, green to FF and blue to zero. The # is presumably used by the library for some purpose. An alternative format would seem to be the normal hex notation of 0x00FF00 (not a string) that sets the colours to the same values

This is based on guesswork as I do not have the LEDStripDriver library

The code just shows the 3 different options available for passing data to setcolor(). "#00FF00" is equivalent to 0, 255, 0 (green) and 0x0000FF = 0, 0, 255 (blue).

Steve

This is a standard represenation for RGB colour used in many different pieces of software, notable HTML/CSS.

I have attached the library below.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include <LEDStripDriver.h>

const char* ssid = "";
const char* password = "";
const char* mqtt_server = "broker.mqttdashboard.com";
const char* mqtt_user = "";
const char* mqtt_pswd = "";
char messageBuffer[50];

WiFiClient espClient;
PubSubClient client(espClient);

// DIN=GPIO16, CIN=GPIO14 in this example
LEDStripDriver led = LEDStripDriver(16, 14);

unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1000;

bool power = false;

void setup_wifi()
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  delay(2000);
}

void setup()
{
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
void loop()
{
  client.loop();
  if (!client.connected())
  {
    reconnect();
  }
  if (power == true)
  {
    RainbowEffect();
  }
}

void reconnect()
{
  while (!client.connected())
  {
    Serial.println("Attempting MQTT connection...");
    if (client.connect("Reception"))
    {
      Serial.println("Connected to MQTT server");
      client.subscribe("RGB/Output");
    }
    else
    {
      Serial.print("Failed to connect to MQTT...");
      Serial.println();
      Serial.println("Attempting to connect again in 3 seconds.");
      delay(3000);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.println();
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print("length : ");
  Serial.println(length);
  memcpy(messageBuffer, payload, length);
  messageBuffer[length] = '\0';
  Serial.print("the payload is : ");
  Serial.println(messageBuffer);
  if (strcmp(messageBuffer, "on") == 0)
  {
    {
      Serial.print("turning ");
      Serial.println(messageBuffer);
      digitalWrite(LED_BUILTIN, LOW);
      power = true; //activate rainbow 
    }
  }
  else if (strcmp(messageBuffer, "off") == 0)
  {
    {
      Serial.print("turning ");
      Serial.println(messageBuffer);
      digitalWrite(LED_BUILTIN, HIGH);
      OffRainbow();
      power = false;  //stop the rainbow
    }
  }

}
void OffRainbow()
{
  led.setColor(); // turn off
}
void RainbowEffect()
{
  currentTime = millis();
  if (currentTime - startTime >= period)
  {
    for (uint16_t color = 0; color < 256; color++) {
    led.setColor(color, color, color);}
    }
    startTime = currentTime;
}

I'm trying to integrate it to this code. Everything except RainbowEffect works(Led does not light up when given command). How shld I cycle thorugh colours of the led strip, in this case?

LED-Strip-Driver-Module-master.zip (106 KB)

Check your brace pairing vs indentation....

 void RainbowEffect()
{
  currentTime = millis();
  if (currentTime - startTime >= period)
  {
    for (uint16_t color = 0; color < 256; color++) {
    led.setColor(color, color, color);}
    }
    startTime = currentTime;
}

That code always sets startTime equal to currentTime. Therefore the if statement never executes.

Edit: Also it’s not going to be a very effective effect if you do ALL of the steps of the rainbow in one go, and then wait for “period” milliseconds without doing anything. But that’s a different problem....

Edit again: Or indeed give a “rainbow” effect if the 3 values represent RGB intensities.

pcbbc:
Edit: Also it’s not going to be a very effective effect if you do ALL of the steps of the rainbow in one go, and then wait for “period” milliseconds without doing anything. But that’s a different problem....

Edit again: Or indeed give a “rainbow” effect if the 3 values represent RGB intensities.

void RainbowEffect()
{
  currentTime = millis();
  if (currentTime - startTime >= period)
  {
    for (uint16_t r = 0; r < 256; r++) 
    {
      for (uint16_t b = 0; b < 256; b++) 
      {
        for (uint16_t g = 0; g < 256; g++) 
        {
          led.setColor(r, b, g);
   }
     }
       }
    startTime = currentTime;
    }

I just tested the code, the led remained white and is unable to turn off when given command. How else do I give it a rainbow effect without using the nested for loop?

void RainbowEffect()
{
  if (currentTime - startTime >= period)
  {
    countR++;
    if (countR > 1)
    {
      countR = 0;
      countG++;
      if (countG > 1)
      {
        countG = 0;
        countB++;
        if (countB > 1)
        {
          countB = 0;
        }
      }
    }
    led.setColor(countR,countG,countB);
    startTime = currentTime;
}

I tried if else statement & strip remains off. Is there something wrong with the code above?