Char Array passed to function printing out garbage.

Hi Guys, love arduino, gave me a new lease on life. building a project using a tft screen. Got the screen working now but I have written some code that does not seem to work. I have researched for hours but I cannot see what I am doing wrong. (new to C programming). Please find the two functions I have written below. The idea was to use the function to draw leds (circles) multiple times on different screens where required using the char array and passing it into the drawLEDCircle function. I keep getting garbage printed out on the tft and serial monitor.

void showControlLEDS() {

int arraySize = 14;
int ledLabelLength = 20;
int xPosTxt = 40;
int yPosTxt = 50;
int xPosCircle = 20;
int yPosCircle = 53;

tft.setTextSize(2);
tft.setCursor(50, 20);
tft.setTextColor(ForestGreen);
tft.print("LED");
tft.drawLine(10, 40, 140, 40, ForestGreen);
tft.drawLine(140, 40, 140, 300, ForestGreen);
tft.setTextSize(1);

char ledArray[arraySize];
char ledLabels[arraySize][ledLabelLength] = {"Auto Mode", "Manual Mode", "Bypass Mode", "Radio Rx", "VAC Active", "Servo's Running", "Pump Running", "Tank Empty", "Tank Level OK", "Tank Filling", "Tank 100% Full", "Tank => 75% Full", "Tank <= 25% Full"};

tft.setTextColor(Yellow);
drawLEDCircle(ledArray, arraySize, ledSpacing,xPosTxt, yPosTxt , xPosCircle, yPosCircle); //ledSpacing is a global const int
}

//---------------------------------------------------------------------------------
void drawLEDCircle(char arrayName[], int Size, int spacing,int xPosT, int yPosT, int xPosCirc, int yPosCirc) {

for (uint8_t idx = 0; idx < Size; idx++) {
tft.setCursor(xPosT, yPosT);
tft.drawCircle(xPosCirc, yPosCirc, ledRadius, White);
yPosT = yPosT + spacing;
yPosCirc = yPosCirc + spacing;
tft.print(arrayName[idx]);
Serial.println(arrayName[idx]);
}
}
//---------------------------------------------------------------------------------
Serial monitor printout:


{

Any assistance would be grateful. thankyou

When posting code, please use the code-tags (</>, the leftmost button).

 char ledArray[arraySize];
 char ledLabels[arraySize][ledLabelLength] = {"Auto Mode", "Manual Mode", "Bypass Mode", "Radio Rx", "VAC Active", "Servo's Running", "Pump Running", "Tank Empty", "Tank Level OK", "Tank Filling", "Tank 100% Full", "Tank => 75% Full", "Tank <= 25% Full"};

 tft.setTextColor(Yellow);
 drawLEDCircle(ledArray, arraySize, ledSpacing,xPosTxt, yPosTxt , xPosCircle, yPosCircle);  //ledSpacing is a global const int

You are passing the ledArray to the function, instead of the initialized ledLabels array. Since ledArray is not initialized, you will only get garbage in the serial monitor. Also, you are setting up the constant string-array everytime you call the showControlLEDS function. This is unnecessary overhead, use a global array (like you did for ledSpacing) for that or wrap it into a class.

Also change your type to char* [], instead of char[][]. This will make things a lot easier.

const unsigned short arraySize = 14;
char* ledLabels[arraySize]= {"Auto Mode", "Manual Mode", "Bypass Mode", "Radio Rx", "VAC Active", "Servo's Running", "Pump Running", "Tank Empty", "Tank Level OK", "Tank Filling", "Tank 100% Full", "Tank => 75% Full", "Tank <= 25% Full"};

void showControlLEDS()
{
}

void drawLEDCircle()
{
}

void setup()
{
}

void loop()
{
}

Could it be a baudrate mismatch between the Serial.begin() and the Serial Monitor?

 char ledArray[arraySize];

 drawLEDCircle(ledArray, arraySize, ledSpacing,xPosTxt, yPosTxt , xPosCircle, yPosCircle);

You are passing a local, not initialized char array and treat it as if it had names in it.

Hi Guys thanks for the help. :slight_smile: I went with the following:

//---Global------------------------------------------------------------------------------
const unsigned short ledRadius = 7;
const unsigned short ledSpacing = 20;
const unsigned short ledControlArraySize = 14;
char* ledControlLabels[ledControlArraySize] = {"Auto Mode", "Manual Mode", "Bypass Mode", "Radio Rx", "VAC Active", "Servo's Running", "Pump Running", "Tank Empty", "Tank Level OK", "Tank Filling", "Tank 100% Full", "Tank => 75% Full", "Tank <= 25% Full"};

//---Functions------------------------------------------------------------------------------
void showControlLEDS() {

  int xPosTxt = 40;
  int yPosTxt = 50;
  int xPosCircle = 20;
  int yPosCircle = 53;

  tft.setTextSize(2);
  tft.setCursor(20, 20);
  tft.setTextColor(ForestGreen);
  tft.print("Controller");
  tft.drawLine(10, 40, 150, 40, ForestGreen);
  tft.drawLine(150, 40, 150, 300, ForestGreen);
  drawControlLEDS(ledControlLabels, ledControlArraySize , ledSpacing, xPosTxt, yPosTxt , xPosCircle, yPosCircle);
}
//---------------------------------------------------------------------------------
void drawControlLEDS(char* arrayName[], int Size, int spacing, int xPosT, int yPosT, int xPosCirc, int yPosCirc) {

  tft.setTextSize(1);

  for (uint8_t idx = 0; idx < Size - 1; idx++) {   //Size - 1 deals with null and stops extra circle being drawn.
    if(idx <3) {  //changes mode text color to orange
       tft.setTextColor(Orange);
    }
    else {
       tft.setTextColor(Yellow);
    }
    tft.setCursor(xPosT, yPosT);
    tft.drawCircle(xPosCirc, yPosCirc, ledRadius, White);
    yPosT = yPosT + spacing;
    yPosCirc = yPosCirc + spacing;
    tft.print(arrayName[idx]);
    Serial.println(arrayName[idx]);
  }
}
//---------------------------------------------------------------------------------

One more thing: Since your array containing the names and its size are now global, you can remove them from the drawLEDCircle parameter list. If you want to pass different arrays into it, it has to stay the same though.

thanks Lightuc. I plan to pass other arrays through it. :slight_smile: