Running ultrasonic sensors without a delay into multiple DC motors and Servo's

Hey guys,

im currently building a robot to move and lift a payload off of a platform. i am struggling to figure out how to create an object avoidance robot that will work without delays. If i use a regular newping, it pings every 10 seconds or so (how long it takes the sketch to loop around), any pointers?

This code has my attempt at utilising the NewPing Event Timer library. i couldnt figure out how to use that to create a distanceInCms object.

#include <Servo.h>

#include <NewPing.h>
#define trigger_pin 10
#define echo_pin 9
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 500


Servo servo_scissor;
Servo servo_armswivel;
Servo servo_forkswivel;

int pos = 0;
int angleInDegrees = 0;
int distanceInCms = 0;
int distanceInCmsCliff = 0;



NewPing sonar(trigger_pin, echo_pin, MAX_DISTANCE);

NewPing sonar_cliff(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);


unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
  unsigned long pingTimer;     // Holds the next ping time.
  
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 7;
int in1 = 6;
int in2 = 5;
// motor two
int enB = 2;
int in3 = 4;
int in4 = 3;

int state = 0;


void setup()
{
  Serial.begin(115200);
  
  // set all the motor control pins to outputs
  servo_scissor.attach(A1);  // attaches the servo on pin a2 to the servo object
  servo_armswivel.attach(A2); //attached to pin analog 1
  servo_forkswivel.attach(A3);

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  analogWrite(enA, 0);
  analogWrite(enB, 0);

}
void loop() {

  
 if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
  }

distanceInCms = sonar.ping_timer();
/*
  delay(50);
/*
  Serial.print("ping:");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
  /*
  Serial.print("cliffping:");
  Serial.print(sonar_cliff.ping_cm());
  Serial.println("cm");servo_scissor.write(50);
*/
  switch (state)
  {
    case 0:
      forwards(255);
      servo_scissor.write(81);
      servo_forkswivel.write(120);
      servo_armswivel.write(10);

      if (distanceInCms < 25)
      {
        servo_forkswivel.write(22);

      }
      {
        state = 1;
      }
      break;


    case 1:
      turnRight(255);
      delay(2000);
      {
        forwards(255);
        if (distanceInCms <25);
        {
          turnLeft(255);
          delay(1000);
          servo_armswivel.write(90);
          servo_forkswivel.write(70);
        }
          {
            state = 2;
          }
      }

      break;

    case 2:

      turnRight(255);
      delay(2000);
      if (distanceInCms < 15)
      {
        backwards(255);
        delay(2000);
        servo_armswivel.write(30);
        delay(200);
        servo_armswivel.write(120);
        delay(200);
        servo_forkswivel.write(120);
        delay(200);
        servo_forkswivel.write(20);
      }
      {
        state = 4;
      }

      break;

      
    case 4:
{
     Stop();
      
      break;
  }
}

}

void forwards(int speed)
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, speed);
  analogWrite(enB, speed);
}
void turnRight(int speed)
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in4, HIGH);
  digitalWrite(in3, LOW);
  analogWrite(enA, speed);
  analogWrite(enB, speed);

}

void turnLeft(int speed)
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in4, HIGH);
  digitalWrite(in3, LOW);
  analogWrite(enA, speed);
  analogWrite(enB, speed);

}

void backwards(int speed)
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in4, LOW);
  digitalWrite(in3, HIGH);
  analogWrite(enA, speed);
  analogWrite(enB, speed);

}

void Stop()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in3, LOW);
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
  // Don't do anything here!
  if (sonar.check_timer()) { // This is how you check to see if the ping was received.
    // Here's where you can add code.
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
    Serial.println("cm");
  }
  // Don't do anything here!
}

Thanks again!

You can use millis() to work out the time loop(), or parts of it take.
https://www.arduino.cc/reference/en/language/functions/time/millis/

If you don't want to have delays you need to get rid of all the calls to delay() that you have.