Lesson 82: Using min() Function
Learn how min() selects the smaller of two values for limits, protection rules, and safer control behavior.
Progress indicator
Lesson 82 of 82
Learning Objectives
- Understand what min() does mathematically and how it selects the smaller value.
- Learn min() syntax and use it to simplify conservative decision logic.
- Apply min() with sensor values when lower-wins behavior is required.
- Understand data-type conversion behavior when comparing mixed numeric types.
- Avoid common mistakes such as side effects in arguments and wrong helper-function choice.
Concept Explanation
What is min()
min() returns the smaller of two values. It is useful when your logic follows a lower-wins rule.
In embedded systems, min() helps keep behavior conservative by choosing the lower of two candidate values before output or safety checks.
min() Syntax
Basic syntax: min(a, b)
min(10, 25)returns10.min(sensorA, sensorB)returns the lower reading.- If both are equal, returned value is that same number.
Pattern example: safeValue = min(requestedValue, maxAllowed) for upper-limit protection.
How min() Works
- Evaluate both input arguments.
- Compare first value with second value.
- Return whichever value is smaller.
Conceptually similar to: (a < b) ? a : b.
Comparing Two Values
min() is perfect when you want conservative output, such as selecting the lower safe limit or weaker reading for protective behavior.
Real use: when two sensors estimate the same quantity, you may choose min() if your system must avoid over-driving actuators.
min() with Different Data Types
- Common with
int,long, and floating values. - Mixed types trigger implicit conversions.
- Using same type on both arguments gives more predictable behavior.
Beginner rule: cast intentionally when types differ, so comparison behavior is explicit and repeatable.
When to Use min()
- Selecting the lower of two sensor/limit values.
- Applying conservative control decisions.
- Building compact minimum-bound logic in loops.
- Limiting command/request values against a maximum-safe threshold.
Example Code
This example selects the smaller sensor value and maps it to LED PWM output.
const int LED_PIN = 2;
int sensorA = 620;
int sensorB = 480;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Simulate changing sensor values.
sensorA = sensorA - 35;
sensorB = sensorB + 30;
if (sensorA < 180) sensorA = 650;
if (sensorB > 700) sensorB = 260;
int selectedValue = min(sensorA, sensorB);
int pwmValue = map(selectedValue, 0, 700, 0, 255);
pwmValue = constrain(pwmValue, 0, 255);
analogWrite(LED_PIN, pwmValue);
Serial.print("sensorA: ");
Serial.print(sensorA);
Serial.print(" | sensorB: ");
Serial.print(sensorB);
Serial.print(" | min: ");
Serial.print(selectedValue);
Serial.print(" | pwm: ");
Serial.println(pwmValue);
delay(700);
}Example Code Explanation
- sensorA decreases while sensorB increases, creating changing comparison outcomes.
- Reset conditions keep both signals in a repeating training range.
min(sensorA, sensorB)selects whichever value is currently smaller.- Selected minimum is mapped from 0..700 to PWM 0..255 range.
- constrain() protects mapped output against out-of-bound results.
- analogWrite() applies safe PWM value to LED output.
- Serial output prints full state to verify selection and scaling behavior.
What Happens Inside
- Both argument values are evaluated.
- CPU performs numeric comparison between values.
- Function returns smaller value (or either one if equal).
- Returned minimum feeds mapping and output update logic.
- Hardware output therefore tracks the conservative/lowest source input.
Common Mistakes with min()
- Using min() where max() logic is actually required.
- Passing arguments with side effects (like increment/decrement) into min().
- Mixing types carelessly and assuming no conversion occurs.
- Using min() as a full clamp substitute instead of proper range limiting.
- Comparing values from different units without normalization.
Best Practices for min()
- Use simple variables as min() arguments for readable logic.
- Keep argument data types aligned for predictable comparisons.
- Log both compared values and chosen minimum during tuning/testing.
- Document why lower-wins behavior is correct for your control scenario.
- Combine min() with map()/constrain() when values drive hardware outputs.
Try it now
Open the simulator workspace and observe how min() selects the conservative sensor value.