/* */ // this constant won't change. It's the pin number of the sensor's output: const int pingPin = 2; //INT0 as well long inches, cm; volatile long starttime, endtime, duration; volatile bool newVal=false; void setup() { pinMode(pingPin, INPUT); // initialize serial communication: Serial.begin(9600); cli(); //we are disabling all interrupts EICRA = 0b00000011; // INT0, rising edge EIFR = 0b00000001; //clear INT0 flag EIMSK = 0b00000001; //enable INT0 sei(); // we re-enable interrupts including timer interrupts } void loop() { if (newVal) { Serial.print(duration); Serial.print("us "); Serial.println(); newVal=false; delay(100); } //end if }//end loop ISR(INT0_vect) { // INT0 interrupt service routine unsigned long currenttime=micros(); if ((PIND & (1 << PIND2))) { starttime=currenttime; EICRA = 0b00000010; // INT0, falling edge } else { endtime=currenttime; EICRA = 0b00000011; // INT0, rising edge duration=endtime-starttime; newVal=true; } EIFR |= 0b00000001; //clear INT0 flag }// end ISR