I don’t know if it’s just me, but I can’t seem to figure out what #define means and how it works and how to use it. I can’t seem to find any sources on google that helps explain it in a way so that I understand… Everyone seems to know already what it does lol

Can anyone help with explaining #define? Thanks!

Define is similar to initializing normal variables (int a, float a, etc) but it remains constant. You can use either form of declaring variables if the variable does not change. Define is good for uses such as PID values that do not change throughout the program. Also, variables declared using #define are usually in all caps to easily distinguish them from normal variables. If you are using it, then you should put it after the #pragma statements at the top of the program. It has the form:

#define        name        replacement text

Name is the name of the variable (sequence of letters and digits that start with a letter) and the replacement text can be anything you want the variable to actually be, which is not limited to just numbers.

I hope this helps.

To add on to/clarify the above
#define simply replaces the first bit of text everywhere it appears in your program with the second bit of text at compile time.

#define FOO 123
int myVar = FOO;
motor[1] = FOO;


int myVar = 123;
motor[1] = 123;

when the code is compiled. FOO and 123 can be anything at all, not necessarily numbers on one hand and text on the other.

#define is not a variable. It is like a search/replace feature in your text editor. The compiler will replaced all instance of #define text with the appropriate value before compiling your program.

#define is a pre-processor directive in the C family of languages (including ROBOTC). This means that this code is not instructing the micro controller, like most of the code you write. It’s giving instructions to the compiler, in the case to replace something with something else. So, for example, this code:

#define PI 3.141592653;
int tau = PI * 2

is transformed to

int tau = 3.141592653 * 2

and then compiled. You would use these in case where you don’t want a variable, likely because what you’re doing is static, or where your being really particular about memory, I guess, since, because they are converted before runtime, they have no cost for memory considerations.