defining a macro with variables

hi there,
could you help me for this please..

void _user_gcode_exe(const char * const cmd) {
lcd_return_to_status();
enqueue_and_echo_commands_P(cmd);
}

void user_gcode_exe_1() {

float XOFF = (25.00 + PRB_XOFFSET + (TOOL_DIA * 0.5));
float YOFF = (25.00 - (PRB_YOFFSET + (TOOL_DIA * 0.5)));
float ZOFF = (10.00 + PRB_ZOFFSET);

#define PRB_GCODE "M117 Z-XY Probing now..\nG38.2 F120 Z-20.00\nG92 X0.00 Y.00 Z0.00\nG0 Z5.00 F1200\nG0 X-20.00 F1200\nG0 Z-5.00 F1200\nG38.2 F120 X25.00\nG92 X0.00\nG0 X-5.00 Y-25.00 F1200\nG0 X15.00 F1200\nG38.2 F120 Y25.00\nG92 Y0.00\nG0 Y-5.00 F1200\nG0 Z10.00 F1200\nG0 X-25.00 Y25.00 F1200\n"

#define PRB_GCODE_END "G92 X-"XOFF" Y"YOFF" Z"ZOFF"\n"
#define MSG__OK "M117 Z-XY Probing done..\n"
_user_gcode_exe(PSTR(PRB_GCODE PRB_GCODE_END MSG__OK));

}

After PRB_GCODE_END definition is complete
The macro value must be G92 X-26.50 Y23.50 Z15.00
Do you have any idea how to put these variables into the definition when I defining the PRB_GCODE_END ?

The correct answer depends on how you are using PRB_GCODE, so please post your entire sketch. Please follow the forum guidelines for posting code, using code tags.

aarg:
The correct answer depends on how you are using PRB_GCODE, so please post your entire sketch. Please follow the forum guidelines for posting code, using code tags.

[color=#202124]thanks i updated my problem[/color]

You didn't like the answers you got before? definin a macro with variables - Programming Questions - Arduino Forum

I do not think there is a way to put a calculated value into that #define statement, just do the calculations and insert the answer yourself. If you are intending to do the calculations at run-time it will not work anyway, because the resulting text string is being stored in program memory, which cannot be altered at run-time.

After PRB_GCODE_END definition is complete
The macro value must be G92 X-26.50 Y23.50 Z15.00

Then just use that value in the #define and forget about the calculations:

#define PRB_GCODE_END "G92 X-26.50 Y23.50 Z15.00\n"

david_2018:
You didn't like the answers you got before? definin a macro with variables - Programming Questions - Arduino Forum

I do not think there is a way to put a calculated value into that #define statement, just do the calculations and insert the answer yourself. If you are intending to do the calculations at run-time it will not work anyway, because the resulting text string is being stored in program memory, which cannot be altered at run-time.

ok thanks,
String zxyPRBgcend = "G92 X-" + String(float(XOFF)) + " Y" + String(float(YOFF)) + " Z" + String(float(ZOFF)) + "\n";
Could there be a way to define the variable zxyPRBgcend with #define in this definition? For example:
#define PRB_GCODE_END zxyPRBgcend

Why do you need to calculate these values at run-time?

xyPRBgcend with #define in this definition? For example:
#define PRB_GCODE_END zxyPRBgcend

No . That is simply the best answer i can come up with. You have to understand what '#define' actually does.
As i understand, the pre-compiler, replaces the keyword anywhere within the sketch with whatever 'literal' value (or data, basically whatever comes right after the keyword on the same line)
A variables value is typically unknown at pre-compile time, and although the optimizer may decide that it is known, that step only happens afterwards.
Now of course one could do #define zxyPRBgcend = "G92 X-" + String(float(XOFF)) + " Y" + String(float(YOFF)) + " Z" + String(float(ZOFF)) + "\n";and then create an alias by #define PRB_GCODE_END zxyPRBgcendbut why ?
i use #define to create better overview within my program and to create values which i do known at compile time, but that i may use throughout the program and i define them all at the beginning of my program so i don't need to look for them. When reducing memory consumption, sometimes the optimizer may need some extra help. Consider this on an ATtiny13

#define TWELVE 12
#define FOURTEEN 14
#define BO_BRACE TWELVE * FOURTEEN
#define BRACE (TWELVE * FOURTEEN)

that the #define without the braces takes up more progmem than the #define with the braces (somehow... i found that quite strange though)

david_2018:
Why do you need to calculate these values at run-time?

Hello there,
In this project, the Origin point is determined for the XYZ probe used in the CNC machine, the offset distances for the probe used for the probing process and the diameter value for the insert may vary. In order to send these changes to the software and Gcode system, changes are required during the program's running.

You cannot do this using the enqueue_and_echo_commands_P() function, that requires a string stored in program memory, which cannot be changed at run-time. I'm not familiar enough with Marlin to know if there is an equivalent command that takes a string stored in ram, you may want to ask in a forum that is specific to Marlin, but in that case you would not use a #define to create the string so do not confuse people by asking the wrong question.

In order to send these changes to the software and Gcode system, changes are required during the program's running.

Then using #define is not the proper way to do this. If you want to retain the overview you could make the desired value as a return value to a function, though using the 'String' class may get you into trouble on an AVR (so using char array is recommended.)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.