Skip to content

Instantly share code, notes, and snippets.

@neosarchizo
Created May 10, 2016 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neosarchizo/e3e741ee28df126db0959947de354e69 to your computer and use it in GitHub Desktop.
Save neosarchizo/e3e741ee28df126db0959947de354e69 to your computer and use it in GitHub Desktop.
[아두이노, 상상을 현실로 만드는 프로젝트 심화편] 코드 7 - 1
#include <Servo.h>
Servo servo;
int servoDirection = 1, degree = 0;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, INPUT);
servo.attach(9);
}
void loop() {
digitalWrite(2, HIGH);
delayMicroseconds(5);
digitalWrite(2, LOW);
long distance = pulseIn(3, HIGH, 5800) / 58;
Serial.print("r");
Serial.print(degree);
Serial.print("d");
Serial.println(distance);
degree += servoDirection;
if (degree > 180) {
degree = 179;
servoDirection = -1;
} else if (degree < 0) {
degree = 1;
servoDirection = 1;
}
servo.write(degree);
delay(15);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment