//從我開始複製 Copy from here
// 2019-03-27 主機:OLED螢幕與搖桿測試
// 20190604修正上下左右界線數值
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SH1106 display(OLED_RESET);
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM str_4[] =
{
0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0x10, 0x88, 0x10, 0x98, 0x30, 0xA0, 0x50, 0xC0, 0x90, 0x80,
0x11, 0x80, 0x12, 0x80, 0x14, 0x80, 0x10, 0x80, 0x10, 0x82, 0x10, 0x82, 0x10, 0x7E, 0x10, 0x00,
};
static const unsigned char PROGMEM str_5[] =
{
0x3A, 0xB8, 0x21, 0x08, 0x3A, 0xB8, 0x21, 0x08, 0x3A, 0xB8, 0x20, 0x08, 0x7F, 0xFE, 0x40, 0x02,
0x9F, 0xE4, 0x00, 0x40, 0x01, 0x80, 0xFF, 0xFE, 0x01, 0x00, 0x01, 0x00, 0x05, 0x00, 0x02, 0x00,
};
static const unsigned char PROGMEM str_6[] =
{
0x08, 0x04, 0x08, 0x04, 0x14, 0x04, 0x12, 0x24, 0x29, 0xA4, 0x24, 0xA4, 0x7F, 0x24, 0xA1, 0x24,
0x3F, 0x24, 0x21, 0x24, 0x3F, 0x24, 0x20, 0x24, 0x3F, 0x04, 0x31, 0x04, 0x5F, 0x14, 0x91, 0x08,
};
static const unsigned char PROGMEM str_7[] =
{
0x02, 0x00, 0x01, 0x00, 0x7F, 0xFE, 0x48, 0x02, 0x8F, 0xF4, 0x08, 0x20, 0x14, 0x40, 0x23, 0x80,
0x0C, 0x60, 0x30, 0x1E, 0xCF, 0xE4, 0x08, 0x20, 0x08, 0x20, 0x08, 0x20, 0x0F, 0xE0, 0x08, 0x20,
};
static const unsigned char PROGMEM str_8[] =
{
0x02, 0x00, 0x01, 0x00, 0x7F, 0xFE, 0x40, 0x02, 0x9F, 0xF0, 0x11, 0x10, 0xFF, 0xFE, 0x11, 0x10,
0x1F, 0xF0, 0x00, 0x00, 0x1F, 0xF0, 0x14, 0x90, 0x12, 0x50, 0x1F, 0xF0, 0x0C, 0x40, 0x30, 0x20,
};
static const unsigned char PROGMEM str_9[] =
{
0x00, 0x40, 0x7C, 0x40, 0x50, 0xA0, 0x7C, 0xA0, 0x51, 0x10, 0x7E, 0x0E, 0x55, 0xF4, 0x50, 0x00,
0x7D, 0xDC, 0x05, 0x54, 0x55, 0x54, 0x55, 0xDC, 0x54, 0x88, 0x85, 0x54, 0x16, 0x22, 0x08, 0x00,
};
static const unsigned char PROGMEM str_10[] =
{
0x02, 0x00, 0x01, 0x00, 0x7F, 0xFE, 0x40, 0x02, 0x80, 0x04, 0x3F, 0xF8, 0x04, 0x00, 0x08, 0x20,
0x1F, 0xF0, 0x01, 0x10, 0x01, 0x00, 0x1F, 0xF0, 0x01, 0x00, 0x01, 0x08, 0x7F, 0xFC, 0x00, 0x00,
};
int xPin = A2;
int yPin = A3;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
String jxDirection = "";
String jyDirection = "";
String jsDirection = "";
void setup() {
Serial.begin(9600);
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
if (xPosition > 600) {
jxDirection = "Right";
}
else if (xPosition < 475) {
jxDirection = "Left";
}
else {
jxDirection = "";
}
if (yPosition > 600) {
jyDirection = "Down";
}
else if (yPosition < 475) {
jyDirection = "Up";
}
else {
jyDirection = "";
}
if (buttonState == 0) {
jsDirection = "Press";
}
else {
jsDirection = "";
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
display.setCursor(0, 0);
display.print(F("ChemBox V2.0 20190327"));
display.setTextColor(WHITE);
display.drawBitmap(8, 48, str_4, 16, 16, 1);
display.drawBitmap(24, 48, str_5, 16, 16, 1);
display.drawBitmap(40, 48, str_6, 16, 16, 1);
display.drawBitmap(56, 48, str_7, 16, 16, 1);
display.drawBitmap(72, 48, str_8, 16, 16, 1);
display.drawBitmap(88, 48, str_9, 16, 16, 1);
display.drawBitmap(104, 48, str_10, 16, 16, 1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(6, 15);
display.print("xPos=");
display.print(xPosition);
display.setCursor(68, 15);
display.print("yPos=");
display.print(yPosition);
display.setCursor(6, 30);
display.print("Sw=");
display.print(buttonState);
display.setCursor(32, 30);
display.print(jsDirection);
display.setCursor(68, 30);
display.print(jyDirection);
display.setCursor(93, 30);
display.print(jxDirection);
display.drawLine(0, 10, 127, 10, WHITE);
display.drawLine(0, 25, 127, 25, WHITE);
display.drawLine(0, 40, 127, 40, WHITE);
display.drawLine(0, 10, 0, 40, WHITE);
display.drawLine(63, 10, 63, 40, WHITE);
display.drawLine(127, 10, 127, 40, WHITE);
display.display();
}
//複製結束
END