//Created by Barbu Vulc!
//Special thanks to @Koepel & @Tim2000 (Discord) for help!
//GACSM = Gyro-accelerometer-controlled Servo Motor!
//Libraries:
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Servo.h>
//Sensor object:
Adafruit_MPU6050 mpu;
//Servo motor object & pin connection:
Servo servo;
const int servopin = 13;
//Project initialization!
void setup() {
Serial.begin(115200);
servo.attach(13);
//Testing sensor before usage:
while (!Serial)
delay(10);
Serial.println("Adafruit MPU6050 test!");
//Initialization!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
//Setup for accelerometer range:
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
//Setup for gyroscope range:
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
//Set up filter bandwidth:
mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
delay(100);
}
void loop() {
//Get new sensor events with the readings.
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
//I chose 1 scenario: rotating the horn by temperature detected by sensor!
//You can copy the project and modify it as you want! Have fun! :)
if (temp.temperature > 50) {
servo.write(30);
} else if (temp.temperature < -30) {
servo.write(60);
} else {
servo.write(0);
}
}