Runtime error: function: assertion "%s" failed: file "%s", line %d%s%s

Hi,

I’m having a little bit of trouble getting the value of only the frist two itens in the following array:

String colorsNames = {
String(“black”),
String(“red”),
String(“green”),
String(“blue”),
String(“orange”),
String(“yellow”),
String(“cyan”),
String(“purple”),
String(“pink”),
String(“white”),
String(“summer”),
String(“purple_red”),
String(“viridis_gp”),
String(“classic_eq”),
String(“oceanInside”),
String(“cool_hot”),
String(“hot_cool”),
String(“pride”),
};

I have this function:

void displayColors(int colorChange) {
colorValue = colorValue + colorChange;

Serial.println(“colorValue”);
Serial.println(colorValue);
Serial.println(“colorChange”);
Serial.println(colorChange);
Serial.println(“colorName”);

if (colorValue > TOTAL_PALETTES-1) {
colorValue = 0;
} else if (colorValue < 1) {
colorValue = TOTAL_PALETTES-1;
}
Serial.println(colorsNames[colorValue]);
// display(“colors”);
}

When colorValue is 2 or higher it works fine, but when color values ammounts to 0 or 1 serial monitor goes haywire with this:
function: assertion “%s” failed: file “%s”, line %d%s%s
among a lot of random characters.

Any help?

read how to use the forum please, don’t post snippets and use code tags.


the String class is probably not the best way to store your strings.

A const char* array would probably be good enough - and use progmem if memory runs low

const char * colorsNames[] = {
  "black",
  "red",
  "green",
  "blue",
  "orange",
  "yellow",
  "cyan",
  "purple",
  "pink",
  "white",
  "summer",
  "purple_red",
  "viridis_gp",
  "classic_eq",
  "oceanInside",
  "cool_hot",
  "hot_cool",
  "pride"
};
// count how many we have
const byte TOTAL_PALETTES = sizeof(colorsNames) / sizeof(colorsNames[0]);
 else if (colorValue < 1) {
      colorValue = TOTAL_PALETTES-1;
    }

hum… 0 is an acceptable index. if you do that you’ll have a hard time with “black”

 if (colorValue < 1) {
      colorValue = TOTAL_PALETTES-1;

Zero is less than one.
What’s wrong with “black”?

J-M-L:
the String class is probably not the best way to store your strings.

I 100% agree...

const char colorsNames[][18] = {
  "black",
  "red",
  "green",
  "blue",
  "orange",
  "yellow",
  "cyan",
  "purple",
  "pink",
  "white",
  "summer",
  "purple_red",
  "viridis_gp",
  "classic_eq",
  "oceanInside",
  "cool_hot",
  "hot_cool",
  "pride",
};

// ...

void displayColors(int colorChange) {
  colorValue += colorChange;
  if (colorValue > TOTAL_PALETTES - 1) colorValue = 0;
  else if (colorValue < 0) colorValue = TOTAL_PALETTES - 1;
  Serial.println("colorValue");
  Serial.println(colorValue);
  Serial.println("colorChange");
  Serial.println(colorChange);
  Serial.println("colorName");
  Serial.println(colorsNames[colorValue]);
}

You want to do your change on colorValue before your print statements, so they reflect the same information.

…you may wish to add some array dimensions, or change the array type to char*

you can also do

 void displayColors(int colorChange) {
  colorValue = (colorValue + colorChange) % TOTAL_PALETTES;
...

to keep colorValue within the [0, TOTAL_PALETTES[ interval

luisalonsobr:
Any help?

Your code works fine for me after adding some of the parts you left out. Maybe the problem is in the parts you didn’t show? Perhaps you should show a complete working sketch that shows the problem.

size_t colorValue = 0;


String colorsNames[] =
{
  String("black"),
  String("red"),
  String("green"),
  String("blue"),
  String("orange"),
  String("yellow"),
  String("cyan"),
  String("purple"),
  String("pink"),
  String("white"),
  String("summer"),
  String("purple_red"),
  String("viridis_gp"),
  String("classic_eq"),
  String("oceanInside"),
  String("cool_hot"),
  String("hot_cool"),
  String("pride"),
};
const size_t TOTAL_PALETTES = sizeof colorsNames / sizeof colorsNames[0];


void displayColors(int colorChange)
{
  size_t oldColorValue = colorValue;
  colorValue = oldColorValue + colorChange;

  if (colorValue > TOTAL_PALETTES - 1)
  {
    colorValue = 0;
  }
  else if (colorValue < 1)
  {
    colorValue = TOTAL_PALETTES - 1;
  }


  Serial.print("old colorValue ");
  Serial.print(oldColorValue);
  Serial.print(" colorChange ");
  Serial.print(colorChange);
  Serial.print(" new colorValue ");
  Serial.print(colorValue);
  Serial.print(" colorName ");
  Serial.println(colorsNames[colorValue]);
  //      display("colors");
}


void setup()
{
  Serial.begin(115200);
}


void loop()
{
  displayColors(1);
  delay(333);
}

I also changed the display format so it didn’t put every field on a separate line.
Output:

old colorValue 14 colorChange 1 new colorValue 15 colorName cool_hot
old colorValue 15 colorChange 1 new colorValue 16 colorName hot_cool
old colorValue 16 colorChange 1 new colorValue 17 colorName pride
old colorValue 17 colorChange 1 new colorValue 0 colorName black
old colorValue 0 colorChange 1 new colorValue 1 colorName red
old colorValue 1 colorChange 1 new colorValue 2 colorName green
old colorValue 2 colorChange 1 new colorValue 3 colorName blue
old colorValue 3 colorChange 1 new colorValue 4 colorName orange
old colorValue 4 colorChange 1 new colorValue 5 colorName yellow
old colorValue 5 colorChange 1 new colorValue 6 colorName cyan
old colorValue 6 colorChange 1 new colorValue 7 colorName purple
old colorValue 7 colorChange 1 new colorValue 8 colorName pink
old colorValue 8 colorChange 1 new colorValue 9 colorName white
old colorValue 9 colorChange 1 new colorValue 10 colorName summer
old colorValue 10 colorChange 1 new colorValue 11 colorName purple_red
old colorValue 11 colorChange 1 new colorValue 12 colorName viridis_gp
old colorValue 12 colorChange 1 new colorValue 13 colorName classic_eq
old colorValue 13 colorChange 1 new colorValue 14 colorName oceanInside
old colorValue 14 colorChange 1 new colorValue 15 colorName cool_hot
old colorValue 15 colorChange 1 new colorValue 16 colorName hot_cool
old colorValue 16 colorChange 1 new colorValue 17 colorName pride
old colorValue 17 colorChange 1 new colorValue 0 colorName black
old colorValue 0 colorChange 1 new colorValue 1 colorName red
old colorValue 1 colorChange 1 new colorValue 2 colorName green