#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int potValue = 0;
unsigned long currentTime = 0;
unsigned long saveTime = 0;
unsigned long diffrenceTime = 0;
int xPlay1=10;
int yPlay1=30;
int xPlay1Dir = 1;
int scorPlay=0;
int x1_p2 = SCREEN_WIDTH - 4;
int xBall;
int yBall;
int xNewBall = 7;
int yNewBall = 7;
int xBallDir = 1;
int yBallDir = 1;
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
delay(2000);
display.clearDisplay();
// display.setRotation(2);
display.setTextSize(1);
display.setTextColor(WHITE);
display.display();
int x1_p1 = 4;
}
void loop() {
display.fillScreen(BLACK);
drawCourt();
currentTime = millis();
diffrenceTime = currentTime - saveTime;
display.setCursor(50, 3);
// Display static text
display.println(scorPlay);
if (diffrenceTime >= 10) {
saveTime = currentTime;
xNewBall += xBallDir;
yNewBall += yBallDir;
display.drawPixel(xBall, yBall, BLACK);
display.drawPixel(xNewBall, yNewBall, WHITE);
xBall=xNewBall;
yBall=yNewBall;
}
if (yNewBall == 63) {
yBallDir = -yBallDir;
}
if (yNewBall == 0) {
yBallDir = -yBallDir;
}
if (xNewBall == 127) {
xBallDir = -xBallDir;
}
if (xNewBall == 0) {
xBallDir = -xBallDir;
scorPlay++;
}
// display.fillCircle(x++, y++, 1, WHITE);
//xPlay1=xPlay1+1*xPlay1Dir;
potValue = analogRead(0);
xPlay1=map(potValue,0,1024,1,48);
display.drawFastVLine(15,xPlay1,15,WHITE);
if (xPlay1 == 0) {
xPlay1Dir = -xPlay1Dir;
}
if (xPlay1 == 64-10) {
xPlay1Dir = -xPlay1Dir;
}
if (xNewBall == 15 && yNewBall >=xPlay1 && yNewBall<=xPlay1+15) {
xBallDir = -xBallDir;
}
//Serial.println(potValue);
display.display();
}
void drawCourt() {
display.drawRect(0, 0, 128, 64, WHITE);
}