Circuit:
Arduino Programming:
#include <Servo.h>
int ir_sensor = A0;
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
myservo.write(0);
}
void loop() {
int sensor_value = analogRead(ir_sensor); //read the sensor value
int distance_cm = pow(3027.4/sensor_value, 1.2134); //convert readings to distance(cm)
if (distance_cm < 10) {
myservo.write(180);
delay(10);
}
else
delay(1000);
myservo.write(0);
delay(100);
}
Demo Video:
留言