0

Soccer Robot C - Basic lessons - Variables and assignment

By now we managed to avoid code that would need variables but we cannot go this way anymore. Variables are places that can remember something, like a number or a string. Numbers are: 1, -3, 4.56, or 3.14e-10 (exponential notation). Strings examples are "day", "Computer", etc. Strings are always enclosed in double quotes and numbers never are. Therefore, "3.14" and 3.14 are not the same thing, but a string and a number.
Before using a variable, we must declare it. That means, tell the computer which kind of data it will store, like a string or a certain type of number. For example, let's instruct the program to make a variable which will store integers, numbers that have no decimals:
int aNameYouChoose;
aNameYouChoose is the name You give to the variable. This command does nothing else than reserving a space for one integer, that can be referenced by the chosen name.
We can store content in a variable in a process that is called assignment. An example:
void RobotSoccer::loop() {
	int i = 7;
}
Here, a combined command is used that first declared a variable named "i", that will store integer numbers ("int"), and, a new part, assigns a value (7) to the variable. Sign "=" means assignment. How long will variable "i" exist? Some variables live quite long, but our "i" is not so lucky. As soon as the function loop() completes, in a fraction of the second, it will be destroyed. After the loop() starts executing a fraction of the second later, a new variable, with the same name, will be created, and so on. With the variable, the content is destroyed, too. Look at this example:
void RobotSoccer::loop() {
	int i;
	i = 8;
}
Variable "i" is created. The next instruction executes, storing 8 in the variable, so now "i" is 8. The loop() completes and "i" is destroyed. Right after that, loop() starts executing again with "int i", creating a new variable "i". The question is, what will be stored in "i", before the next instruction executes? We may wish it is 8, but that will not happen. The variable with content 8 was destroyed and the program has no idea about the previous content. The right answer is that the content of "i" will be undetermined here.
It is obvious that often it is desirable that this variable isn't destroyed each time as we will have difficulties in conveying information between different executions of the same function. C++ has a remedy for this problem in a form of keyword static. In this context static means "do not destroy the variable that follows". The same example again, this time with "static".
void RobotSoccer::loop() {
	static int i;
	i = 8;
}
Now, as "i" is not destroyed, in the next run the value of "i" will be again 8 right in the beginning of the loop() function.
Let's use the acquired knowledge to make a useful program fragment. We may want the robot to go for 3 seconds straight ahead and then stop. Without variables, it will be very difficult to do. We must mark (and store) the time when the motors are started, let it go ahead till the stopwatch shows 3 seconds elapsed, when the motors should stop. Therefore, we have to remember (store) the start time. The place to store it is a variable. The program:
void RobotSoccer::loop() {
	static uint32_t startMs;
	if (setup()){
		startMs = millis();
		go(50, 0);
	}
	if (millis() - startMs > 3000)
		end();
}
The first line declares a very big integer (uint32_t is the name of this type). In the first run (when setup() is "true"), number of milliseconds is stored in variable "startMs". The motors are started. In each run elapsed time in milliseconds (millis() - startMs) is compared against 3000 ms. When the time elapses, the second "if" evaluates "true" and the next 2 commands execute, ending the function. "end()" will also stop the motors.

Task: delayed start.

Write a program which instructs the motors to drive the robot straight ahead after initial 2 seconds pause.