#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int X_axis=23;
int a;
float b;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3c
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,&Wire, OLED_RESET);
int ball_x=63;
int ball_y=31;
int ball_radius=2;
int ball_speed_x=1;
int ball_speed_y=1;
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);}
pinMode(34,INPUT);
}
void loop() {
a=analogRead(34);
if (a!=0 && a>2047.5){
b= a*128/4095;
}
if (a!=0 && a<2047.5){
b= a*128/4095 +20 ;
}
if (a==0){
b=20;
}
ball_x=ball_x+ball_speed_x;
ball_y=ball_y+ball_speed_y;
display.clearDisplay();
display.fillRect(b-20,60,20,5,WHITE);
if (ball_y-ball_radius <= 0 ){
ball_speed_y= - ball_speed_y;
}
if (ball_x-ball_radius <=0 || ball_x+ball_radius >=128){
ball_speed_x=- ball_speed_x;
}
if (b-20<= ball_x && ball_x <=b && ball_y==60 ){
ball_speed_y = -ball_speed_y;
}
display.fillCircle(ball_x,ball_y,ball_radius,WHITE);
display.display();
delay(20);
}
2ND CODE
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int k1;
int k2;
int x1=64;
int z1=32;
int x_speed=1;
int z_speed=1;
int score=0;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int x=55;
int y=60;
void setup()
{ pinMode(16,INPUT);
pinMode(15,INPUT);
pinMode(18,OUTPUT);
Serial.begin(9600);
// initialize the OLED object
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
}
void loop(){
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor( 115, 1);
display.print(score);
k1=digitalRead(16);
if(k1==0 && x+20<=127){
x=x+3;
}
k2=digitalRead(15);
if(k2==0 && x>=0){
x=x-3;
}
if(2<x1<125 && 0<z1){
x_speed=x_speed;
z_speed=z_speed;
}
if(x<=x1 && x1<=x+20 && z1+2==59){
z_speed=-z_speed;
x_speed=x_speed;
score=score+1;
digitalWrite(18,HIGH);
delay(20);
digitalWrite(18,LOW);
}
if(x1==125 || x1==2){
x_speed=-x_speed;
z_speed=z_speed;
}
if(z1==2 ){
z_speed=-z_speed;
x_speed=x_speed;
}
x1=x1+x_speed;
z1=z1+z_speed;
display.fillCircle(x1, z1 , 2 ,WHITE);
display.fillRect(x,y,20,4, WHITE);
display.display();
display.clearDisplay();
if(z1==61){
digitalWrite(18,HIGH);
delay(1000);
digitalWrite(18,LOW);
}
while(z1==61){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor( 25,25);
display.print("score ");
display.print(score);
display.display();
}
//if(x_speed<0){
//x_speed=x_speed-score;
//}
//if(x_speed>0){
//x_speed=x_speed+score;
//}
//if(z_speed<0){
// z_speed=z_speed-score;
//}
//if(z_speed>0){
// z_speed=z_speed+score;
//}
}