0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino cpplinq: Getting minimum and maximum values from array

DFRobot May 08 2019 1397

In this esp32 tutorial we will check how to get the maximum and minimum values of an array, using cpplinq. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we will check how to get the maximum and minimum values of an array, using cpplinq. We will be running the code on an ESP32, using the Arduino core.

For an introductory tutorial on how to install cpplinq as an Arduino library, please check here.

The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

The code

We will start our code by importing the cpplinq library and then declaring the use of the cpplinq namespace.

#include "cpplinq.hpp"
 
using namespace cpplinq;

Then we will move on to the Arduino setup. The first thing we will do is opening a serial connection, to output the results of our program.

After that, we will declare an array of integers containing some arbitrary values. This will be the array from which we will obtain both the maximum and minimum values, using cpplinq operators.

Serial.begin(115200);
    
int ints[] = {5,7,4,7,8,15,9,25,15,14,30,9,24,5,78,912,37,48,980,200,201};

Before we start applying the operators, we need to convert the array to a range object, like we have been doing in previous tutorials. This is done with a call to the from_array function, passing as input the array of integers.

from_array(ints)

Then, to obtain the maximum value of the array, we simply need to call the max operator. This operator takes no arguments and will return the integer with the maximum value from the array.

max()

To obtain the minimum value, we need to call the min operator, which also takes no arguments. This will return the minimum integer value from the array.

min()

In both cases, we need to use the >> operator between the call to the from_array function and the call to the cpplinq operator. In both cases, the expected return value is an integer.

int maxVal = from_array(ints)
               >> max();
 
int minVal = from_array(ints)
               >> min();

Finally, we will print both values we have obtained from applying these operators.

Serial.println(maxVal);
Serial.println(minVal);

The final complete code can be seen below.

#include "cpplinq.hpp"
 
using namespace cpplinq;
 
void setup() {
  Serial.begin(115200);
    
  int ints[] = {5,7,4,7,8,15,9,25,15,14,30,9,24,5,78,912,37,48,980,200,201};
    
  int maxVal = from_array(ints)
               >> max();
 
  int minVal = from_array(ints)
               >> min();
 
  Serial.println(maxVal);
  Serial.println(minVal);
}
  
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. When the procedure finishes, open the Serial Monitor. You should get an output similar to figure 1, which shows both the minimum and maximum values of the array getting printed.

Figure 1 – Output of the program, showing the maximum and minimum values of the array.

Related Posts
ESP32 Arduino: filtering arrays with cpplinq
ESP32 Arduino: getting started with cpplinq