What's the matter to control vibrating motors by 2 sonar sensors

I want to make a code that makes motors vibrating by two sonar sensors. The sensor will be located in the right and left side. Each side can read the distance they are toward. 1 Motor vibrates in 300cm, 2 motors vibrate in 200cm , 4 motors vibrate in 100 cm.
here are my codes

#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 400 // 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.
int motor_1m1 = 10;   
int motor_2m1 = 9;
int motor_3m1 = 8;
int motor_4m1 = 7;

int motor_1m2 = 14;  
int motor_2m2 = 13;
int motor_3m2 = 12;
int motor_4m2 = 11;

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;
 pinMode(motor_1m1, OUTPUT); 
 pinMode(motor_2m1, OUTPUT); 
 pinMode(motor_3m1, OUTPUT); 
 pinMode(motor_4m1, OUTPUT);
 pinMode(motor_1m2, OUTPUT);  
 pinMode(motor_2m2, OUTPUT); 
 pinMode(motor_3m2, OUTPUT); 
 pinMode(motor_4m2, OUTPUT); 
}

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).


   }
   if(cm[SONAR_NUM] >= 300){
  digitalWrite(motor_1m1, LOW);
  digitalWrite(motor_2m1, LOW);
  digitalWrite(motor_3m1, LOW);
  digitalWrite(motor_4m1, LOW);
    digitalWrite(motor_1m2, LOW);
  digitalWrite(motor_2m2, LOW);
  digitalWrite(motor_3m2, LOW);
  digitalWrite(motor_4m2, LOW);
 } 
   else if(cm[SONAR_NUM] >=200 && cm[SONAR_NUM] < 300){ 
  digitalWrite(motor_1m1, LOW);
  digitalWrite(motor_2m1, LOW);
  digitalWrite(motor_3m1, LOW);
  digitalWrite(motor_4m1, HIGH);
    digitalWrite(motor_1m2, LOW);
  digitalWrite(motor_2m2, LOW);
  digitalWrite(motor_3m2, LOW);
  digitalWrite(motor_4m2, HIGH);
 }
 else if(cm[SONAR_NUM] >=100 && cm[SONAR_NUM] < 200){ 
  digitalWrite(motor_1m1, LOW);
  digitalWrite(motor_2m1, LOW);
  digitalWrite(motor_3m1, HIGH);
  digitalWrite(motor_4m1, HIGH);
     digitalWrite(motor_1m2, LOW);
  digitalWrite(motor_2m2, LOW);
  digitalWrite(motor_3m2, HIGH);
  digitalWrite(motor_4m2, HIGH);
 }
 else if(cm[SONAR_NUM] >=0 && cm[SONAR_NUM] < 100){ 
  digitalWrite(motor_1m1, HIGH);
  digitalWrite(motor_2m1, HIGH);
  digitalWrite(motor_3m1, HIGH);
  digitalWrite(motor_4m1, HIGH);
     digitalWrite(motor_1m2, HIGH);
  digitalWrite(motor_2m2, HIGH);
  digitalWrite(motor_3m2, HIGH);
  digitalWrite(motor_4m2, HIGH);
 }
 

 }
 // Other code that *DOESN'T* analyze ping results can 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.
 // The following code would be replaced with your code that does something with the ping results.
 for (uint8_t i = 0; i < SONAR_NUM; i++) {
   Serial.print(i);
   Serial.print("=");
   Serial.print(cm[i]);
   Serial.print("cm ");
}
 Serial.println();
 
}

It can read the distances by 2sensors but motors doesn’t work.
plz help me.

Please use [ code ] tags. Some of your text is [ i ] italic.

for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;

In this for loop i will only ever be 1

if (millis() >= pingTimer[i]) {        // Is it this sensor's time to ping?I doubt that it will matter but this will fail when millis() rolls over to zero.

As to the motors. How are they wired and powered and do they work if you simply apply the appropriate signal levels to their inputs ?

   if(cm[SONAR_NUM] >= 300){

Wrong!!! You are reading beyond the end of the cm array.

In this for loop i will only ever be 1

Unless OP adds another sonar. That code is fine. Its the rubbish after that that is the problem.

UKHeliBob:

for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.

pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;


In this for loop i will only ever be 1

`if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?`I doubt that it will matter but this will fail when millis() rolls over to zero.

As to the motors. How are they wired and powered and do they work if you simply apply the appropriate signal levels to their inputs ?

:motors worked well in simple condition. I used NEWPING code. So maybe there’s no error in standard code. maybe errors are in what I made.

I changed my code a little bit. I moved the code what I made. because of this sentence what master left in this code “// The following code would be replaced with your code that does something with the ping results.”

// ---------------------------------------------------------------------------
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
// would normally process the sensor results in this function (for example, decide if a robot needs to
// turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
// ---------------------------------------------------------------------------
#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 150 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo). 원래 33 핑인터벌.

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.
int motor_1m1 = 10;   //변수 motor_5cm에 9번핀 설정
int motor_2m1 = 9;
int motor_3m1 = 8;
int motor_4m1 = 7;

int motor_1m2 = 14;   
int motor_2m2 = 13;
int motor_3m2 = 12;
int motor_4m2 = 11;

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;
     pinMode(motor_1m1, OUTPUT);  
  pinMode(motor_2m1, OUTPUT); 
  pinMode(motor_3m1, OUTPUT);  
  pinMode(motor_4m1, OUTPUT); 
  pinMode(motor_1m2, OUTPUT); 
  pinMode(motor_2m2, OUTPUT); 
  pinMode(motor_3m2, OUTPUT);  
  pinMode(motor_4m2, OUTPUT); 
}

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).
    }
  }
  // Other code that *DOESN'T* analyze ping results can 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.
  // The following code would be replaced with your code that does something with the ping results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {

if(cm[SONAR_NUM] >= 300)
{  digitalWrite(motor_1m1, LOW);
   digitalWrite(motor_2m1, LOW);
   digitalWrite(motor_3m1, LOW);
   digitalWrite(motor_4m1, LOW);
     digitalWrite(motor_1m2, LOW);
   digitalWrite(motor_2m2, LOW);
   digitalWrite(motor_3m2, LOW);
   digitalWrite(motor_4m2, LOW);
  } 
    else if(cm[SONAR_NUM] >=200 && cm[SONAR_NUM] < 300){ 
   digitalWrite(motor_1m1, LOW);
   digitalWrite(motor_2m1, LOW);
   digitalWrite(motor_3m1, LOW);
   digitalWrite(motor_4m1, HIGH);
     digitalWrite(motor_1m2, LOW);
   digitalWrite(motor_2m2, LOW);
   digitalWrite(motor_3m2, LOW);
   digitalWrite(motor_4m2, HIGH);
  }
  else if(cm[SONAR_NUM] >=100 && cm[SONAR_NUM] < 200){ 
   digitalWrite(motor_1m1, LOW);
   digitalWrite(motor_2m1, LOW);
   digitalWrite(motor_3m1, HIGH);
   digitalWrite(motor_4m1, HIGH);
      digitalWrite(motor_1m2, LOW);
   digitalWrite(motor_2m2, LOW);
   digitalWrite(motor_3m2, HIGH);
   digitalWrite(motor_4m2, HIGH);
  }
  else if(cm[SONAR_NUM] >=0 && cm[SONAR_NUM] < 100){ 
   digitalWrite(motor_1m1, HIGH);
   digitalWrite(motor_2m1, HIGH);
   digitalWrite(motor_3m1, HIGH);
   digitalWrite(motor_4m1, HIGH);
      digitalWrite(motor_1m2, HIGH);
   digitalWrite(motor_2m2, HIGH);
   digitalWrite(motor_3m2, HIGH);
   digitalWrite(motor_4m2, HIGH);
  }
    
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  
  }
  Serial.println();
  
}

but it always doesnt work. … help me

but it always doesnt work. .. help me

It does something. You didn't explain what it does.
You expect it to do something. You didn't explain what you expect.

So, how can we?