Upload intcomboson.ino
Browse files- intcomboson.ino +50 -0
intcomboson.ino
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <Servo.h>
|
2 |
+
|
3 |
+
const int trigPin= 10;
|
4 |
+
const int echoPin = 11;
|
5 |
+
|
6 |
+
long duration;
|
7 |
+
int distance;
|
8 |
+
Servo myServo;
|
9 |
+
|
10 |
+
void setup() {
|
11 |
+
// put your setup code here, to run once:
|
12 |
+
pinMode(trigPin, OUTPUT);
|
13 |
+
pinMode(echoPin, INPUT);
|
14 |
+
Serial.begin(9600);
|
15 |
+
myServo.attach(12);
|
16 |
+
}
|
17 |
+
|
18 |
+
void loop() {
|
19 |
+
// put your main code here, to run repeatedly:
|
20 |
+
for(int i=15; i<=165;i++){
|
21 |
+
myServo.write(i);
|
22 |
+
delay(30);
|
23 |
+
distance = calculateDistance();
|
24 |
+
|
25 |
+
Serial.print(i);
|
26 |
+
Serial.print(", ");
|
27 |
+
Serial.print(distance);
|
28 |
+
Serial.print(".");
|
29 |
+
}
|
30 |
+
|
31 |
+
for(int i=165;i>15;i--){
|
32 |
+
myServo.write(i);
|
33 |
+
delay(30);
|
34 |
+
distance = calculateDistance();
|
35 |
+
Serial.print(i);
|
36 |
+
Serial.print(",");
|
37 |
+
Serial.print(distance);
|
38 |
+
Serial.print(".");
|
39 |
+
}
|
40 |
+
}
|
41 |
+
|
42 |
+
int calculateDistance(){
|
43 |
+
digitalWrite(trigPin, LOW);
|
44 |
+
delayMicroseconds(2);
|
45 |
+
delayMicroseconds(10);
|
46 |
+
digitalWrite(trigPin, LOW);
|
47 |
+
duration = pulseIn(echoPin, HIGH);
|
48 |
+
distance = duration*0.034/2;
|
49 |
+
return distance;
|
50 |
+
}
|