int trig = 10;
int echo = 9;
int duration;
int distance;
void setup() {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW);
delay(.002);
digitalWrite(trig, HIGH);
delay(.01);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH); //record the time it takes for the sound to return
distance = (duration / 2) / 29.1; //convert the time to distance
Serial.println(distance);
delay(100);
}
No comments:
Post a Comment