Browse Source

Initial commit

Thorsten Riess 2 years ago
commit
b4d55213c1
1 changed files with 48 additions and 0 deletions
  1. 48
    0
      zeedee.ino

+ 48
- 0
zeedee.ino View File

@@ -0,0 +1,48 @@
1
+#include <NewPing.h>
2
+#include <Servo.h>
3
+
4
+#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
5
+#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
6
+#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
7
+
8
+#define LEFTSERVO_PIN 8
9
+#define RIGHTSERVO_PIN 9
10
+
11
+NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
12
+
13
+Servo leftservo, rightservo;
14
+
15
+void setup() {
16
+  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
17
+  leftservo.attach(LEFTSERVO_PIN);
18
+  rightservo.attach(RIGHTSERVO_PIN);
19
+  leftservo.write(90); // stop
20
+  rightservo.write(90);
21
+}
22
+
23
+double dist;
24
+
25
+void loop() {
26
+  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
27
+  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
28
+  dist = uS / US_ROUNDTRIP_CM;
29
+  //Serial.print("Ping: ");
30
+  //Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
31
+  //Serial.println("cm");
32
+
33
+  if(dist > 0) {
34
+    if(dist > 10) {
35
+      leftservo.write(120);
36
+      rightservo.write(60);
37
+    } else {
38
+      if(dist > 1) {
39
+        leftservo.write(60);
40
+        rightservo.write(120);
41
+      } else {
42
+        leftservo.write(90);
43
+        rightservo.write(90);
44
+      }
45
+    }
46
+  }
47
+}
48
+

Loading…
Cancel
Save