PS2搖桿一般可以用來控制小車等,其構(gòu)造主要就是兩個10K的電位器,還有一個按鍵開關(guān)。五個端口分別為VCC、X、Button、Y、GND(,接線示意:
程序代碼:
#include <LiquidCrystal.h>? ?//調(diào)用arduino自帶的LiquidCrystal庫
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);//設(shè)置接口
int xpotPin = 0;? //設(shè)置模擬口0為X的信號輸入端口
int ypotPin = 1;? //設(shè)置模擬口1為Y的信號輸入端口
int xval=0;? ? //設(shè)置變量
int yval=0;
void setup()
{
pinMode(xpotPin,INPUT);//
pinMode(ypotPin,INPUT);//
lcd.begin(16, 2);? //初始化LCD
delay(1000); //延時1000ms
}
void loop ()
{
xval = analogRead(xpotPin);? ?//xval變量為從0信號口讀取到的數(shù)值
yval = analogRead(ypotPin);? ?//yval變量為從1信號口讀取到的數(shù)值
lcd.clear(); //清屏
lcd.setCursor(0, 0) ; //設(shè)置光標(biāo)位置為第一行第一個位置
lcd.print("X=");? ? ? //使屏幕顯示文字X=
lcd.print(xval);
lcd.setCursor(7, 0) ; //設(shè)置光標(biāo)位置為第一行第八個位置
lcd.print("Y=");? ? ? //使屏幕顯示文字Y=
lcd.print(yval);
delay(100);? ? ? ? ? ? //延時0.1秒,這里也就是刷新速度。
}