/**
*    文件: 超声测距.ino      by 零知实验室(www.lingzhilab.com)
*    -^^- 零知开源，让电子制作变得更简单！ -^^-
*    时间: 2018/10/13 15:29
*    说明: 
**/




#define SR04_TRIG	9

#define SR04_ECHO	10



//超声测距

int getDistance()



{



	digitalWrite(SR04_TRIG, LOW);



	delayMicroseconds(2);



	digitalWrite(SR04_TRIG, HIGH);



	delayMicroseconds(10);



	digitalWrite(SR04_TRIG, LOW);



	int distance = pulseIn(SR04_ECHO, HIGH);



	distance = distance/58.0;



	return distance;



}

// the setup routine runs once when you press reset:
void setup() {
    // put your setup code here, to run once:
	

	pinMode(SR04_TRIG, OUTPUT);

	pinMode(SR04_ECHO, INPUT);

	

	Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
    // put your main code here, to run repeatedly:
	

	int distance = getDistance();

	

	Serial.print("distance = ");

	Serial.println(distance);

	

	delay(300);
}
