Team 1370 Blue Charge

Middletown Robotics, Team 1370

Basic Programming in "C"

This is an introduction to some of the programming concepts and practices employed in the coding of your robot. It is far from a complete or comprehensive guide, so don't expect to learn everything right here.

NOTE: This is written for the "C" programming language. While many other languages use similar concepts, they often use different syntax and structure. "C++" is a newer version of "C", so students who have learned "C++" will have a leg up on those learning other programming languages.

Contents

Variables

A variable is a spot in computer memory that holds some form of information. In math terms, think of it as "x" or "y" or "n". You use variables to hold any information that needs to be kept track of, but is subject to change (and thus varying).

Variables come in different forms. "int" variables can only hold integers (whole numbers). "char" variables can only hold a single character (like the letter A, for example) or any integer from 0 to 255. There are other kinds of variables as well. Variables also have a limit to their size, which if exceeded can result in errors or very strange program function. This is due to the binary nature of computer memory.

Variables are given information through assignment. Assignment uses the = sign operator. For example:

	int Num;		//Creates an "int" variable named Num
	int Data = 5;		//Assigns the value 5 
				//  to an new int variable named Data

As used in coding the robot, there are a few different keywords that you will need to be aware of. "signed" is used to mark a variable that can hold both positive and negative values, with its maximum range of values being split evenly between the two. "unsigned" is the opposite, only able to hold positive values but at the same time can hold twice the range of positive values as a "signed" variable.

	signed char Alpha;		//Can hold values from -127 to +127
	unsigned char Beta;		//Can hold values from 0 to +255

One kind of variable not mentioned so far, but used very often, is a "double". These can hold values with decimal places, and thus are called floating-point numbers (the decimal place can "float" around depending on what is needed). Doubles take up more memory space than "integer" or "char" variable types, so they should only be used when needed.

Constants

A constant is written in a similar way to a variable, except that the initial value assigned to it is permanent. Constants cannot be reassigned values. They are useful for clarifying code and for making it easy to use a single, easily changeable value in multiple places. For example:

	const double PI = 3.14;
	double Radius;
	double Area;
	
	Radius = 10;
	Area = PI * Radius * Radius;	// Area = 314.0
	
	Radius = 12;
	Area = PI * Radius * Radius;	// Area = 452.16

Comments

Programmers use comments to provide textual explanations of their code. They help other programmers understand what the code is doing and help you edit or change code in the future. There are two kinds of comments used in C programming. The first is a single line comment, which uses a double forward slash // . Any text entered after the commenting symbol on the same line is skipped by the compiler that turns your code into a working program. The other kind of comment uses /* and */ such that anything between the two symbols is "commented out". Multi-line comments are useful if you need to explain in detail how a particular aspect of the program is supposed to work.

// A single line comment

/* A multi-line
	comment		*/

Functions

A function is one of the basic components to a program or application. Functions are blocks of code that perform tasks. "Calling" a function executes the code inside the function. A simple function may look as follows:

int GetNumber (void) {
	int Num = 5;
	int Num2;
			
	Num2 = Num * 2;		//Num2 is given a value of 10; (5*2 = 10)

	return (Num2);
}

This function creates two variables, Num and Num2, and assigns a value of 5 to Num. It then multiplies the value of Num (5) by 2, giving a value of 10, which assigned to the variable Num2. At the end, the function "returns" the value of Num2, meaning that the function acts as a variable with a value of Num2. See the example below.

int GetNumber (void) {
	int Num = 5;
	int Num2;
			
	Num2 = Num * 2;

	return (Num2);
}

int X;
X = GetNumber();	/* Assigns the value of the function GetNumber 
			to the variable X. In this case, GetNumber = 10,
			so X will be set to 10. */

We will cover functions in-depth in later tutorials. Don't worry about them just yet.

Control Structures

"if...then..."

You can't do very much in a program if you can only assign variables different values and output the results. This is where control structures come into play. You can understand the concept of control structures easily if you imagine yourself holding a pencil above the ground. If you let go of the pencil, the pencil drops. Otherwise, it stays in your hand. This is a real-world example of an "if...then...else..." statement. See the example.

int Num = 6;

if (Num == 5){		// The "==" operator performs a comparison. If the value
	Num = 10;	// on the left is exactly the same as the value on the 
			// right, the program performs whatever actions are inside 
			// the curly braces "{}". This is the "if...then..." part.
}
else{			// An "else" is an optional statement that provides a 
	Num = 15;	// course of action when the first part of the if 
}			// (the Num==5 part) is untrue. The program will do  
			// everything inside the curly braces. */

In the code above, the value of Num is compared to the value of 5. Since Num = 6, the first set of curly braces are skipped (6 is NOT EQUAL to 5). Since there is an "else" statement, the code inside the else is executed. In this case, Num is given a value of 15.

There are all kinds of different operators that you can use instead of "==", too. The "<" (greater than) and ">" (less than) operators can be used, alongside their counterparts "<=" (greater than or equal to) and ">=" (less than or equal to). The "!=" (NOT EQUAL to) operator is like the opposite of the == operator, returning true when the values on its left and right are not equal.

"while..." loops

There are also different forms of control structures. "if...then..." statements can become "if... then...else..." or "if...then...elseif...else...", getting larger and more complex to deal with more difficult or complex scenarios. Another control structure is called a loop. A loop is basically a set of instructions that repeat over and over until a certain condition is met. The following is an example of a "while" loop.

while (condition){	// The "while" statement defines a specific type of 
	statements	// loop. It will perform the actions inside the brackets 
}			// over and over only while the condition is true.

int Num = 0;

while (Num < 5){
	Num = Num + 1;	// This loop will repeat while Num is less
}			// than 5).

At the end of this loop, the value of Num is 5, because the loop keeps adding 1 to Num, but when Num is 5 the loop doesn't run (Num is 5, not less than 5).

Continue the tutorial here

Programming Resources

Here is a link to the current version of our Robot Code 2006, which is a good example of various programming techniques we use.

For more programming tips, check out the Team 1370 wiki at: http://team1370.webhop.net