Learn the basics of programming using C/C++.
What is programming
Programming is the process of creating complex behaviour by giving simple instructions to a computer. It is the art of creating a program – a sequence of instruction which the computer should execute to achieve a specific task.
What is a programming language
A programming language is the means by which a programmer gives instruction to a computer. It is a framework given to programmer to be able to express how to resolve a given problem.
What is a compiler
Computers can only understand instructions in a specific, pretty cryptic language which is written directly in binary (zeros or ones written directly in a file). Since it would be pretty annoying to write directly in this language, we use programming languages (see above) to help us write programs. To translate the program to an executable (a binary file that can be executed by the computer), one must use a compiler. When the translation process is finished, the computer can read the instructions in its “native language”.
Some languages use what’s called an interpreter. Rather than translating the program one time and create an optimized executable, interpreters translate the program as it is run. Some languages that are aimed at fast prototyping (e.g. Python) prefer this method, as it is faster to try out for a developer. However, since this waste the processing power, Arduino based boards use compiled languages.
What are C and C++
C is a language that aims to translate from a simple language to the native instructions on the computer almost directly. When you write a program in C, the executed instructions map almost directly to the hardware.
The advantages it provides are:
Increased performance because of a tight control on the machine. On really small computers like the Arduino, this means that you don’t waste any processing power.
A common interface to different kind of computers. The binary language understood by computer changes. For example, it won’t be the same on a phone and on a laptop. A program written in C, on the other hand, will still work across those machines. Hence, you can learn C once and use it on pretty much every computer.
A big pain point however is that C was developed when computer were rather weak and as such programs were rather short. As such, it provides little to no standard way to organize a bigger program.
C++ aimed to expand the C language with constructs to increase ease-of-use and organisation for bigger programs. While technically the language used for the Arduino based boards is C++, the constrained environment doesn’t give much place for a lot of features. As such, we won’t be going into details about C++ vs C. However, keep in mind that some techniques won’t be available to you if you use plain C.
How is a CrcDuino programmed?
Programming a micro-controller board - here the CrcDuino - is a fairly repetitive process.
The code is written in, compiled and finally downloaded in the micro-controller using an Integrated Development Environment. Although the CRC Robotics Competition participants are free to use whatever Arduino compatible IDE, we strongly recommend (and only support) the official desktop-based Arduino IDE.
The lines of code are written using C/C++.
Your IDE then uses a compiler to transform your C/C++ lines of code into micro-controller instructions.
Once your code as been compiled (or verified in the Arduino world), your IDE then allow you to upload these instructions into your micro-controller.
Once the upload is done, your micro-controller (the brain of the 9880 board) reboots and then starts executing your code. It will run whatever is written in the
void setup()
part of your program once, and then it will execute over and over again whatever you wrote in thevoid loop()
part.You repeat step 2 to 5 until your robot does what you want it to do!
Primitives
Computers can natively only perform operations on some data types. These are integer and floating point (float) numbers. Those are called primitives. Integers are further divided in two classes, signed and unsigned. Signed numbers can represent negative as well as positive integers (signed = with a sign, get it?), while unsigned numbers can’t.
Here is a summary chart:
Primitive | Can it represent decimal numbers (e.g. 0.1)? | Can it represent negative numbers (e.g. -2)? |
---|---|---|
Floats | Yes | Yes |
Signed integers | No | Yes |
Unsigned integers | No | No |
While it could be tempting to always use floats, you should always strive to use the type that provides the least while providing what you need. By removing some possibilities, you can avoid small mistakes because of inattention. Computers are also generally faster with integers than with floats.
Precision and size
Computer, like humans, have a limited quantity of information they can hold in memory. Thus, each number has a finite number of space dedicated to it. You should always choose the type of a value depending on the range of values it can take. For example, using a variable that can hold values up to a billion would waste space if dedicated to a value that is at most a hundred.
For floats, you have two options:
float
which can hold values from 10-38 to 1038 with a precision of around 7 digitsdouble
which can hold values from 10-308 to 10308 with a precision of around 16 digits.
For integers, you have a wider range of choices. As a general structure you have int<size>_t
to specify a type, where <size>
can be 8, 16, 32, 64. For unsigned versions, you can prefix a u
to the type. The size indicates the acceptable values taken by a type. Specifically, int<size>_t
can take from -2(size - 1) to 2(size - 1) - 1 and uint<size>_t
can take from 0 to 2size - 1.
In a more understandable format:
Type | Negatives? | Size | Minimum | Maximum |
---|---|---|---|---|
uint8_t | No | 8 | 0 | 28 - 1 (255) |
int8_t | Yes | 8 | -27 (-128) | 27 - 1 (127) |
uint16_t | No | 16 | 0 | 216 - 1 (65535) |
int16_t | Yes | 16 | -215 (-32768) | 215 - 1 (32767) |
uint32_t | No | 32 | 0 | 232 - 1 (4294967295) |
int32_t | Yes | 32 | -231 (-2147483648) | 231 - 1 (2147483647) |
uint64_t | No | 64 | 0 | 264 - 1 (3.68935 × 1019) |
int64_t | Yes | 64 | -263 (-9.22337 × 1018) | 263 - 1 (9.22337 × 1018) |
0 Comments