There are two ways to create constant variables in code (their sole purpose is to hold one value and to never change it, an example would be k constants fo PID). One is #define and the other is the const keyword. The differences are that #define copies and paste any text specified with other text (it’s a macro).
#define text replacementText //any text in file with the characters 'text' will be replaced by the characters of 'replacementText' string textYeh = "text"; //the value of textYeh would be "replacementText" #define kP 1 //i gave it a value of 1 but could be anything #define kI 1 #define kD 1 /*after some PID stuff later*/ motor[motorYeh] = (error*kP)+(integral*kI)+(derivative*kD); //compiler would copy and paste values into kP, kI, and kD
The const keyword is something like the int or string keyword. All it does is specify the variable as a constant:
const int maxMotorValue = 127; motor[motorYeh] = maxMotorValue; //now you are reading the constant as a variable, no copy and paste stuff going on here
The controversy is this: they both do the same thing. All they do is copy and paste the variable. Although the const keyword doesn’t literally do that, we can understand it’s doing something that acts of that nature. All the const keyword does is add another variable into memory, while #define copies and paste it into the code during compilation and saves memory. I personally use #define because of this reason (however the difference in memory is probably very little). What do you guys use?