You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zeedee.ino 1.5KB

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