volatile int echo_start, echo_end, d, duration;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
EICRA=EICRA&0b00000000;
EICRA=EICRA|0B00000001;
SREG=0X80;
EIMSK=EIMSK|0b00000001;
pinMode(3,OUTPUT);
pinMode(2,INPUT);
}
ISR(INT0_vect){
if(digitalRead(2)==1){
echo_start=micros();
}
echo_end=micros();
duration=(echo_end-echo_start);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(3, HIGH);
delay(10);
digitalWrite(3, LOW);
d=(duration*0.001715);
Serial.println(d);
delay(500);
}