Arduino
자이로 센서(Gyro Sensor) : 서보모터 동작하기
작성자 임베디드코리아
작성일25-05-16 00:46
조회171회
댓글0건
--------< 자이오 센서에 따른 서보모터 동작 소스 코드 >------------------------------
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
Servo myservo;
int val;
int prevVal;
void setup()
{
Wire.begin();
Serial.begin(38400);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
myservo.attach(9);
}
void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
val = map(ay, -17000, 17000, 0, 179);
if (val != prevVal)
{
myservo.write(val);
prevVal = val;
}
delay(50);
}