#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define button_1 19
#define button_2 18
#define button_3 14
#define button_4 12
int x=60;
int y=30;
int width=10;
int height=8;
Adafruit_SSD1306 display (128,64,&Wire,-1); //-1 means the reset pin is not connected
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(button_1, INPUT_PULLUP);
pinMode(button_2, INPUT_PULLUP);
pinMode(button_3, INPUT_PULLUP);
pinMode(button_4, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0X3c))
{
Serial.println("display not found");
while(1);
}
display.clearDisplay();
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10,10);
delay(2000);
display.clearDisplay();
display.stopscroll();
display.drawRect(x,y,width,height,WHITE);
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
if (digitalRead(button_1)==0) {
Serial.println("left button pressed");
x=x-1;
}
if (digitalRead(button_4)==0) {
Serial.println("right button pressed");
x=x+1;
}
if (digitalRead(button_2)==0) {
Serial.println("up button pressed");
y=y-1;
}
if (digitalRead(button_3)==0) {
Serial.println("down button pressed");
y=y+1;
}
display.clearDisplay();
display.drawRect(x,y,width,height,WHITE);
display.display();
}