Map Function, Constrain, FIR Filters, And Other Math Functions

Previous Tutorial Previous Tutorial Tutorial 33 Next Tutorial Next Tutorial
Map Function, Constrain, FIR Filters, And Other Math Functions
STM32 Course Home Page ????


Map Function, Constrain, And Moving Averge Filters and MATH


In this article, we’ll discuss some math functions like map function, constrain, digital filtering, and how to implement them in Embedded-C. We’ll be using these functions in the upcoming tutorials and you’ll find them in the math directory in the STM32 course repo. So I’ve decided to briefly mention each of them at least the ones we’ll be using in the near future. And let’s dive right in!

   Map Function & Implementation In C   


Let’s start with the map function which we quite often use in different embedded systems applications. The map function is commonly used as a built-in Arduino C function and it’s very handy in a wide range of applications. Mathematically, the mapping function is to find the corresponding value from a certain domain to another domain. Here is an example of this.

We can use a potentiometer with an ADC to read the analog voltage and if the ADC is 10-Bit in resolution then its digital output is going to be in this range (domain) [0 up to 1023]. If we’re willing to use this value as a control signal to position a servo motor whose angle has a range (domain) of [0° up to 180°], then we need to do a mapping from the first domain to the second one. As you can see in the diagram below.

Map function implementation in C - Arduino

The map function does this conversion ting from the ADC result to the servo motor angle. The function will simply figure out the conversion ratio from domain1 to domain2, does the arithmetic, and return the result.

I personally used to do this manually back in the days in process control exams. The procedure was as follows: first of all, I’ll draw both domains and their ranges on top of each other like in the diagram below. The first one (from 0 up to 1023) and the second one (from 0 up to 180). Then, I take an arbitrarily chosen point (x) which represents the ADC reading at any time instance. And define the goal to be finding out the corresponding point (y) in the other domain that maps to the x point.

With keeping in mind the fact that the length of the (orange) bars are equal and the ratio between each one of them to its full domain range is also equal. Therefore, x/1023 = y/180. Which results in the following equation which we also call the (map function) or equation y=(x.180)/1023

Map function In Arduino example and explanation and implemetnation in C language

Here is an implementation for the map function in C language which we’ll add to our MATH.c source code in the MATH directory. You’ll find it also in the course’s repo on GitHub.

We’ll implement this example at the end of this article as well. To give you an example application code for how to include and use the MATH functions that we’ll be implementing today.


  Constrain Function  


The constrain function is a very simple logical operation. We typically perform this function on sensors’ readings when want to hard-limit or cap the readings of a specific sensor and also define a minimum output value to be accepted by the system.

Using this function guarantees that any digital controller or calculations performing mathematical operations on a specific sensor’s reading would not go wrong. As it’s guaranteed to fall in the specified range by using this function.

Here is a very simple implementation for this function in C language.


   Min & Max Functions   


The min & max functions are commonly used in different applications. And the task required by each of them is very basic and simple. It’s a searching routine that goes through an array of values to find the maximum or minimum and return it back.

Sometimes you may have a system that records some variables or sensors’ readings and would like to find the maximum or minimum peaks or drops. Or maybe use this information to know if your data needs some sort of digital filtering or not. In case of high spikes in the readings recorded.

Here is an implementation in C for both functions.

Note that the data type being used here is defined so that the user (the programmer) can adjust it if the variable being monitored is a floating-point number of signed values that can go negative. In case of having negative numbers, you have to be careful in the MIN function as defining the min value at the beginning with 0 may not converge to the right result.


   Moving Average FIR Digital Filter   


I’ll discuss this topic in detail in the next tutorial and show you how to create a simple yet effective digital filter in order to eliminate the push-button bouncing issue. A moving average filter is a very simple special case of the general Finite Impulse Response (FIR) digital filters.

Simple, you’ll save the most recent N values of a sensor’s reading. And constantly add a new reading, remove the oldest one, and calculate the average by summing all readings in the array and divide by N. Now you have got the average value and it’ll be way smoother than the original sensor’s reading.

For example, the IMU (gyro + accelerometer) module MPU6050 which we’ve discussed in detail in a previous tutorial. This sensor’s output is very noisy in nature every when you fix the sensor in place with no motion at all. Here is an example for the output data (the raw data from the sensor) and the same data points but after applying this simple moving average filter (with an order of 20).

Raw Data From The Accelerometer Sensor (in Blue)

Filtered Sensor Data with AVG Filter order 20 (in Red)

Map Function In Arduino Example And Moving Average Filters


 Map Function Example Application – LAB 


In this LAB, we’ll be using the MAP function with the ADC readings from a potentiometer (12-Bit ADC). To control a servo motor’s angle (0 to 180). This example was exactly performed in a previous tutorial (Servo Motor Control) but without having the map function implemented in code. So here is an example application for it.

The Application Code For This LAB (main.c)

We give the function the input (AD_RES) from the ADC reading that is 12-Bit in resolution so its range is from 0 up to 4096. And the target range from 0 up to 180. And that’s it! the map function will calculate and return the result as Angle and we’ll send it to the Servo_MoveTo function.

Here is the result for this LAB

The same as the previous servo motor LAB that I highly recommend checking it out if you didn’t yet (servo motor control tutorial – LAB27).

Download This LAB Project Folder

Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!



Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!



Previous Tutorial Previous Tutorial Tutorial 33 Next Tutorial Next Tutorial


Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment