#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int buttonPin = 4;
#define SCREEN_WIDTH 128 // OLED display width, in pixels ANCHO
#define SCREEN_HEIGHT 64 // OLED display height, in pixels ALTO
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
pinMode(buttonPin, INPUT_PULLUP); //joystick button
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialization
display.clearDisplay();
}
void loop() {
bool button = digitalRead(buttonPin);
int x = analogRead(A0);
int y = analogRead(A1);
f_drawJoystickInfo(button, x, y);
}
void f_drawJoystickInfo(bool button, int x, int y) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Joystick");
display.println(" test");
display.println();
display.print("X: ");
display.println(x);
display.print("Y: ");
display.println(y);
if(!button) {
display.setCursor(0, 54);
display.setTextColor(BLACK, WHITE);
display.print("Button");
}
int x1 = map(x, 0, 1023, display.width() - 60 - 2, display.width() - 2);
int y1 = map(y, 0, 1023, display.height() - 60 - 2, display.height() - 2);
display.drawRect(display.width() - 60 - 2, display.height() - 60 - 2, 60, 60, WHITE); //draw frame
display.drawLine(x1, y1 - 2, x1, y1 + 2, WHITE); //draw joystick position inside frame
display.drawLine(x1 - 2, y1, x1 + 2, y1, WHITE);
display.display();
}