Error writing String (hex color code) in strip.setPixelColor (NeoPixel)

Hi! I write a function by which I can change the modes of the LED:

void light(int mode) { // mode-color: 1 - white / 2 - green / 3 - blue / 4 - orange / 5 - red
  String color = "";
  switch (mode) {
    case 1:
      color = "0xffffff";
      break;
    case 2:
      color = "0x00FF00";
      break;
    case 3:
      color = "0x3a4493";
      break;
    case 4:
      color = "0xff4500";
      break;
    case 5:
      color = "0xd70101";
      break;
  }
    for (int i = 0; i < NUM_LEDS; i++) {
        strip.setPixelColor(i, color);
        strip.show(); // Send Color Code
        color = "";
    }
}

An error occurs during compilation:

no matching function for call to 'Adafruit_NeoPixel::setPixelColor(int&, String&)'

How to fix it?

(I`m using Arduino UNO and Adafruit_NeoPixel lib)

You're trying to pass a String to setPixelColor() as the second argument. That is absolutely not supported.

From https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-use

An alternate syntax has just two arguments:

strip.setPixelColor(n, color);

Here, color is a 32-bit type that merges the red, green and blue values into a single number. This is sometimes easier or faster for some (but not all) programs to work with; you’ll see the strandtest code uses both syntaxes in different places.

How to fix it?

STOP USING STRINGS! For anything!

The color is NOT a string or a String or a moldy sandwich. It is a NUMBER!

// Light Func
void light(int mode) { // mode-color: 1 - white / 2 - green / 3 - blue / 4 - orange / 5 - red
  switch (mode) {
    case 1:
     for (int i = 0; i < NUM_LEDS; i++) {
       strip.setPixelColor(i, 0xffffff);
       strip.show(); // Send Color Code
    }
      break;
    case 2:
     for (int i = 0; i < NUM_LEDS; i++) {
       strip.setPixelColor(i, 0x00FF00);
       strip.show(); // Send Color Code
    }
      break;
    case 3:
    for (int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, 0x3a4493);
      strip.show(); // Send Color Code
    }
      break;
    case 4:
    for (int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, 0xff4500);
      strip.show(); // Send Color Code
    }
      break;
    case 5:
    for (int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, 0xd70101);
      strip.show(); // Send Color Code
    }
      break;
  }
}

This works great, but I want to simplify the code.
I tried changing String to char, but it did not work.

Suyano:
I tried changing String to char, but it did not work.

That would be because a char can’t represent a 24-bit integer.

Store the colours in an array and use mode as an index; in that case you don't need a switch/case.

Don't forget that the index in an array starts at 0 for the first element.

Read the documentation I posted:

Here, color is a 32-bit type that merges the red, green and blue values into a single number.

Why would you try to use the char type to hold a 32 bit number?

Let's look at the Arduino reference page to see if we can find out which type would be suitable to use for a 32 bit number on an Uno: https://www.arduino.cc/reference/en/ This one looks pretty good: https://www.arduino.cc/reference/en/language/variables/data-types/unsignedlong/

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes).

unsigned long solved the problem!

void light(int mode) { // mode-color: 1 - white / 2 - green / 3 - blue / 4 - orange / 5 - red
  unsigned long color = 0;
  int c = 0;
  switch (mode) {
    case 1:
      color = 0xffffff;
      c = 1;
      break;
    case 2:
      color = 0x00FF00;
      c = 1;
      break;
    case 3:
      color = 0x3a4493;
      c = 1;
      break;
    case 4:
      color = 0xff4500;
      c = 1;
      break;
    case 5:
      color = 0xd70101;
      c = 1;
      break;
  }
    if(c == 1) {
    for (int i = 0; i < NUM_LEDS; i++) {
        strip.setPixelColor(i, color);
        strip.show(); // Send Color Code
    }
    c = 0;
}
}

Thank you very much! :wink: