Ultrasonic Sensor

/*
 *https://arduspot.blogspot.in/p/ultrasonic-sensor.html
 */
#include <NewPing.h>
#define TRIGGER_PIN  2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 1000 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

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

void setup()
{
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop()
{

  delay(1500);                  
  unsigned int uS = sonar.ping();
  dist = uS / US_ROUNDTRIP_CM;

  Serial.print(dist);
  Serial.println("cm");
}

No comments:

Post a Comment