[Diaoye learns programming] Mathematical operation min() in Arduino manual

insert image description here
insert image description here
What is an Arduino?
Arduino is an open source electronic prototyping platform that allows you to create a variety of creative projects with simple hardware and software. Whether you're a beginner or an expert, Arduino offers you endless possibilities. You can use Arduino to control sensors, lights, motors, robots, IoT devices, etc., as long as you can think of it, Arduino can help you achieve it.

If you want to learn more about Arduino, you can visit the official website of Arduino, where there are rich resources and tutorials for your reference. You can also join the Arduino community and exchange ideas and experiences with enthusiasts, students, designers and engineers from all over the world. In addition, you can also use Arduino's online programming tool to write code in the cloud and upload it to your development board.

Arduino is a constantly evolving and innovative platform with a wide range of applications and potential. I hope this manual can stimulate your interest and enthusiasm for Arduino, and let you enjoy the creativity and fun brought by Arduino

insert image description here

Wikipedia's definition
Arduino is an open source embedded hardware platform for users to make interactive embedded projects. In addition, Arduino, as an open source hardware and open source software company, has both projects and user communities. The company designs and manufactures Arduino boards and related accessories. These products are distributed under open source hardware and software licensed under the GNU Lesser General Public License (LGPL) or the GNU General Public License (GPL). Arduino allows anyone to build Arduino boards and distribute software. Arduino boards are sold commercially pre-assembled or purchased as DIY kits.

Introduced in 2005, the Arduino was designed as a student at the Ivrea Institute for Interactive Design in the Ivrea region of Italy, with the aim of providing novices and professionals with a low-cost and easy way to build interactive devices using sensors to interact with the environment. installation. Beginners and hobbyists can use Arduino to create devices such as sensors, simple robots, thermostats, and motion detectors.

The name Arduino comes from a bar in Ivrea, Italy, where some of the project's founders used to hang out. The bar is named after Arduin of Ivrea, count of the frontiers of Ivrea and king of Italy from 1002 to 1014.

insert image description here

17. Arduino math operation min()
Arduino math operation min() is a function used to return the smallest value among two or more values. Its syntax is:

min(x, y);

or

min(x, y, z, ...);

Among them, x, y, z, ... are values ​​of any type, which can be integers, floating-point numbers, characters, etc. For example, min(3, 5) returns 3, min(3.14, 2.72) returns 2.72, and min('a', 'b') returns 'a'.

The scope of use of the min() function is mainly when values ​​need to be compared or limited. For example, if you want the value of a variable not to exceed an upper limit, you can use the min() function to achieve it. For another example, if you want to find the minimum value in a set of data, you can use the min() function to achieve it.

Application scenarios:
1) Minimum value selection: The min() function is often used in scenarios where the minimum value needs to be selected from multiple values. For example, in sensor data processing, it may be necessary to select the smallest measured value as valid data, or to select the smallest error value for adjustment in a control loop.
2) Range limitation: The min() function can be used to limit the range of values. By comparing a numerical value to the upper limit, you can ensure that the numerical value does not exceed the specified upper limit value. This is useful for things like controlling output ranges, limiting sensor readings, or preventing overflow.
3) Numerical sorting: In some sorting algorithms, it may be necessary to compare multiple values ​​and select the smallest value. Use the min() function to conveniently select the minimum value during sorting.

Notes for the min() function are as follows:
1) The min() function can accept any number of parameters, but at least two parameters are required. If there is only one parameter, or if there are no parameters, it will cause a compile error.
2) The min() function will return the result of the corresponding type according to the type of the parameter. If there are values ​​of different types in the parameters, an implicit type conversion will be performed first, and then compared. For example, min(3, 5.0) will first convert 3 to a float of 3.0, and then return the float of 3.0.
3) The min() function will compare parameters sequentially from left to right. If there are multiple arguments with the same minimum value, the leftmost argument is returned. For example, min(3, 5, 3) returns 3, and min('a', 'b', 'a') returns 'a'.

The following are three practical application cases of the Arduino mathematical operation min():

Case 1: Let an analog input signal not exceed 500

// 定义模拟输入引脚
#define ANALOG_PIN A0

// 初始化函数
void setup() {
    
    
  // 设置串口通信波特率为9600
  Serial.begin(9600);
}

// 循环函数
void loop() {
    
    
  // 读取模拟输入引脚的电压值(0-1023)
  int value = analogRead(ANALOG_PIN);
  // 使用min()函数将电压值限制在0-500之间
  value = min(value, 500);
  // 将电压值打印到串口
  Serial.println(value);
}

Case 2: Let an LED light adjust the brightness according to the light intensity of the photoresistor

// 定义LED灯连接的引脚(支持PWM)
#define LED_PIN 9
// 定义光敏电阻连接的引脚(支持模拟输入)
#define LDR_PIN A0

// 初始化函数
void setup() {
    
    
  // 设置LED灯引脚为输出模式
  pinMode(LED_PIN, OUTPUT);
}

// 循环函数
void loop() {
    
    
  // 读取光敏电阻引脚的电压值(0-1023)
  int value = analogRead(LDR_PIN);
  // 使用map()函数将电压值映射到0-255之间(PWM范围)
  value = map(value, 0, 1023, 0, 255);
  // 使用min()函数将亮度值限制在0-200之间(避免过亮)
  value = min(value, 200);
  // 设置LED灯引脚的PWM值(0-255),控制亮度
  analogWrite(LED_PIN, value);
}

Case 3: Find the minimum value in an array

// 定义一个数组
int array[] = {
    
    10, 20, 30, 40, 50};
// 定义数组的长度
int length = sizeof(array) / sizeof(array[0]);
// 定义一个变量用于存储最小值
int min_value;

// 初始化函数
void setup() {
    
    
  // 设置串口通信波特率为9600
  Serial.begin(9600);
  // 将数组的第一个元素赋值给最小值变量
  min_value = array[0];
}

// 循环函数
void loop() {
    
    
  // 遍历数组中的其他元素
  for (int i = 1; i < length; i++) {
    
    
    // 使用min()函数比较当前元素和最小值变量,将较小的值赋值给最小值变量
    min_value = min(min_value, array[i]);
  }
  // 将最小值变量打印到串口
  Serial.println(min_value);
}

Case 4: Minimum value selection:

int sensor1Value = analogRead(A0); // 读取传感器1的值
int sensor2Value = analogRead(A1); // 读取传感器2的值

int minValue = min(sensor1Value, sensor2Value); // 选择最小的传感器值

// 在LCD显示屏上输出最小值
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
lcd.begin(16, 2);
lcd.print("Min Value: ");
lcd.print(minValue);

In this case, the min() function is used to compare the readings of two sensors and select the smallest sensor value. Then, display the minimum value on the connected LCD display.

Case Five: Scope Limitation:

int inputValue = analogRead(A0); // 读取输入值
int minValue = 0;
int maxValue = 1023;

int constrainedValue = min(maxValue, max(minValue, inputValue)); // 限制输入值在最小值和最大值之间

analogWrite(9, constrainedValue); // 输出限制后的值到PWM引脚

In this case, use the min() function to clamp the input value between the minimum and maximum values. This can be used to scope the input to ensure it is within a certain valid range. Then, output the limited value to the pin as a PWM signal.

Case 6: Numerical sorting:

int value1 = 5;
int value2 = 3;
int value3 = 7;

int minValue = min(min(value1, value2), value3); // 从三个数值中选择最小值

Serial.print("Min Value: ");
Serial.println(minValue);

In this case, three values ​​are compared using the min() function and the smallest value is selected. Then, output the minimum value to the serial monitor for debugging or display purposes.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132633922