#include <Adafruit_SSD1306.h> //oled
#include <Adafruit_GFX.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 54
#define DEADZONE 50 //chatGPT
LiquidCrystal_I2C lcd(16,2,0x27);
Adafruit_SSD1306 display(SCREEN_WIDTH,SCREEN_HEIGHT, &Wire , -1 );
Servo servoX;
Servo servoY;
const int joystickX= A0;
const int joystickY= A1;
const int servoX_pin= 9;
const int servoY_pin= 10;
int pointX=SCREEN_WIDTH/2;
int pointY = SCREEN_HEIGHT/2;
int currentServoX= 90;
int currentServoY= 90;
int xvalue=0;
int yvalue=0;
void setup() {
// put your setup code here, to run once:
servoX.attach(servoX_pin);
servoY.attach(servoY_pin);
servoX.write(currentServoX);
servoY.write(currentServoY);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
while(true){
}
}
display.clearDisplay();
lcd.begin(16,2);
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
xvalue= analogRead(joystickX);
yvalue= analogRead(joystickY);
if(abs(xvalue-512)>DEADZONE){
int targetX = map(xvalue, 0,1023, SCREEN_WIDTH-1,0);
int targetServoX = map(xvalue, 0,1023, 0, 180);
if(currentServoX<targetServoX) {currentServoX +=2;}
if(currentServoX>targetServoX) {currentServoX -=2;}
servoX.write(currentServoX);
if(pointX<targetX){pointX +=2;}
if(pointX>targetX){pointX -=2;}
}
}
display.clearDisplay();
display.fillCircle(pointX, pointY, 3, WHITE); // Draw the point
display.display();