#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
#define UP 2
#define DOWN 3
#define LEFT 4
#define RIGHT 5
int dx = 0;
int dy = 0;
void drawAxes() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.drawLine(64 + dx, 0, 64 + dx, 64, SSD1306_WHITE);
display.drawLine(0, 32 + dy, 128, 32 + dy, SSD1306_WHITE);
display.display();
}
void drawPoint() {
display.fillCircle(64, 32, 2, SSD1306_WHITE);
display.display();
}
void moveAxes() {
int up = digitalRead(UP);
int down = digitalRead(DOWN);
int left = digitalRead(LEFT);
int right = digitalRead(RIGHT);
if (up == HIGH && dy > -31)
dy--;
if (down == HIGH && dy < 32)
dy++;
if (left == HIGH && dx > -64)
dx--;
if (right == HIGH && dx < 64)
dx++;
}
void showCoordinates() {
int x = -dx;
int y = -dy;
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 55);
display.print("(");
display.print(-x);
display.print(", ");
display.print(y);
display.print(")");
display.display();
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(LEFT, INPUT);
pinMode(RIGHT, INPUT);
drawAxes();
showCoordinates();
}
void loop() {
moveAxes();
drawAxes();
showCoordinates();
}