[Arduino] 15 기울기로 서보 움직이기

exercise

가속센서를 이용해 기울기에 따라 서보모터를 움직여 봅시다

material

RC Servo motor​

Acceleration Sensor(AM-3AXIS)

circuit

code

#include "Servo.h"​

Servo myservo;​
int servopin = 9;​
int xpin = 0;​

void setup() {​
  Serial.begin(9600);​
  myservo.attach(servopin);​
  myservo.write(90);  //초기화 ​
  delay(1000);​
}​

void loop() {​
  int xVal = analogRead(xpin);​
  Serial.println(xVal); //212 ~ 472​

  xVal = map(xVal, 212, 472, 0, 180);​
  xVal = constrain(xVal, 0, 180);​

  myservo.write(xVal);​
  delay(200);​
}


further exercise

[중.고급] 스텝모터를 이용하여 같은 기능을 하도록 해봅시다