Arduino UNO LCD代码以循环方式显示数据

时间:2016-02-16 16:19:05

标签: c embedded arduino-uno lcd

我是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);
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

print函数对于参数类型具有不同的定义,就像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