Now i want that it can compare the 3 values to choose the longest distance. I used an if..else loop to let is choose but is there any way that works better. Because now it compares 45 with 90 and 135 with 90 degrees. But if the first string (45>90) is true, it won't check (135>90).

if (microsec45 > microsec90) { digitalWrite(ledpin43, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledpin43, LOW); // set the LED off } else if (microsec135 > microsec90) { digitalWrite(ledpin13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledpin13, LOW); // set the LED off } else { digitalWrite(ledpin13, HIGH); // set the LED on digitalWrite(ledpin43, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledpin13, LOW); // set the LED off digitalWrite(ledpin43, LOW); // set the LED off }

I used the 2 led pins on my Uno32 in stead of dc motors, to make it move visible what choices it makes.