Arduino+LM35+kalman+i2C_lcd, 加入卡爾曼濾波显示温度比较度稳定, 可显示小数後三位。
- /*
- This sample code demonstrates how to use the SimpleKalmanFilter object.
- Use a potentiometer in Analog input A0 as a source for the reference real value.
- Some random noise will be generated over this value and used as a measured value.
- The estimated value obtained from SimpleKalmanFilter should match the real
- reference value.
- SimpleKalmanFilter(e_mea, e_est, q);
- e_mea: Measurement Uncertainty
- e_est: Estimation Uncertainty
- q: Process Noise
- https://github.com/denyssene/SimpleKalmanFilter
- */
- #include <SimpleKalmanFilter.h>
- SimpleKalmanFilter simpleKalmanFilter(1, 1, 0.1);
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x3f,16,2); //(0x20,16,2)
- const long SERIAL_REFRESH_TIME = 100;
- long refresh_time;
- float tempC;
- int tempPin = A0;
- void setup(){
- Serial.begin(9600);
- lcd.begin();
- lcd.print("Temp =");
- lcd.setCursor(0, 1);
- lcd.print("TempKa=");
- }
- void loop(){
- if (millis() > refresh_time) { // 每100ms發送到串行輸出
- tempC=0;
- tempC = analogRead(tempPin); //从传感器读取值
- tempC += analogRead(tempPin);
- tempC += analogRead(tempPin);
- tempC += analogRead(tempPin);
- tempC =tempC/4;
- tempC = (5.0 * tempC * 100.0)/1024.0; //将模拟数据转换为温度
- // 用卡爾曼濾波器計算估計值
- float kal_tempC = simpleKalmanFilter.updateEstimate(tempC);
- Serial.print(tempC); // 使用串行繪圖儀進行良好的可視化
- Serial.print(",");
- Serial.println(kal_tempC);
- //将结果到lcd显示
- lcd.setCursor(7, 0);
- lcd.print(tempC,3);
- lcd.print(" C");
- lcd.setCursor(7, 1);
- lcd.print(kal_tempC,3);
- lcd.print(" C");
- refresh_time = millis() + SERIAL_REFRESH_TIME;
- }
- }
复制代码
|