【问题标题】:Arduino UNO LCD code to display data in loopArduino UNO LCD 代码循环显示数据
【发布时间】:2020-04-01 02:02:15
【问题描述】:

我是 Arduino 新手。我想在 LCD 上显示从传感器读取的数据,第一行固定,第二行随传感器值变化。但我看到第一行有一段时间,然后全是垃圾。

以下是完整代码供参考:

#include <LiquidCrystal.h>
char ch;
int Contrast=155;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int analogInPin1 = A0;  // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A1;  // Analog input pin that the potentiometer is attached to
const int analogInPin3 = A2;  // Analog input pin that the potentiometer is attached to

int sensorValue1 = 0;        // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)

int sensorValue2 = 0;        // value read from the pot
int outputValue2 = 0;        // value output to the PWM (analog out)

int sensorValue3 = 0;        // value read from the pot
int outputValue3 = 0;        // value output to the PWM (analog out)

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

    analogWrite(6,Contrast); // setting contrast using code
    analogWrite(9,28836); // setting backlight led on

    // set up the LCD's number of columns and rows: 
    lcd.begin(16, 2);

    // Print a message to the LCD.
    lcd.setCursor(0,0);
    lcd.print(" CAR1 CAR2 CAR3 ");

    delay(200);
}

void loop() 
{
    // read the analog in value:
    sensorValue1 = analogRead(analogInPin1);
    // map it to the range of the analog out:
    outputValue1 = map(sensorValue1, 0, 1023, 0, 255);
    delay(250);
    sensorValue2 = analogRead(analogInPin2);
    // map it to the range of the analog out:
    outputValue2 = map(sensorValue2, 0, 1023, 0, 255);
    delay(250);  
    sensorValue3 = analogRead(analogInPin3);
    // map it to the range of the analog out:
    outputValue3 = map(sensorValue3, 0, 1023, 0, 255);
    delay(250);
    lcd.setCursor(0,1);

    lcd.print(outputValue1);
    lcd.print("  ");
    lcd.print(outputValue2);
    lcd.print("  ");
    lcd.print(outputValue3);
}

【问题讨论】:

  • 我猜你也应该在第二个循环中用固定文本更新第一行
  • 请显示“lcd.print()”的声明。目前尚不清楚它需要什么作为参数,并且 C 没有重载。
  • 我在循环中清除了整个 lcd 并打印了两行。它的工作,但现在它在几秒钟内变成空白。可能是什么问题呢。我必须重置 arduino 才能看到 lcd 数据。然后它又变成空白了。

标签: c embedded arduino-uno lcd


【解决方案1】:

我的猜测是 lcd.print 只打印字符串。如果有 lcd.printf,就是这样。如果不是,那么您必须使用 itoa() 或 sprintf() 将整数值更改为字符串。

【讨论】:

    【解决方案2】:

    打印函数对参数类型有不同的定义,就像 Serial.print 一样。 清除 lcd 以覆盖它是一种很好的做法。 例如,如果outputval 是第一次 1011 和第二次 1,您将获得第一次。 1011 1011 1011 第二个。 1 1 1 这给了你 1 1 11011 1011

    最简单的方法是调用lcd.clear() 清空lcd 并重新写入第一行。 或者你可以在第二行写“”(空格)见:https://forum.arduino.cc/index.php?topic=212460.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多