Print name of passed variable, not the value

I have many functions that I call from Loop, and pass parameters from the Loop to the function. For example, I have a function called BigEx which displays a big X (8 feet diameter) with LEDs. The call is written as BigEx(500, Green);. The 500 is a delay value (del) and the color of the X will be Green.

This is the function:

void BigEx(int del, byte color)//displays big x  blinks at 1/2 second rate if del=500
{aSerial.p("Function: BigEx  Del:").p(del).pln(color);
  PORTB = color;
    for (byte arm = 1; arm < 8; arm += 2)
  {
    CS.portWrite(arm, 0xFFFF);
  }
  delay (del);
  for (byte arm = 1; arm < 8; arm += 2)
  {
    CS.portWrite(arm, 0x0000);
  }
 }

The very first line of the function is a serial print to monitor (since I have lots of functions I want to know what's running and what the passed parameters are). I use aSerial library because I can put multiple statements on one line. The statement as shown will print BigEx Del: 500 04 because the VALUE of color is 04, corresponding to Port 0B0000 0100, where the 1 (bit 2) represents the wire connected to the GREEN LED color bus.

I want to print the name of function (BigEx), the delay value (500) and the color (GREEN). How do I do that? I want to print GREEN, instead of 04. I have looked at about 30 Google hits trying to solve this likely easy problem i have.

You can't.

You can use the variable to index into an array of c-strings that contain the names.

There's a macro func which holds the name of the function it is in. Check this:

void setup()
{
   Serial.println(__func__);
}

void loop()
{
   Serial.println(__func__);
   delay(1000);
}

Thanks galvo...I will try that.

Arduino_new...How do I use that macro__func__? Can you show me using my parameters above?

Thanks.

to get more responces and make it easier for folks on the forum to answer your question sometimes it's best to give the most basic exaple of what you are trying to do. i think you gave us too much info.

if you are simply asking how to assign a name to a number, then you could do it with a character array or string array like this:

int select;
String colornames [4]={"red","purple","watermellon","yellow"};
void setup() {
  Serial.begin(9600);
  select = 2;
  Serial.println(colornames[select]);


}

void loop() {}
void setup()
{
  Serial.begin(115200);
  report(__func__);
  anotherFunction();
}

void loop()
{
}

void report(char * name)
{
  Serial.println(name);
}

void anotherFunction()
{
  report(__func__);
}

Another useful avenue of research could involve the preprocessor # and ## operators.

One way to get the text and value of the arguments is to call the function through a macro. The downside of this is that you have to change all the function calls and you need a macro for each number of arguments. The upside is that you get the name of the calling function AND you can use it on library functions. This version displays:

setup->BigEx(MyDelay=500, green=4);

Note: I doubt that this macro will work with functions that return a value but I'm sure that can be done.

#define green 4
const unsigned long MyDelay = 500;


#define Call2(func, a1, a2) \
  ({Serial.print(__func__); \
    Serial.print("->" #func "(" #a1 "=");  \
    Serial.print(a1);  \
    Serial.print(", " #a2 "=");  \
    Serial.print(a2);  \
    Serial.println(");");  \
    func(a1,a2);})




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


  Call2(BigEx, MyDelay, green);
}


void loop() {}


//displays big x  blinks at 1/2 second rate if del=500
void BigEx(int del, byte color)
{
}

taterking:

int select;

String colornames [4]={"red","purple","watermellon","yellow"};
void setup() {
  Serial.begin(9600);
  select = 2;
  Serial.println(colornames[select]);

}

void loop() {}

Good suggestion. You can also have an array of color values...

const int NumColors = 3;
char colorNames[NumColors][12]={"red","green","blue"};
byte colorValues[NumColors] = {0b0000001, 0b00000100, 0b00000010};
const byte Red = 0;
const byte Green = 1;
const byte Blue = 2;

void BigX(unsigned long delayMs, byte colorIndex) {
  Serial.print("Delay ");
  Serial.print(delayMs);
  Serial.print("  color ")
  Serial.print(colorNames(colorIndex);

  PORTB = colorValues(colorIndex);
}

void setup() {
  Serial.begin(9600);
  BigX(500, Green);
}

void loop() {}

thanks all. With about 50 functions, I can't see myself typing in a whole lot more code. Seems to me when I was using PICBASIC PRO with Pics, if I wanted to print the parameter name, eg Green, I would just say "print Green" and if I wanted to print the value of Green, I would write "print (Green)" and it would print the value (or the "contents of), in this case 04. In the debugger for that program Green would display 04.

To simplify my first entry: (cause it was too much info)

I defined the parameter: const byte Green = 0x04 //Green bus, pin 10 on UNO //0000 0100

I call the function: BigEx(500, Green;

The first line of function is: void BigEx(int del, byte color)

I print "del" and it gives me 500. I print "Green" and it prints 04. I want to print "Green", that is the equivalent to "color", not the value.

All this is to help with troubleshooting, it's not a major requirement.

I have 28 arrays as it is...and I'm getting warnings about running out of memory on the UNO.
For interest sake I show here all the functions (BibEx is the second one) Any function with a color to be passed is one I want to print the color name for.
// RotAllColor(50, 1); //pass delay and reps
BigEx(500, Green); //displays "x" using 21, 25, 23, 27
BigPlus(500, Red);//displays BigPlus sign using 20, 24, 22, 26
/* BlinkExBigPlus(4);//blink bigex then BigPlus for #reps
Repel(10, Magenta);//use with Pulsar
Pulsar(White);//start at center shimmer to 4th LED
Nova(5, White); //5 del about right, no attempt at slowing down at ends
BlackHole(10, Red); //start at ends, implode
NewGalaxy(1000);//Blackhole followed by Nova, delay after
RotOneColor(50, Green, 2);//pass delay,color,reps
StaryNight(4000);//all lights flickering for number of millis
BlendAll(10000);//all pixels, all colors blending into next one red-pink for 10 seconds
DendriteRot(4); // 4 reps kinda cool having one follow the other (rot and static)
DendriteStatic(4, HUE_PINK); //4 reps
Grow(40, HUE_ORANGE);//pass color in FastLed format, start at center, rotate CW each arm light (no dendrites)
Grow(40, HUE_PINK);//del=5 Nova, del=50 slow grow one level at a time.
Grow(40, HUE_AQUA);
RotThreeColor(50, 2);//pass delay,reps
Shrink(10, HUE_RED);
FadeColor(40, HUE_PINK);//Fade all pixels in passed color with FastLed. fade from dim to full brightness
FadeColor(40, HUE_GREEN);
RainbowRotate(100, 1); //delay 0.1sec, once only
// Breathe (HUE_RED);
// Breathe (HUE_BLUE);
// Breathe (HUE_ORANGE);
// RedGreenRotate(150);//rotates all arms red, then green, then red...
// Psycho(5000);//center and dendrites blue and red disturbing (epileptic) fashion 5sec runtime
// Mishmash(4000, Red, Blue); //Blended colors random on arms and dendrites, developed from DendriteRot, 5 sec time
Mishmash(4000, Red, Green);
// Mishmash(4000, Blue, Green);
// ZoomRedGreen(60);
// Flower(0, 12);//delay 100=Flower, 10=Starburst
// DendriteBlink(4);//4 reps,red dendrites blinking, center 8 magenta "fairly steady"
// RedGreenRotateAcc(2);//rotate red and green alternately, accelerating each pass
ColorPerPixel(0, 12);//lights pixel different color as it progresses outward. 12 pixels=8 colors (4 repeat)
ColorPerPixel(16, 28); //dev 21
ColorPerPixel(32,44);//dev 22
ColorPerPixel(48, 60);//dev 23
ColorPerPixel(64, 76);//dev 24
ColorPerPixel(80, 92);//dev 25
ColorPerPixel(96, 108);//dev 26
ColorPerPixel(112, 124);//dev 27
BigV(16, 28); //lights arm 27 and 21, individual pixels, individual (8)colors
BigCaret(48, 60); //lights arm 23 and 25, individual pixels, individual (8)colors
Starburst(0, 12,4);//lopix,hipix add 16 to each to get another arm delay 100=Flower, 10=Starburst
RotOneColAcc( HUE_RED, 6000);//6 secs is 2 full rotations works
RotOneColAcc(HUE_GREEN, 6000);
// PixChaserVert();
//PixChaserHoriz();

//ExecTimer();//uncomment if timing entire loop
// Testing(32, 44);
*/

I'm getting warnings about running out of memory on the UNO.

In that case, you will definitely want to step away from using literal strings as your aid to debugging.

Being a compiled language there is no source code at runtime (in larger systems you can afford to have a debugger and incorporate a symbol table into the binary, but that really would use up all your memory on a tiny microcontroller).

The compiler's job is to replace source code with binary code, and names get replaced by addresses in memory.

i would personally get a notebook and write down all the corresponding color names and numbers. and put it next to my keyboard for reference. as a general rule, I wouldn't sacrifice any proficiency for debugging unless it is temporary

anyways, if you are running out of memory with your variables, don't forget there is usually more space in your program memory for any variables that are not dynamic.