else statement error message USING XC4442 Ultrasonic sensor + arduino

hi guys sorry to post but i cant figure out why my else statements aren’t working???

any ideas or fixes would be much appreciated

#define TRIG 3
#define ECHO 2
#define USMAX 3000 
int led2=A0;
int led4=A1;
int led6=A2;
int led8=A3;
int led10=A4;


void setup()
{
  Serial.begin(115200); 
  
 usonicsetup();
  

  
  
  
  pinMode(led2, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led10, OUTPUT);
  
  digitalWrite(led2, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led10, LOW);
  
  delay(1000);
}

void loop()
{
 int distance; 
 distance=usonic(11600)/58; //distance in cm, time out at 11600us or 2m maximum range
 Serial.println(distance); //distance in cm
 delay(10000); // how long between differnt sonar scans 1000 = 1 sec
  
}
 void usonicsetup(void)
 {
 pinMode(ECHO, INPUT);
 pinMode(TRIG, OUTPUT);
 digitalWrite(TRIG, LOW);
}

long usonic(long utimeout){ //utimeout is maximum time to wait for return in us
 long b;
 if(digitalRead(ECHO)==HIGH){return 0;} //if echo line is still low from last result,
return 0;
 digitalWrite(TRIG, HIGH); //send trigger pulse
 delay(1);
 digitalWrite(TRIG, LOW);
 long utimer=micros();
 while((digitalRead(ECHO)==LOW)&&((micros()-utimer)<1000)){} //wait for pin state to changereturn starts after 460us typically or timeout (eg if not connected)
 utimer=micros();
 while((digitalRead(ECHO)==HIGH)&&((micros()-utimer)<utimeout)){} //wait for pin state to change
 b=micros()-utimer;
 if(b==0){b=utimeout;}
 return b;
  int distance; 
 distance=usonic(11600)/58;


if (  (distance > 0) && (distance <= 10)  ) // distance in cm
 
  digitalWrite(led2, HIGH); 
  digitalWrite(led4, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led10, HIGH);

} else
if(  (distance > 10) && (distance <= 20)  ) 
{
 
  digitalWrite(led10, LOW); 
  digitalWrite(led2, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led8, HIGH); 
  
  
 } else
 
if(  (distance > 20) && (distance <= 30)  ) 
{
 
  digitalWrite(led10, LOW); 
  digitalWrite(led8, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led6, HIGH);
} else

if(  (distance > 30) && (distance <= 40)  ) 
{
 
  digitalWrite(led10, LOW); 
  digitalWrite(led8, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led2, HIGH);
} else
  
if(  (distance > 50) && (distance <= 60)  ) 
{
 
  digitalWrite(led10, LOW); 
  digitalWrite(led8, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led2, HIGH);
} else
  
if(  distance > 60 ) 
{
 
  digitalWrite(led10, LOW); 
  digitalWrite(led8, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led2, LOW);
} 
 
}

my else statements aren’t working

What exactly do you mean, not working ?

  if (digitalRead(ECHO) == HIGH)
  {
    return 0; //if echo line is still low from last result,
  }
  return 0;

Your program unconditionally returns at this point so the if/else pairs are never executed.

This is just as well because of

  int distance;
  distance = usonic(11600) / 58;
  if (  (distance > 0) && (distance <= 10)  ) // distance in cm
    digitalWrite(led2, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led10, HIGH);
}

which line or lines of code will be executed if the conditions are met and which will be executed unconditionally ?

Why does the function end on the last line quoted above ? Did you perhaps forget an opening { ?

This all shows up if you use Auto format in the IDE

thank you so much for the response as I am clearly a noob.

can you make any full suggestions or altered code or did you suggest I just put in into arduino format tool?

thanks in advance

and with quote 3 of code I got that from the actual sensors manual so Im not sure why that isn't working?

and im sorry for attaching the actual errors here they are

  94:3: error: expected unqualified-id before 'else'
105:4: error: expected unqualified-id before 'else'
115:3: error: expected unqualified-id before 'else'
125:3: error: expected unqualified-id before 'else'
135:3: error: expected unqualified-id before 'else'

can you make any full suggestions or altered code or did you suggest I just put in into arduino format tool?

Auto format will not fix the problems, it makes the code easier to read and can reveal problems such as functions not starting on the left margin as they should due to missing closing }s

As to fixing the problems

Make sure that you have { and } round your conditional code blocks Make sure that you don't have any unconditional returns in the function before its closing }

Please post a link to where you got the code

https://www.jaycar.com.au/medias/sys_master/images/images/9399252451358/XC4442-manualMain.pdf

that is the link to the code from the manual

ok will try

thanks so much!!

if (  (distance > 0) && (distance <= 10)  ) // distance in cm
 
  digitalWrite(led2, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led10, HIGH);

} else

There is a ‘{’ missing in your ‘if’.

Also note, once you fix the ‘return 0;’ problem, none of the lines after ‘return b;’ will be executed.