#include <Wire.h> // I2C程式庫
#include <LiquidCrystal_I2C.h> // LCD_I2C模組程式庫
/**
LCD I2C位址,默認為0x27或0x3F,依據背板的晶片,
不同而有差異,16、2為LCD顯示器大小。
**/
LiquidCrystal_I2C lcd(0x27, 20, 4);
uint8_t heart[8]=
{
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
};
uint8_t up[8]=
{
0b00000000,
0b00000000,
0b00000100,
0b00001110,
0b00011111,
0b00000100,
0b00000100,
0b00000000,
};
uint8_t right[8]=
{
0b00000000,
0b00000000,
0b00000100,
0b00001100,
0b00011111,
0b00001100,
0b00000100,
0b00000000,
};
uint8_t down[8]=
{
0b00000000,
0b00000000,
0b00000100,
0b00000100,
0b00011111,
0b00001110,
0b00000100,
0b00000000,
};
uint8_t left[8]=
{
0b00000000,
0b00000000,
0b00000100,
0b00000110,
0b00111111,
0b00000110,
0b00000100,
0b00000000,
};
int x, y;
int dir=1;
void setup()
{
// put your setup code here, to run once:
// 初始化LCD
lcd.init();
lcd.backlight();
lcd.createChar(1, heart);
lcd.createChar(2, up);
lcd.createChar(3, right);
lcd.createChar(4, down);
lcd.createChar(5, left);
}
void loop()
{
// put your main code here, to run repeatedly:
y = analogRead(A0);
x = analogRead(A1);
if (y>700) // 搖桿往上
dir=2;
if (y<300) // 搖桿往下
dir=4;
if (x<300) // 搖桿往左
dir=6;
if (x>700) // 搖桿往右
dir=8;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("\x01");
lcd.setCursor(0,1);
lcd.print("\x02 \x03 \x04 \x05");
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print("x :");
lcd.print(x);
lcd.print(" Y :");
lcd.print(y);
lcd.setCursor(0,3);
if (dir==2)
lcd.print("\x02");
if (dir==4)
lcd.print("\x04");
if (dir==6)
lcd.print("\x05");
if (dir==8)
lcd.print("\x03");
delay(300);
}