#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>
#define Potentiometer A0
#define gripper_pin 3
Servo gripper;
Adafruit_MPU6050 mpu;
float sensitivity = 0.25;
float degree = 0;
float pot_value;
void setup(void) {
Serial.begin(115200);
gripper.attach(gripper_pin);
// MPU6050 IMU
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
// محدوده شتاب سنج
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// محدوده ژیروسکوپ
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
// پهنای باند فیلتر
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(100);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// pot_value = analogRead(A0);
//pot_sensitivity = map(pot_value, 0, 1023, 1, 5)
degree = map(a.acceleration.x, -19.6 * sensitivity, 19.6 * sensitivity, 0, 180);
gripper.write(degree);
Serial.print("Gripper degree: ");
Serial.print(degree - 90);
Serial.print("\n");
// Serial.print("Acceleration X: ");
// Serial.print(a.acceleration.x);
// Serial.print(", Y: ");
// Serial.print(a.acceleration.y);
// Serial.print(", Z: ");
// Serial.print(a.acceleration.z);
// Serial.println(" m/s^2");
// Serial.print("Rotation X: ");
// Serial.print(g.gyro.x);
// Serial.print(", Y: ");
// Serial.print(g.gyro.y);
// Serial.print(", Z: ");
// Serial.print(g.gyro.z);
// Serial.println(" rad/s");
// Serial.print("Temperature: ");
// Serial.print(temp.temperature);
// Serial.println(" degC");
// Serial.println("");
delay(100);
}