Discover this podcast and so much more

Podcasts are free to enjoy without a subscription. We also offer ebooks, audiobooks, and so much more for just $11.99/month.

Data Types: One size does not fit all

Data Types: One size does not fit all

FromLearn Programming and Electronics with Arduino


Data Types: One size does not fit all

FromLearn Programming and Electronics with Arduino

ratings:
Length:
11 minutes
Released:
Mar 27, 2017
Format:
Podcast episode

Description

Discussion: We're going to explore some of the specific data types you'll encounter as you begin to program with Arduino.  We’ll tackle data types in more detail, to include: Integer and Integer Qualifier Long Float Byte Boolean Character Data Types Recap Let's review what we’ve learned in previous units.  We know that when we create a variable, we need to declare its data type.  We also know that a data type is simply a way to classify the types of information that you're going to put in a variable. Data type determines several things: what range of values can be stored in the variable how much space and memory that variable needs what types of operations can be performed on that variable Integer We'll begin our discussion of data types with the most common one that you'll see in this course - the integer.  This is abbreviated “int” when you type it into the Arduino IDE. The integer is really the go-to data type if you need to store a number.  It can hold a value from -32,768 all the way up to 32,767. Keep in mind that it only holds a whole number.  You can't store a decimal, such as 3.14, in a variable whose data type is integer. Also, an integer is a 16-bit value.  In other words, it takes 16 bits (or two bytes) of memory to store an integer. It doesn't matter if you put a small number in that integer variable like 10, or a big number like 30,000.  The compiler will always set aside two bytes for any integer, regardless of number size. In the grand scheme of things, that's pretty small and is why integers are our friends.  They don't take up that much space, even if you need to hold a very large value. Now, what happens if you try to put a number bigger than 32,767 inside an integer variable?  Well, it actually rolls over.  I’m sure most of you are familiar with Pac-Man. You know how if he goes off the screen, he comes right back on the other side?  The same thing happens with variables. Let’s say we had an integer variable storing 32,767.  Then, we add one to it.  The result will actually be -32,768, which is called rolling over.  Likewise, if you subtract one from -32,768, the result will be 32,767. This leads us to a really important point.  When you create a variable, you should have some idea about how big you expect the stored value will be.  That expected value will help you determine what data type you should use for a given variable. Remember, the key aspect of variables is that they change - they can vary.  At some point, you'll probably do calculations with those variables and change what is stored there based on your results. You can only put so much water in a bucket before it spills over.  Therefore, if the value in that variable at any time exceeds the size allow by its data type, it's going to roll over.  You can imagine that might return some really weird results. With this in mind, what happens if you need to store a number bigger than 32,767?  Well, you have a couple of options. However, before we abandon the integer for a data type that takes up more of our precious memory storage, let's talk about what signed and unsigned means. Integer Qualifier By default, an integer is signed.  This means that it accepts both positive and negative signs.  So when we say signed, we're talking about the fact that the value could be negative or positive. What if we know we won’t be working with negative numbers?  Our variable will never need to store negative values. For example, if I'm using a variable to store the number of days that have passed, there's never going to be a negative number of days.  The number is only going to increase from its starting point - unless, for all of you 80’s kids out there, I jump in a Delorean or something like that. If this is the case, we can use what is called a qualifier to make an unsigned integer.  This gets rid of all the negative values and shifts them into the positive range. To use the unsigned qualifier, all you have to do is write unsigned before the “int” data type a
Released:
Mar 27, 2017
Format:
Podcast episode

Titles in the series (61)

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or no experience in programming and electronics and get them fast-tracked to learning the skills to prototype using Arduino. We'll include some lessons from the first edition and the second edition of our training course.