19. Sensor Value Saturation

#include <iostream>
using namespace std;

// Inline function to clamp a value between minVal and maxVal
inline int saturate(int value, int minVal, int maxVal) {
    if (value < minVal) return minVal;
    if (value > maxVal) return maxVal;
    return value;
}

int main() {
    int value, minVal, maxVal;
    cin >> value >> minVal >> maxVal;

    cout << saturate(value, minVal, maxVal);

    return 0;
}

Explanation & Logic Summary:

Inline functions reduce call overhead for small, frequently used operations like saturation.

Saturation is widely used in embedded firmware to ensure values remain within safe and valid limits, such as:

  • ADC reading bounds
  • PWM duty cycle limits
  • Sensor value conditioning

The function enforces bounds safely and efficiently.

Firmware Relevance & Real-World Context:

Saturation prevents invalid or unsafe values from propagating due to sensor noise, arithmetic errors, or control-loop instability.

This pattern is common in:

  • Control systems and PID controllers
  • IMU and sensor data processing
  • ADC scaling and signal conditioning
  • Safety-critical and real-time firmware systems

 

 

 

 

 

Loading...

Input

120 0 100

Expected Output

100