Arduino

# Get Minimum and Maximum Value of an Arduino Array Using min() and max() Function

The min() and max() functions are two of the most useful functions in Arduino, which are used to find the minimum and maximum value in an array respectively. In this article, we will discuss the use of these functions and how you can use them to find the minimum and maximum value of an Arduino array.

## Introduction to min() Function – Arduino

The min() function is a built-in function in the Arduino programming language. It’s used to return the smallest value among two or more values. min() takes two or more arguments and returns the smallest value.

Syntax
The syntax of the min() function is quite simple. It takes two or more arguments as input and returns the smallest value. Here is the syntax of the function:

min(x, y)

## Parameter

The function can take two or more arguments, and it returns the smallest value among them.

x: This is the first number to be compared. It can be of any data type.

y: This is the second number to which the first will be compared. It can be of any data type.

Any data type can be used for arguments, like integer or float.

## Returns

The min() function compares the two arguments and returns the smallest one. Function arguments determine the data type of the returned value.

Note: Don’t use any other function inside the min() function brackets as this may lead to incorrect output results.

## Example Code

Following code explains use of min() function in Arduino code:

int a = 10;
int b = 5;
int c = min(a, b); // set c to the minimum of a and b
void setup() {
Serial.begin(9600); // initialize serial communication
Serial.print("Minimum of 10 and 5 is: ");
Serial.println(c); // print the value of c to the serial monitor
}
void loop() {
}

This code initializes two variables a and b with values of 10 and 5, respectively. The min() function is then used to set the value of c to the minimum of a and b, which is 5. The value of c is printed using the Serial.println().

Output
In output we can see a minimum of two numbers that is 10 and 5.

## Introduction to max() Function – Arduino

The Arduino max() function is a mathematical function that is used to determine the highest value in a set of given values. It returns the highest value among two or more input values.

Syntax
The syntax of the Arduino max() function is as follows:

max(x, y)

## Parameter

The max() function takes two or more parameters, which are the values to be compared to determine the highest value.

x: This is the first number to be compared. It can be of any data type.

y: This is the second number to which the first will be compared. It can be of any data type.

## Returns

The max() function returns the highest value among the set of given values. Return values will have the same datatype as input values.

Note: Don’t use any other function inside the min() function brackets as this may lead to incorrect output results.

## Example Code

Following code explains use of max() function in Arduino code:

int a = 10;
int b = 5;
int c = max(a, b); // set c to the maximum of a and b
void setup() {
Serial.begin(9600); // initialize serial communication
Serial.print("Maximum of 10 and 5 is: ");
Serial.println(c); // print the value of c to the serial monitor
}
void loop() {
}

Above code initializes two variables a and b with values of 10 and 5, respectively. The max() function is then used to set the value of c to the maximum of a and b, which is 10. The value of c is printed using the Serial.println().

Output

In output we can see a maximum of two numbers that is 10 and 5.

## Arduino Code to Get Minimum and Maximum Value of an Arduino Array Using min() and max() Function

Here is a simple Arduino code that uses the min() and max() functions to find the minimum and maximum values in an array:

void setup() {
Serial.begin(9600); // Initialize serial communication
int myArray[] = { 1, 2, 3, 4, 5 }; // Define the array with 5 elements
int minValue = min(myArray[0], myArray[1]); // Initialize the minimum value with the first two elements
int maxValue = max(myArray[0], myArray[1]); // Initialize the maximum value with the first two elements
// Loop through the rest of the elements to find the minimum and maximum values
for (int i = 2; i < 5; i++) {
minValue = min(minValue, myArray[i]);
maxValue = max(maxValue, myArray[i]);
}
// Print the minimum and maximum values
Serial.print("Minimum value: ");
Serial.println(minValue);
Serial.print("Maximum value: ");
Serial.println(maxValue);
}
void loop() {
}

In this code, we define an array myArray with 5 elements. We then initialize the minValue and maxValue variables with the first two elements of the array using the min() and max() functions.

We then use a for loop to iterate through the rest of the elements in the array and update the minValue and maxValue variables accordingly using the min() and max() functions.

Finally, we print the minValue and maxValue variables using the Serial.print().

Output
In output the minimum and maximum value from the array is printed.

## Conclusion

The min() and max() functions in Arduino can find the minimum and maximum values in an array. By initializing the minimum and maximum values and then using a for loop to iterate through the rest of the elements, we can easily find the maximum and minimum values from any array. For detailed description of min() and max() function read the article.