I can't combine leds and ultrasonic sensor

I wrote the entire code correctly but the last part give me some problems, the idea was to turn on led 13 and others turn off if distance is more than 30 cm. if the distance is less than 10 cm the led 9 turn on e others turn off. this part works but i can't make the led 11 turn on and others turn off when the distance is between 10 and 30 cm. Can someone explaine me how can do this?

Please post code, not pictures of code.

sorry i dont know how to post it in other ways

const int trigger=2;

const int echo=4;

void setup() {








void loop() {





long durata = pulseIn(echo,HIGH);

long distanza = 0.034 * durata / 2;




if( distanza > 30) digitalWrite(13,HIGH); 

if( 10 < distanza < 30 ) digitalWrite(11,HIGH);

if(distanza < 10) digitalWrite(9,HIGH);


This doesn't do what you think

if (10 < distanza && distanza < 30)

Please remember to use code tags when posting code

thanks a lot, im a newbie so I have to learn the basics

Now edit your post #5, select all code and click the </> button; Next save the post. This will add the so-called code tags which makes it easier to read, easier to copy and prevents the forum software from
Interpretation of some characters in your code as 'commands'.

what are cod tags?



Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.