newping and led on(rear sensor)

Hello, I'm using 2 sonar sensor and 6 leds. I want to make a rear sensor. The sonar sensors will be located right and left. These will detect their side and turn on 1 led when something in 100cm from a sonar sensor. if something is in 50cm, 2 leds on and if it is in 30 cm 3 leds on.

I want to make this process in a arduino code by using newping. I've made some code before but that is not precise. how can I make this code by using below code? PLZ help me

#include <NewPing.h>

#define SONAR_NUM     2 // Number or sensors.
#define MAX_DISTANCE 150 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(41, 42, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(43, 44, MAX_DISTANCE),
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // The rest of your code would go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();
}
  // The rest of your code would go here.

But, you don't have any code here. Why not? Isn't this where you are supposed to turn some pins on?

Which sensor are you using? What does "that is not precise" mean?

actually, I explained my code by leds instead of coin motors. because this code is so long. we use two sonar sensors and 16 coin motors. I fixed my code. you can understand motors are same with leds. this code what I made shows 'not precise' result.

#include <NewPing.h>

#define MAX_DISTANCE 150 

int motor_1m1 = 7;  
int motor_2m1 = 8;  
int motor_3m1 = 9; 
 
int motor_1m2 = 11;   
int motor_2m2 = 12; 
int motor_3m2 = 13;  

NewPing sonar1(3,4, MAX_DISTANCE);  
NewPing sonar2(5,6, MAX_DISTANCE);  

void setup() {
  Serial.begin(115200); 
  pinMode(motor_1m1, OUTPUT); 
  pinMode(motor_2m1, OUTPUT);
  pinMode(motor_3m1, OUTPUT);  

  pinMode(motor_1m2, OUTPUT);  
  pinMode(motor_2m2, OUTPUT);
  pinMode(motor_3m2, OUTPUT); 
  
}

void loop() {
  unsigned int uS1 = sonar1.ping_median(); 
  int value1 = uS1 / US_ROUNDTRIP_CM;
  unsigned int uS2 = sonar2.ping_median(); .
  int value2 = uS2 / US_ROUNDTRIP_CM; 

if(value1 >= 100){  
   digitalWrite(motor_1m1, LOW);
   digitalWrite(motor_2m1, LOW);
   digitalWrite(motor_3m1, LOW);
   
  } 

    else if(value1 >=50 && value1 < 100) {
   digitalWrite(motor_1m1, LOW);
   digitalWrite(motor_2m1, LOW);
   digitalWrite(motor_3m1, HIGH);
  
  }
  else if(value1 >=30 && value1 < 50) 
 {
   digitalWrite(motor_1m1, LOW);
   digitalWrite(motor_2m1, HIGH);
   digitalWrite(motor_3m1, HIGH);
   
  }
  else if(value1 >=1 && value1 < 30) 
 {
   digitalWrite(motor_1m1, HIGH);
   digitalWrite(motor_2m1, HIGH);
   digitalWrite(motor_3m1, HIGH);
 
  }

  if(value2 >= 100)  
 {
   digitalWrite(motor_1m2, LOW);
   digitalWrite(motor_2m2, LOW);
   digitalWrite(motor_3m2, LOW);
   
  } 
   else if(value2 >=50 && value2 <100) 
 {
   digitalWrite(motor_1m2, LOW);
   digitalWrite(motor_2m2, LOW);
   digitalWrite(motor_3m2, HIGH);
   
  }
  else if(value2 >=30 && value2 < 50) 
 {
   digitalWrite(motor_1m2, LOW);
   digitalWrite(motor_2m2, HIGH);
   digitalWrite(motor_3m2, HIGH);
   
  }
  else if(value2 >=1 && value2 < 30) 
 {
   digitalWrite(motor_1m2, HIGH);
   digitalWrite(motor_2m2, HIGH);
   digitalWrite(motor_3m2, HIGH);
  
  }

 {
  Serial.print("value1: ");
  Serial.print(uS1 / US_ROUNDTRIP_CM); 
  Serial.println("cm"); 
  delay(500); 
  Serial.print("value2: "); 
  Serial.print(uS2 / US_ROUNDTRIP_CM); 
  delay(500);
 }

So. I want to make a code using 15 sonar code in newping library.

this code what I made shows 'not precise' result.

It might, but I don't know what sensor you are using, so I can not replicate the "not precise" result. Nor can I determine what "not precise" means.

I use two 'SRF05' sensors. And 'Not precise' means leds(motors) responds slowly when the object is detected. and changed the range from 100,50,10 to 300,200,100 if(value1 >= 100)
it is not precise more than 250cm range.

And 'Not precise' means leds(motors) responds slowly when the object is detected. and changed the range from 100,50,10 to 300,200,100

How long does it take to send a pulse, and get a response, when the nearest object is 300 centimeters away? How long does it take to do that 5 times? Per sensor.

Well, do a little math.
In SI units with dry air at 20°C (68°F), the speed of sound c is 343 meters per second (m/s).
Or 3430 cm/S.
So 600 cm round trip: 600 cm/(3430cm/S) = .175S, 175mS, 175000uS

You can read micros() before and after each write/read to see how long it's taking.