使用 Arduino 进行 LCD 编程 [英] LCD Programming with Arduino

查看:30
本文介绍了使用 Arduino 进行 LCD 编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 LCD 显示Voltage=(sensorValue)",但现在我可以让程序在我转动电位器时识别该值的唯一方法是将其置于循环中.但是当我把它放在一个循环中时,整个屏幕会被 1s、2s、3s、4s 或 5s 填满,具体取决于电位器的设置位置.

I would like my LCD to display "Voltage=(sensorValue)" but right now the only way I can get the program to recognize the value as I turn the potentiometer is if I put it in a loop. But when I put it in a loop the whole screen gets filled with 1s, 2s, 3s, 4s, or 5s depending on where the potentiometer is set.

如果我没有循环,那么无论电位器处于何种设置,屏幕上都会弹出并且在电位器打开时不会改变.

If I don't have it in a loop then whatever setting the potentiometer is on is what will pop on the screen and will not change if potentiometer is turned.

如何将循环的结果放在循环之外,以便获得(Voltage=sensoreValue)"?

How can I put the results of a loop outside of a loop so I can have "(Voltage=sensoreValue)"?

这是我的程序:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  

void setup()
{
    lcd.init();                      
    lcd.backlight();
    int sensorPin = A0;
    int sensorValue = 0;
    sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    lcd.print("Voltage=");
}

void loop()
{
    int sensorPin = A0;
    int sensorValue = 0;
    sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    lcd.print(sensorValue);
}

推荐答案

您要求它打印读数并且它正在执行 - 它正在打印每个读数!

You asked it to print the reading and it is doing - it's printing each reading!

我怀疑您要么希望它仅在值更改时打印

I suspect you either want it to only print if the value changes

int sensorValue = 0;
int prevValue = 0;

void loop()
{    
    sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    if (sensorValue != prevValue) {
       lcd.print(sensorValue);
       prevValue == sensorValue;
    }
}

或者,如果您的显示器 lcd.print 支持,您可以打印 'n' 个退格,以便新值打印在旧值的顶部

Alternatively you could print 'n' backspaces so the new value is printed over the top of the old one, if your display lcd.print supports that

这篇关于使用 Arduino 进行 LCD 编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆