|
各位大大,日前測試Arduino+LCD,遇到顯示的問題,請先進們指教~剛開始顯示正常(Driver RPM=3,數值3顯示正常)
(Driver RPM=120,數值120也顯示正常),但之後數值回到6的時候,卻顯示為600,數值回到88的時候,卻顯示為880
下列為程式碼:
//腳位定義
#define KeyPadPin A0 //Analog按鍵
//按鍵參數
#define Offset 10
#define SelectADC 639 //按鍵Select Analog值
#define LeftADC 408 //按鍵Left Analog值
#define UpADC 99 //按鍵Up Analog值
#define DownADC 256 //按鍵Down Analog值
#define RightADC 0 //按鍵Right Analog值
//按鍵旗標
int val ;
union{
unsigned char Val;
struct
{
unsigned Select:1; //Select按鍵被按下旗標
unsigned Left:1; //Left按鍵被按下旗標
unsigned Up:1; //Up按鍵被按下旗標
unsigned Down:1; //Down按鍵被按下旗標
unsigned Right:1; //Right按鍵被按下旗標
unsigned :1; //保留
unsigned :1; //保留
unsigned :1; //保留
};
}KeyDown;
//按鍵參數
int KeyVal;
unsigned char KeyDownOld; //按鍵舊值暫存器
unsigned int KeySelectVal, //Select按鍵按下計數器
KeyLeftVal, //Left按鍵按下計數器
KeyUpVal, //Up按鍵按下計數器
KeyDownVal, //Down按鍵按下計數器
KeyRightVal, //Right按鍵按下計數器
dontPressVal; //未按下按鍵
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
/* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD BL pin to digital pin 10
* KEY pin to analogl pin 0
*/
LiquidCrystal lcd(8,9,4,5,6,7); //腳位定義 lcd(RS,E,D4,D5,D6,D7);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Motor RPM =");
lcd.setCursor(0, 1);
lcd.print("Driver RPM =");
}
//***************************
//******* Main Loop *********
//***************************
void loop()
{
val = analogRead(1);
Serial.println(val);
int rpm = map(val,0,1023,0,120);
lcd.setCursor(12,1);
lcd.print(rpm);
} |
|