0

Soccer Robot C - 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 RobotSoccer::loop() {
	int startSpeed = INITIAL_SPEED;
}
will be replaced by
void RobotSoccer::loop() {
	int startSpeed = 50;
}
before compiling. Find C:\Users\[user name]\Documents\Arduino\libraries\mrm-robot\src\mrm-robot-soccer.h, in the same directory as mrm-robot-soccer.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, for example after line:
#include <mrm-robot.h>

Task: set Your own value.

Define distance at which the robot should stop, if an obstacle emerges, using a "define" instruction in mrm-robot-soccer.h file.