/*
Wokwi | questions
help me resolve this error
Ben October 24, 2025 9:23 AM
Project Link: https://wokwi.com/projects/445627259529460737?gh=1
*/
#include <Wire.h>
#include <ESP32Servo.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
const int SERVO_PIN = 18;
Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
Servo myServo;
void setup() {
Serial.begin(115200);
if (!mpu.begin()) {
Serial.println("\nSensor init failed");
for (;;); // halt
} else {
Serial.println("\nFound a MPU-6050 sensor");
}
display.setRotation(2);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("\nSSD1306 allocation failed"));
for (;;); // halt
}
myServo.attach(SERVO_PIN);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); // draw white on black
display.clearDisplay();
display.print("Ready!");
display.display();
delay(2000);
display.setTextSize(1);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
int angle = map(a.acceleration.x, -20, 20, 0, 180); // Adjust range as needed
myServo.write(angle);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Accel X: "); display.println(a.acceleration.x);
display.print("Servo: "); display.println(angle);
display.display();
delay(100);
}