In this section, we'll look at how to use different mathematic functions and methods in Arduino.

Transcript

Everything we've discussed so far has been about functions, loops, and using words, and that's just the way that programming languages make the lower-level functions and commands easier to understand. However, everything that a microcontroller does is based on maths. Not the maths that you or I might use day to day, of course, but binary maths and bit manipulation. This makes it fairly straightforward for microcontrollers to perform traditional mathematical operations such as addition, multiplication, subtraction, and division, through to more complex stuff such as trigonometry. However, microcontrollers deal with integers, whole numbers remember. They don't like fractions or numbers with decimal places, which are called floating point numbers.

Now you can, of course, create variables that are float variables, and these allow you to use decimal numbers for that particular variable, but remember that these will take up much more space and require more processing power and time when performing calculations with floating point numbers versus standard integers. Whilst there are libraries for dealing with more advanced and complex maths functions, as we mentioned before, you can also write them out yourself. But first, let's take a look at some of the basic functionality plus some tips and tricks along the way that you can use to create maths operations for your project. Multiplication, division, addition, and subtraction all work in the same way, with an asterisk denoting multiplication and a forward slash for division, plus the normal plus and minus signs for addition and subtraction.

In programming, these symbols are called operators, and Arduino will follow the same ordering conventions as traditional mathematics, with the brackets controlling the order in which operations are applied. Something that people starting out with Arduino often miss is that you can also define a variable using itself, as we've already done so far in the course. Now let's take a look at how we can use these. So here we've got a fairly simple sketch. We've defined two variables, x and y. Now x is equal to 10, and y is equal to 3. And I'm initializing the serial monitor so we can print the output values and see what's going on.

So in the loop, I'm going to create int I and set I as equal to x plus y for the start, as we'll look at some addition. Now serial print this on a line, and delay for a second. So here is a basic piece of code that we're going to use to test out some of these math operations that we were talking about. I've created two variables, x and y, and x is going to be equal to 10, and y is going to be equal to 3. After initializing the serial port, as I'm sure you're familiar with by now, I'm going into our loop and creating an integer variable called i, which is going to be equal to x plus y. Now we'll print the value out, wait for a second, just for sanity's sake, and see what happens.

So if we upload this to our board, after waiting for it to compile and upload, we can go to the serial monitor and see our output, and we get 10 plus 3 is of course 13. Fantastic! Let's try subtraction. Get, again, 7. 10 minus 3 is of course 7. Fantastic! So, what about if we try multiplication? We use the asterisk as mentioned before, open up our serial port, and we get an outcome of 30, as expected. Now let's try division and see what happens. Now if you pick up on this at the moment, 10 divided by 3 isn't a whole number, so let's wait and see what our Arduino spits out. It spits out 3, which is the closest integer value to 10 divided by 3, which of course you know is 3.333, repeated.

So what about if we change this to a floating type variable? So we go float i. Now float I is useful because int x and int y are just regular integer types, not a problem. But float I will now perform the operation and output a floating type number. If we go here, we can see that it's printing out 3.000. Hmm, so a little strange. So we've got float I is equal to x divided by x and y, but these are integers, right? Well, let's see what happens if we change y to a floating type variable. Remembering that performing calculations with floating type variables is not super efficient. Not super efficient. And at last, we get 3.33, which we know is the outcome to two decimal places.

And this is because if two integers are involved in the operation, even if the outcome is a floating type variable, it's only going to give you an integer output. It just adds two zeros to the end as the decimal places. But if one number type in that is a floating type number, it will always give you a floating type output when we see the accurate result of 3.33, which is exactly what we want. Very good. Now, as we mentioned before, something that people often miss out on is you can define a variable again by itself. We've already used this in some previous sections and chapters, but I thought it necessary to go over it again just to show the point.

Now, of course, we could use, let's use y. y is already a float type variable. So we can say that y is equal to x divided by y, which will perform that function. And now y will always be equal to whatever the new value is. So what happens if we upload this? Let's have a look. So I is not declared in the scope. So of course we need to change our serial output to y because we haven't declared I as a variable. So it's uploading. Now let's go back to our serial monitor. We can see there's 3.33 and there's a few odd things happening because y is alternating between 3.33. And of course, then it's dividing again by 10 and gives you a perfect three.

So what's happening here is at first y is equal to three. So it says, all right, well, y is equal to 10 divided by three, which gives you 3.33. So now y is because it's a global variable is now equal to 3.33. And because of we only have two decimal places to work with, 10 divided by 3.33 gives you exactly three, which is why it's alternating between those outcomes. Really cool. So using those basic mathematic principles, you can easily introduce an extra layer of sophistication and power to your projects. And if you ever get stuck, I've included a really quick reference guide in the written content for you to take a look at and refer back to the syntax and styling for these different functions.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.