floating-point - LCD显示不同 float

标签 floating-point arduino microcontroller lcd arduino-uno

我正在开发将在 LCD 上显示输出的系统。

我将变量数据类型声明为 float,但 LCD 显示屏显示 0.00 我没有初始化,因为我用 0.0

声明它 我希望 LCD 显示屏显示 0.000,但无论我尝试更改初始化值,LCD 显示屏始终显示 0.00 .那么,我怎样才能让液晶显示屏显示0.000

This is the link for the image
以下是我的代码:

#include <LiquidCrystal.h>

volatile int NbTopsFan;
float flowrate = 0.0;
float volume = 0.0;
int hallSensor = 2;


LiquidCrystal lcd (12, 11, 9, 8, 7, 6);
void rpm() {
  NbTopsFan++;
}

void setup(){
  pinMode(3, OUTPUT);
  pinMode (hallSensor, INPUT);
  Serial.begin (9600);
  attachInterrupt (0, rpm, RISING);

  lcd.begin (16, 2);
  lcd.clear ();
}
void loop (){
  digitalWrite(3, HIGH);
  NbTopsFan=0;
  sei();
  delay(1000);
  cli();
  flowrate = NbTopsFan / 7.5;
  volume += (flowrate / 60);



  lcd.clear ();
  lcd.setCursor(0, 0);
  lcd.print("flow :");
  lcd.print(flowrate);
  lcd.print("L/m");
  lcd.setCursor(0, 1);
  lcd.print("VOl :");
  lcd.print(volume);
  lcd.print("L");

  Serial.print("Pulse");
  Serial.print(NbTopsFan);
  Serial.print(flowrate);
  Serial.print("L/m");
  Serial.print(volume,4);
  Serial.print("liter");
}

最佳答案

根据Print的文档,要指定要使用的小数位数,需要第二个参数。所以在你的例子中你可以使用

lcd.print(flowrate,4);

 lcd.print(volume,4);

关于floating-point - LCD显示不同 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25439146/

相关文章:

C语言编程 printf、scanf

c - 使用 Serial.write 串行发送字节数组时出错

arduino - 如何让 Arduino 正常工作而不是在 vscode 上给出 "cannot open source file "avr/pgmspace.h""?

SPI 24位地址设备的C结构

c - 我宣布 PI ,读取 alpha,但它没有进入指令。为什么它不打印0?

c++ - 将整数的二进制数据转换为 float

c++ - Arduino - 通过按钮停止循环

c - 为 32 位微 Controller 编写掩码的替代方法

stm32 - STM32F411RE 中的环回 SPI

matlab - matlab 支持 float16 吗?