I’m a n00b to programming, and I just learned what a deadband is. It seems like it can save me a lot of time and if/else if/else statements, but I don’t know how to set it for a specific variable. I keep finding things like
#define JOY_X_DEADBAND 10
or something like that, but how do I do it for a specific variable? Can I?
The #define macro is one way to do a value assignment, and using a variable is another (there are other ways as well, but we’ll focus on these two).
When using a variable, you need to do three major things; decide what type of data the variable will store, decide what name you want to give to the variable, and assign the variable a value/use it in your code.
In this case, we know we want to use whole numbers for our deadband value, so we will use an integer (int) data type for the variable. We also know that we are going to be using this variable’s value for a deadband, so we can give it the name “deadband”. Finally, we know we want to give the deadband variable a specific value, in this case a value of ten. Declaring the variable would look something like this:
Now that we have created the variable with a data type, name, and value, we can use it in our program. The following program snippet is taken from the “Dual Joystick Control with Threshold” sample program in the Remote Control Sample Programs folder:
The second way is to use #define macros to achieve a similar result. There are three parts to this; the #define keyword, the ‘word’ that will be replaced in the program, and the value that it will be changed to.
These macros basically act as a find-and-replace for your program; before the program is compiled to the robot, the #define macro will replace all of the instances of the ‘DEADBAND’ text in the program with the text ‘10’. This saves a bit on memory space (as the variable method creates a variable in memory) but does not allow for manipulation of the deadband value in the program.
There are many other ways to do this, and each has its own pros and cons. This starts to get into the advanced programming, so I would suggest checking out the technical discussions forum (https://vexforum.com/forumdisplay.php?f=28) to see how other teams do their joystick deadband implementation.