...
Tip |
---|
This page introduces 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.
...
Info |
---|
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 Arduino based boards use compiled languages. |
What are C and C++
...
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 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.
...
Info |
---|
In Arduino IDE, compiling is known as verifying and code is written in |
Info |
---|
Once the upload is done, the CrcDuino reboots and then starts executing the code. It will run whatever is written in the |
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.
...
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.
...
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) |
Info |
---|
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. |
Related articles
Filter by label (Content by label) | ||||||
---|---|---|---|---|---|---|
|