簡體   English   中英

Arduino UNO LCD 代碼循環顯示數據

[英]Arduino UNO LCD code to display data in loop

我是 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 只打印字符串。 如果有lcd.printf,就是這樣。 如果不是,則必須使用 itoa() 或 sprintf() 將整數值更改為字符串。

打印函數對參數類型有不同的定義,很像 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM