0

Line Robot - Basic lessons - Define

As we learned already, "static" keyword transforms a variable, declared in a function, into a permanent variable, that keeps its value between function calls. An even bigger challenge is to have a value that is available in 2 or more functions. For example, some values are needed to set the robot's behaviour and they are supposed to be used in many functions. Like, You can choose if the robot will go slow or fast. We have to enter this specific speed somewhere and then all the functions that use motors will have to have access to this value in order to calculate speeds. We cannot store the value in one function because functions cannot read other functions' data. While there are more ways to solve this problem, we will show a simple one, a kind of text processor's copy-paste solution: "define" command. For example, let's put in the code this statement:
#define INITIAL_SPEED 50
The meaning of this instruction is "before compiling, search all the code and replace every occurence of "INITIAL_SPEED" by "50". "INITIAL_SPEED" is a name You choose. Having entered the line above, the function
void RobotLine::loop() {
	int startSpeed = INITIAL_SPEED;
}
will be replaced by
void RobotLine::loop() {
	int startSpeed = 50;
}
before compiling. Find C:\Users\[user name]\Documents\Arduino\libraries\mrm-robot\src\mrm-robot-line.h, in the same directory as mrm-robot-line.cpp You have been using by now. This file is called a header, therefore ".h" suffix. We will not be explaining its content here, just remember that, if You want to have Your own "#define" commands, put them here. Look at the beginning of the file. It will contain some commands similar to these:
#define CATCH_SERVO_CLOSE 250 // Closed position, ball caught.
#define CATCH_SERVO_OPEN_FULL 70 // Open position, ready to catch a ball.
#define CATCH_SERVO_OPEN_MIN 150 // Open just to drop the ball
You can see that we defined some angles that will be used in all the functions that use servo motors.

Task: set Your own value.

Define distance at which the robot should follow a wall, using a "define" instruction in mrm-robot-line.h file.