1#define trigPin 12
2#define echoPin 13
3
4int n;
5int duration, distance;
6
7String readString;
8
9#include <Servo.h>
10Servo myservo1;
11Servo myservo2;
12
13void setup() {
14 Serial.begin(9600);
15 myservo1.attach(8);
16 myservo2.attach(9);
17 pinMode(trigPin, OUTPUT);
18 pinMode(echoPin, INPUT);
19 pinMode(3, OUTPUT);
20}
21
22void loop() {
23 digitalWrite(trigPin, HIGH);
24 _delay_ms(500);
25 digitalWrite(trigPin, LOW);
26 duration = pulseIn(echoPin, HIGH);
27 distance = (duration/2) / 29.1;
28
29 if (distance < 40) {
30 digitalWrite(3, HIGH);
31 myservo1.write(n);
32 myservo2.write(180-n);
33 delay(1000);
34 myservo1.write(n);
35 myservo2.write(90-n);
36 delay(500);
37}
38 else {
39 digitalWrite(3, LOW);
40 myservo1.write(180-n);
41 myservo2.write(n);
42}
43}
Anonymous user
6 years ago
Very cool project. So simple and yet so neat. I always like it when a project just simply works. Thanks for sharing.