long r = 0.034 * duration / 2;
That could be simplified.
long r = 0.017 * duration;
I've not run the code but it looks about right.
What distance are you trying to measure, 2 to 3 cm is about minimum, and the supposed maximum was probably obtained in an test chamber - or I have edge of the bell curve devices
many code snippets give...
digitalWrite( triggerPort, LOW );
17 delayMicroseconds( 1 );
18 //invia un impulso di 10microsec su trigger
19 digitalWrite( triggerPort, HIGH );
20 delayMicroseconds( 10 );
Just read your post again, are you suggesting that it is pre-triggering? (reading the outgoing chirp?) I would think that was unlikely, although your device may be faulty. you could add a few uS delay before the pulseIn()
I've not hooked a scope to one of these yet, but I do note that there are several models and some can be set to single pin mode - do check your device.