const int TrigPin = 5;
const int EchoPin = 18;
long duration, distance_1;
int colision_distance = 30;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(100);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop() {
digitalWrite(TrigPin, LOW);
delayMicroseconds(10);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(2);
digitalWrite(TrigPin, LOW);
duration = pulseIn(EchoPin, HIGH);
distance_1 = microsecondsToCentimeters(duration);
Serial.print(" Sonar Read - ");
Serial.println(distance_1);
delay(1000);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}