Home ยป Arduino programming basics: Datatypes

Arduino programming basics: Datatypes

by admin
0 comment 705 views

In this series, I wanted to talk to you guys about the fundamentals of programming sketches for Arduino. This section in particular will mainly focus on the primary datatypes in C++/arduino (but not to the letter). If you feel confused already and wonder what datatypes are don’t worry I’ll start from the beginning.

If you already got the basic concepts of programming down this part may not be written for you. When you want to practice or follow along I’d recommend using the things you’ve learned on a regular basis to really master the topics you’ve study (but most importantly do it all at your own comfortable pace). The Arduino IDE where we make all our sketches can be downloaded from here: https://www.arduino.cc/en/software.

When we communicate with other people we make use of words and sentences that consist of letters or numbers. The same principles can be applied to the way we communicate with our Arduino sketches. If I presented the following conversation in real life you would probably think I’m going crazy:

Person A: Hi how are you doing? I just constructed a sentence made out of letters and send it over to your ears.

Person B: Thanks I Just received your sentence consisting out of characters. I am doing fine for the last 4 hours. I will send this message as a response consisting of letters.

Person A: What did you say? I only received: I am doing fine for the last ERROR hours.

Now if we had this conversation with a computer on the other hand things start to fall in place. Our computer has a more logical way of communication. We need to tell the computer what we’re sending. And besides the content, we also need to declare what the content is made of. The identifiers defining what kind of data is being used is called a datatype.

In our Arduino IDE (the program we write our sketches in) there are several basic data types that are widely used.

  • (Char)acter
  • (Char)acter[]
  • string
  • Integer
  • float
  • double

Char

A char is used to store a single (Char)acter. See where the name comes from? If we want to use a char we first have to initiate it. Initiating is the process of defining a variable and asigning it a type. In our case it could look like this:

char firstChar;

Now we’ve defined that the variable firstChar (which is a name we just made up so feel free to change this to anything you like) is of the type char. In C++/Arduino it’s important to end/close every line with a ;.

If we would print this char to our console an error will be thrown by the IDE. This is because firstChar is still empty. We could assign it a value using the following code:

char firstChar = 'A';
char secondChar = 'B';

We created 2 Chars with the names firstChar and secondChar. After the = sign we declared that firstChar represents the value ‘A’ (notice how we use single quotes surounding our chars) and the secondChar represents the value ‘B’.

We’re even able to print both strings by doing this:

char firstChar = 'A';
char secondChar = 'B';

Serial.println(firstChar + secondChar);
//The result would be AB

(when coding it’s possible to add a single line comment by starting the line with //). What would happen if we did the following?

char firstChar = '1';
char secondChar = '2';

Serial.println(firstChar + secondChar);

When we think about humanlike communication between two people we could say 1 + 2 = 3. But our computer knows that 1 is a textlike character

and 2 is a textlike character so 1 + 2 = 12 (it just adds the second char after the first).

String

First things first to please the hardcore C++ programmers. In C++ a string originates from an array of characters. Now the reason why I’m not talking about an array of characters in this chapter is that the next post will be dedicated to arrays.

If we take a look at our chars it would become a full-time job to write a message letter after letter. Hello would become:

char firstLetter = 'h';
char secondLetter = 'e';
char thirdLetter = 'l';
char fourthLetter = 'l';
char fifthLetter = 'o';

Serial.print(
firstLetter + 
secondLetter + 
thirdLetter + 
fourthLetter + 
fifthLetter);

//The result would be hello

Luckily there is another type we could use. Let me introduce the string. A string is a collection of characters stored in a single variable. Hello would simply become:

string firstWord = "Hello";

Notice how we capture our characters between double quotes for a string “” . And just like chars we’re able to combine string by using “Firstpart” + “Secondpart” = “FirstpartSecondpart”. In order to create “Firstpart Secondpart” we could include a space after FirstPart or add it by using “Firstpart” + ” ” + “Secondpart”. And to create a complete picture “123” + “123” would become “123123” and not 246.

Int

Ok by now you probably want to strangle me for repeatedly bringing up the fact you cant use 2 strings or chars to add numbers. So now it’s time to actually start adding numbers. An Integer is a datatype which consists of a number without any decimals. So 1, 2, 3, 4, 5 are all valid Integers but 1.3 isn’t. 1.3 can be stored as an Integer but would automatically lose its decimal. An example:

int intA = 1;
int intB = 1.3;

int intC = intA + intB;
// the result of intC would be 2 since 1.3 gets changed to 1

A noticable difference is that his time we don not use any quotation marks whatsoever. Notice how we’re able to directly assign the vallue of intC by adding intA plus intB after the = sign. This works for most datatypes overal so the same could be used for strings, doubles or floats. With an integer we’re able to perform mathmatical operations like:

  • Divide by /
  • Multiply by *
  • Add +
  • Subtract –

Just keep in mind that the result of 7 / 2 would be 3 since the .5 is lost in translation.

Float

Let’s tackle that nasty little obstacle that prevents us from using decimal numbers. The float is a lighter version of the Double (we’ll get into this later). The float has a decimal precision of up to 7 digits after the dot. So 1.2449878 could be declared as a float:

float floatA = 1.2449878;
float floatB = 1.5;
float floatC = 2;

float floatD = floatB + floatC;

//the result of floatD = 4.8000000

So you might get angry at me. If a float can handle numbers without decimals. Why did you make us use Ints? Well, the answer would be speed and logic. Now, this used to be a bigger issue back in the days when floating-point operations would come with a big performance hit. Nowadays our computers improved so vastly this effect is mostly negated. This argument is also hardware and use-case dependent. For the logic part, we’re able to enforce certain values from either our user or our program. Let’s say we ask someone to pick a number between 0 and 10. A good friend would say something along the lines of 7, while that one friend that always tries to be the smartest in the room decides to share his favorite number is 1.7648. We could let our special friend just be special but we have a magic trick lined up that consists of 10 doors and you couldn’t be bothered to create 10000 doors to cater to our special friends’ love for decimal numbers. So what we could do is make sure we only accept Integers instead of floats.

Doubles

Last but not least we have doubles (or double floating point values). Our floats where precise up to 7 digits after the . but sometimes this just isn’t enough.

Let’s say we have a mission-critical value that needs to be as precise as we can get (in a simplified cenario). In these cases we dish out our arsenal of Doubles. A double has a whopping 15 decimal digits precision. The downside of this is once again size. Our float had a 32-bit precision while our double has a 64-bit precision. This would also increase the size to twice the size of a normal float. This could lead to an unnecessary increase in the size of our programs. This is why the double is mostly used when precision is absolutly required.

Using doubles would look like this:

double firstDouble = 1;
double secondDouble = 1.456678164821;

Finally

Well I hope you managed to pick up a thing or 2 about datatypes. Next post will mainly focus on arrays. In short arrays are variables stored in a container(the array). Why do we need this? Well I’ll get into that next time.

If you made it this far thanks for the read and I’ll see you next time.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept