Help with Boolean Operator

#include <NewPing.h>
#include <FastLED.h>
 
#define TRIGGER_PIN  4
#define ECHO_PIN     3
#define MAX_DISTANCE 379

#define NUM_LEDS 60
#define DATA_PIN 2                                                                       

CRGB leds[NUM_LEDS]; // Define the array of leds
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
void setup() {
  Serial.begin(115200);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
 
void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_in());
  Serial.println("in");


  
  if (sonar.ping_in() <= 40)  {
  
for(int dot = 0; dot < NUM_LEDS; dot++) { 
  leds[dot]= CRGB(20,0,25);
  FastLED.show();
  delay(10);
  }

}
 
else if (sonar.ping_in() >= 41) {
  
  fill_solid( &(leds[0]), NUM_LEDS, CRGB::Black);
 FastLED.show();
}
}

I’m trying to insert a value range, so that an ultrasonic sensor responds if a value <=40, but >0 is detected. I think the solution is to insert a boolean operator like: (a>= 10 && a <=20)

However, I keep getting an error. I would appreciate any help/input. Thanks

(deleted)

Sorry, I must have accidentally deleted the text.

This is the error:

expected identifier before “(”

This is what I tried:

if (sonar.ping_in() <= 40) && (sonar.ping_in() > 0)

I’m new to working with arduino and am still trying to figure out how to properly code

You need one extra set of parenthesis:
if ((sonar.ping_in() <= 40) && (sonar.ping_in() > 0))

The entire conditional has to be in parens.

I suggest that you assign the return value of sonar.ping_in() to a variable first and use that variable in the condition.

Think about what needs to happen if two successive calls return slightly different results (e.g. 40 and 41)?

I suggest that you assign the return value of sonar.ping_in() to a variable first and use that variable in the condition.

Think about what needs to happen if two successive calls return slightly different results (e.g. 40 and 41)?

Forgive my ignorance on the matter, but how exactly would I go about doing this?

I read this , but don't quite understand it

int distance = sonar.ping_in()

then use the distance variable in the subsequent range tests so that the value does not change between the reads used in the tests.

thank you, so much