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.
In Arduino IDE, compiling is known as verifying and code is written in .ino
files.
Once the upload is done, the CrcDuino reboots and then starts executing the code. It will run whatever is written in the void setup()
part of the .ino file
program once, and then it will execute over and over again whatever’s written in the void loop()
part.
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 digits;double
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 | Also known as in Arduino | Negatives? | Size (bit) | Minimum | Maximum |
---|---|---|---|---|---|
uint8_t | unsigned char or byte | No | 8 | 0 | 28 - 1 (255) |
int8_t | char | Yes | 8 | -27 (-128) | 27 - 1 (127) |
uint16_t | unsigned int or word | No | 16 | 0 | 216 - 1 (65 535) |
int16_t | int | Yes | 16 | -215 (-32 768) | 215 - 1 (32 767) |
uint32_t | unsigned long | No | 32 | 0 | 232 - 1 (4 294 967 295) |
int32_t | long | Yes | 32 | -231 (-2 147 483 648) | 231 - 1 (2 147 483 647) |
uint64_t | unsigned long long | No | 64 | 0 | 264 - 1 (3.68935 × 1019) |
int64_t | long long | Yes | 64 | -263 (-9.22337 × 1018) | 263 - 1 (9.22337 × 1018) |
When starting, you can use the “Also known as in Arduino” nomenclature, but you should try to understand and use the official integer nomenclature.
In the official nomenclature, the size of every int type is explicitely defined and the same on every platform, whereas an “int” might be 16bit on a micro-controller model and 32bit on another one. Using this nomenclature is a nice habit since it makes your code more portable; meaning that it will have the same execution characteristic on different platforms.
Add Comment