-
-
Save neosarchizo/e3e741ee28df126db0959947de354e69 to your computer and use it in GitHub Desktop.
[아두이노, 상상을 현실로 만드는 프로젝트 심화편] 코드 7 - 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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