[Diao Ye learns programming] Mathematical operation fmin() 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 mathematical operation fmin()
fmin() is a mathematical operation function in Arduino, which is used to compare the size of two floating-point numbers and return the smaller value. It takes two arguments: two floating-point numbers to compare, and returns the smaller number as the result. Its scope of application:
1) In the case of optimization or decision-making, use the fmin() function to find the worst or most unfavorable solution or plan. For example, if you want to choose one of two investment projects, you can use the fmin() function to compare their risk rates and choose the one with the lower risk rate.
2) In cases involving limits or constraints, use the fmin() function to ensure that the value does not go above a certain threshold or range. For example, if you want to control the speed of a motor, you can use the fmin() function to limit the speed to no higher than the maximum speed.
3) In the case of comparison or sorting, using the fmin() function can simplify the code or logic and avoid using if-else statements or loop structures. For example, if you want to find the minimum value in an array, you can use the fmin() function to compare the array elements one by one.

Main application scenarios:
1) Data processing and screening: In data processing, it may be necessary to compare the size of multiple floating-point values ​​and select the smallest value for further processing. The fmin() function can conveniently compare the size of two numbers and return the smaller value, which is used to filter and extract the minimum value in the data.
2) Control system and feedback mechanism: In a control system, it is often necessary to select an appropriate control strategy or action according to different input signals. The fmin() function can be used to compare the size of different parameters or measured values, and select an appropriate control strategy or action based on the minimum value.
3) Numerical analysis and optimization algorithms: In numerical analysis and optimization algorithms, it is often necessary to compare different numerical values ​​or the size of evaluation indicators, and make decisions or adjustments based on the minimum value. The fmin() function can conveniently compare the size of two numbers for determining the minimum value and related decisions.

When using the fmin() function, you need to pay attention to the following:
1) The fmin() function can accept integers or floating-point numbers as parameters, but the returned result type is the same as the parameter type. If the result is to be assigned to a variable of a different type, type conversion is required.
2) The fmin() function needs to accept two parameters, namely the values ​​x and y to be compared. If you want to compare the minimum value among multiple values, you need to call the fmin() function nestedly or use an array or loop structure.
3) The fmin() function uses floating-point arithmetic, so rounding errors or overflows may occur. If more precise or larger-scale calculations are required, other libraries or methods can be used.

The following are three practical application cases of Arduino mathematical operation fmin():
Case 1: Use temperature sensor and LCD display to realize temperature recording function. Read the temperature every once in a while, and display the current temperature and the lowest temperature in history. Use the fmin() function to update the historical minimum temperature.

// 引入LiquidCrystal库
#include <LiquidCrystal.h>
// 定义温度传感器和LCD显示屏的引脚
#define TEMP_PIN A0
#define RS_PIN 7
#define EN_PIN 8
#define D4_PIN 9
#define D5_PIN 10
#define D6_PIN 11
#define D7_PIN 12
// 创建LiquidCrystal对象,指定引脚顺序
LiquidCrystal lcd(RS_PIN, EN_PIN, D4_PIN, D5_PIN, D6_PIN, D7_PIN);
// 定义读取温度的时间间隔,单位为毫秒
#define INTERVAL 1000

void setup() {
    
    
  // 初始化LCD显示屏,并清屏
  lcd.begin(16,2);
  lcd.clear();
}

void loop() {
    
    
  // 定义一个静态变量存储历史最低温度,初始值为正无穷大
  static float min_temp = INFINITY;
  
   // 读取温度传感器的模拟值,并将其转换为温度值,单位为摄氏度
   int temp_value = analogRead(TEMP_PIN);
   float temp = temp_value * (5.0 / 1023.0) * 100.0;
   
   // 使用fmin()函数更新历史最低温度,如果当前温度小于历史最低温度,则替换之
   min_temp = fmin(temp, min_temp);
   
   // 在LCD显示屏上显示当前温度和历史最低温度,保留一位小数
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Temp: ");
   lcd.print(temp,1);
   lcd.print(" C");
   
   lcd.setCursor(0,1);
   lcd.print("Min: ");
   lcd.print(min_temp,1);
   lcd.print(" C");
   
   // 延迟一段时间,再次读取温度
   delay(INTERVAL);
}

Case 2: Use photoresistors and LED lights to realize the sound and light control function. When the photoresistor detects light, the LED lights up. The brightness of the LED light changes with the intensity of the light and has a linear relationship. Use the fmin() function to limit the brightness of the LED lamp to no higher than the maximum brightness.

// 定义光敏电阻和LED灯的引脚
#define LDR_PIN A0
#define LED_PIN 9
// 定义光敏电阻的读数的范围
#define LDR_MIN 0
#define LDR_MAX 1023
// 定义LED灯的亮度的范围,单位为PWM值
#define BRIGHTNESS_MIN 0
#define BRIGHTNESS_MAX 255
// 定义LED灯的最大亮度,单位为PWM值
#define BRIGHTNESS_THRESHOLD 200

void setup() {
    
    
  // 设置LED灯为输出模式,并初始化为低电平
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
    
    
  // 读取光敏电阻的模拟值,并将其映射到0-1023范围内
  int ldr_value = analogRead(LDR_PIN);
  ldr_value = map(ldr_value, LDR_MIN, LDR_MAX, 0, 1023);
  
   // 根据光敏电阻的读数计算LED灯的亮度,并将其映射到合理的范围内
   float brightness = (float)ldr_value / (LDR_MAX - LDR_MIN) * (BRIGHTNESS_MAX - BRIGHTNESS_MIN) + BRIGHTNESS_MIN;
   brightness = constrain(brightness, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
   // 使用fmin()函数限制LED灯的亮度不高于最大亮度,避免LED灯过亮或损坏
   brightness = fmin(brightness, BRIGHTNESS_THRESHOLD);
   // 使用ceil()函数将亮度转换为整数,并赋值给LED灯
   int brightness_value = ceil(brightness);
   analogWrite(LED_PIN, brightness_value);
}

Case 3: Use the temperature and humidity sensor and fan to realize the temperature and humidity control function. When the temperature and humidity sensor detects that the temperature and humidity are lower than the set threshold, the fan stops. The speed of the fan decreases with the decrease of temperature and humidity, and the relationship is linear. Use the fmin() function to calculate the temperature and humidity index.

// 引入DHT库
#include <DHT.h>
// 定义DHT传感器的类型和引脚
#define DHT_TYPE DHT11
#define DHT_PIN A0
// 创建DHT对象
DHT dht(DHT_PIN, DHT_TYPE);
// 定义风扇的引脚
#define FAN_PIN 9
// 定义温湿度的阈值和范围,单位为摄氏度和百分比
#define TEMP_THRESHOLD 20.0
#define TEMP_MIN 10.0
#define TEMP_MAX 30.0
#define HUMIDITY_THRESHOLD 40.0
#define HUMIDITY_MIN 20.0
#define HUMIDITY_MAX 60.0
// 定义风扇的转速范围,单位为PWM值
#define SPEED_MIN 0
#define SPEED_MAX 255

void setup() {
    
    
  // 初始化DHT传感器
  dht.begin();
  // 设置风扇为输出模式,并初始化为低电平
  pinMode(FAN_PIN, OUTPUT);
  digitalWrite(FAN_PIN, LOW);
}

void loop() {
    
    
  // 获取当前的温湿度,单位为摄氏度和百分比
  float temp = dht.readTemperature();
  float humidity = dht.readHumidity();
  
   // 使用fmin()函数计算温湿度指数,单位为摄氏度
   // 温湿度指数等于温度减去湿度乘以一个系数,公式为t_h = t - k * h
   // 其中t_h是温湿度指数,t是温度,h是湿度,k是一个系数
   float temp_humidity = fmin(temp, humidity * K);
   
   // 如果温湿度指数低于阈值,停止风扇
   if (temp_humidity < TEMP_THRESHOLD + HUMIDITY_THRESHOLD) {
    
    
     // 关闭风扇
     digitalWrite(FAN_PIN, LOW);
   }
   else {
    
    
     // 否则,根据温湿度指数计算风扇的转速,并将其映射到合理的范围内
     float speed = (temp_humidity - (TEMP_THRESHOLD + HUMIDITY_THRESHOLD)) / ((TEMP_MAX + HUMIDITY_MAX) - (TEMP_THRESHOLD + HUMIDITY_THRESHOLD)) * (SPEED_MAX - SPEED_MIN) + SPEED_MIN;
     speed = constrain(speed, SPEED_MIN, SPEED_MAX);
     // 使用floor()函数将转速转换为整数,并赋值给风扇
     int speed_value = floor(speed);
     analogWrite(FAN_PIN, speed_value);
   }
}

Case 4: Find the minimum value:

float a = 12.5; // 数字a
float b = 18.9; // 数字b
float minNum = fmin(a, b); // 比较a和b,得到较小的值

void setup() {
    
    
  Serial.begin(9600);
}

void loop() {
    
    
  Serial.print("较小的值:");
  Serial.println(minNum);
  delay(1000);
}

In this example, we use the fmin() function to compare two numbers a and b and return the smaller value. This way we can find the minimum of these two numbers.

Case five: choose the minimum value:

float value1 = 5.7; // 数值1
float value2 = 9.2; // 数值2
float threshold = 7.0; // 阈值
float selectedValue = fmin(value1, value2); // 比较value1和value2,得到较小的值

void setup() {
    
    
  Serial.begin(9600);
}

void loop() {
    
    
  if (selectedValue < threshold) {
    
    
    Serial.println("选择的值小于阈值");
  } else {
    
    
    Serial.println("选择的值大于或等于阈值");
  }
  delay(1000);
}

In this example, we use the fmin() function to compare two numeric values ​​value1 and value2 and select the smaller value. According to whether the selected value is smaller than the threshold value, corresponding judgment and processing are carried out.

Case 6: Simulation Simulation:

float input1 = 3.2; // 输入1
float input2 = 4.8; // 输入2
float output = 0.0; // 输出

void setup() {
    
    
  Serial.begin(9600);
}

void loop() {
    
    
  output = fmin(input1, input2); // 比较输入1和输入2,得到较小的值
  Serial.print("输出:");
  Serial.println(output);
  delay(1000);
}

In this example, we use the fmin() function to compare two input values ​​input1 and input2 and select the smaller value as output. This allows signals under different input conditions to be processed in analog simulations.

Case 7: Data processing:

#include <math.h>

float data1 = 10.5;
float data2 = 15.2;

void setup() {
    
    
  Serial.begin(9600);
  // ...
}

void loop() {
    
    
  float minData = fmin(data1, data2); // 比较并获取最小值

  Serial.println(minData);
  delay(1000);
}

In this case, use the fmin() function to compare the size of two floating-point numbers data1 and data2 and return the smaller value. This makes it easy to filter and extract the smallest values ​​in the data.

Case Eight: Control System:

float setpoint = 50.0;
float input = 40.0;
float error = 0.0;

void setup() {
    
    
  // ...
}

void loop() {
    
    
  error = setpoint - input;

  if (error < 0) {
    
    
    float correction = fmin(error * 0.1, -1.0); // 根据误差大小选择控制策略
    // 执行相应的控制动作
    // ...
  }
  // ...
}

In this case, the fmin() function is used to select the appropriate control strategy according to the size of the error. According to the value of the error error, it is multiplied by a scaling factor of 0.1 and compared with -1.0, and the smaller value is selected as the correction amount of the control action. In this way, an appropriate control strategy can be selected according to the difference of the input signal.

Case 9: Numerical Analysis:

float value1 = 10.5;
float value2 = 15.2;
float threshold = 12.0;

void setup() {
    
    
  // ...
}

void loop() {
    
    
  float minValue = fmin(value1, value2); // 比较并获取最小值

  if (minValue < threshold) {
    
    
    // 根据最小值进行进一步的数值分析和优化算法
    // ...
  }
  // ...
}

In this case, two floating point numbers are compared using the fmin() function value1 fmin() is a math function in Arduino that returns the smaller value of two floating point numbers. It takes two parameters and returns the smaller of them.

insert image description here

Guess you like

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