void loop() {
while(1) {
The first obvious question is why do you have an infinite loop in a function that gets called in an infinite loop?
The next question concerns your inconsistent placement of curly braces. There are camps that say that every { goes on a new line. I'm firmly in that camp. There are camps that say that the { goes on the line with the statement. They are wrong, but they won't listen to reason. 8).
EVERYONE agrees that NOTHING goes on the same line as the }, except when the } closes out an array initialization statement. Even then, a space before the }, so that it stands out, is a good idea.
Everyone agrees that consistent indenting is a good idea.
Everyone agrees that one letter global variable names are a bad idea.
Most people agree that limiting the scope of variables is a good idea. duration is only used by ultrasonic(), so it should not be a global variable. distance shouldn't be, either, because ultrasonic() (which really needs a better name, like measureDistance()) should be returning a value.
If you need to trigger the sensor, and read the echo time, 10 times, and average the result, this is much easier to do if the variables that are involved are all right there in the function.