Browse Source

Initial commit

master
Thorsten Riess 3 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 @@
#include <NewPing.h>
#include <Servo.h>

#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

#define LEFTSERVO_PIN 8
#define RIGHTSERVO_PIN 9

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

Servo leftservo, rightservo;

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
leftservo.attach(LEFTSERVO_PIN);
rightservo.attach(RIGHTSERVO_PIN);
leftservo.write(90); // stop
rightservo.write(90);
}

double dist;

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
dist = uS / US_ROUNDTRIP_CM;
//Serial.print("Ping: ");
//Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.println("cm");

if(dist > 0) {
if(dist > 10) {
leftservo.write(120);
rightservo.write(60);
} else {
if(dist > 1) {
leftservo.write(60);
rightservo.write(120);
} else {
leftservo.write(90);
rightservo.write(90);
}
}
}
}


Loading…
Cancel
Save