Troubleshooting Define Commands in C for Robot Motor Control

  • Thread starter Lancelot59
  • Start date
In summary, The conversation is about a user's issue with a define command and the use of preprocessor directives in their code. The user has defined a function in motor_control.c and has a value for "fast" enumerated in a header file. The user is having trouble with the code recognizing the defined value and is getting a syntax error. The expert explains that preprocessor directives only do text replacement and the user should remove the equals sign from the define statement.
  • #1
Lancelot59
646
1
This is once again for my robot. My current issue is with a define command:

I have the following function defined in motor_control.c:

Code:
//*****************************************************************************
//Motor Control Functions
//*****************************************************************************
void forward(void)			//Go striaght forward
{
	motorR(THROTTLE,TRIM);
	motorL(THROTTLE,TRIM);
}
The throttle value controls speed, trim offsets the speed variable. Although it probably will be eliminated.

Anyhow, I have throttle defined in a header called system_config.h:

Code:
#define THROTTLE = fast 	//Default forward throttle value to use

The value for "fast" has been enumerated in the header for motor_control.c

I have included the system_config.h file in the motor_control.c library, so what can't I see it? As far as I'm aware it should be able to see that defined value.
 
Physics news on Phys.org
  • #2
Show error messages.
 
  • #3
Get rid of the equals sign.

#define THROTTLE fast
 
  • #4
D H said:
Get rid of the equals sign.

#define THROTTLE fast

...That was a silly mistake. Thanks for the catch.

The only error message I got was a syntax error when the compiler got to the first function that tried to pass THROTTLE to a function. It didn't say anything else.

Thanks again for the help.
 
  • #5
The code your compiler saw was this.
Code:
motorR(= fast,TRIM);
motorL(= fast,TRIM);

The reason for this is that the #define preprocessor directive does nothing more than text replacement. This is why you DON'T want to put = in simple preprocessor directives.
 
  • #6
I see, that makes sense.
 

Related to Troubleshooting Define Commands in C for Robot Motor Control

1. What are defines in C and how are they used?

Defines in C are preprocessor directives that allow you to define constants or macros. Constants are values that cannot be changed during program execution, while macros are pieces of code that are substituted for a particular keyword or expression. Defines are commonly used for defining mathematical constants, creating shortcuts for commonly used code, or for debugging purposes.

2. How do you define a constant using the #define directive in C?

To define a constant using the #define directive, you use the following syntax: #define CONSTANT_NAME constant_value. For example, if you want to define a constant called PI with the value of 3.14, you would write #define PI 3.14. This constant can then be used throughout your program by referencing its name.

3. Can you use variables in a #define statement in C?

No, variables cannot be used in a #define statement in C. This is because the preprocessor replaces all instances of the defined constant with its value, and variables do not have a fixed value. If you need to use variables, you can use a macro instead, which allows you to pass in variables as arguments.

4. What is the difference between a constant and a macro?

The main difference between a constant and a macro is that constants are replaced by their values during the compilation process, whereas macros are replaced during the preprocessing stage. This means that macros can be used for more complex operations, while constants are limited to simple values. Additionally, macros can be redefined and undefined, while constants cannot be changed once defined.

5. Are there any best practices for using defines in C?

Yes, there are a few best practices to keep in mind when using defines in C. First, it is considered good practice to use uppercase letters for the names of constants to differentiate them from variables. It is also recommended to define constants and macros at the top of your code, before any functions or code blocks. Finally, it is important to use descriptive names for your defines to make your code more readable and maintainable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Math Proof Training and Practice
2
Replies
46
Views
11K
  • Programming and Computer Science
Replies
11
Views
3K
Back
Top