How to make a part of code constanly in the background

i have this code for and Arduino robot an i need the wall sensing thingy to always be updating every second or when needed
this is the code that i have so far.

#include <NewPing.h>

#define ECHO_PIN 5
#define TRIGGER_PIN 4
 
const int motor_pin1 = 7;
const int motor_pin2 = 6;
const int motor_pin3 = 8;
const int motor_pin4 = 9;

int detection = 10;
int leftdetection = 0;
int rightdetection = 0;
int number = 0;
int vall = 0;

NewPing DistanceSensor(TRIGGER_PIN, ECHO_PIN);

 unsigned int cm = DistanceSensor.ping_cm();

void setup () {
   Serial.begin (9600);
   pinMode(motor_pin1, OUTPUT);
   pinMode(motor_pin2, OUTPUT);
   pinMode(motor_pin3, OUTPUT);
   pinMode(motor_pin4, OUTPUT);
   pinMode(TRIGGER_PIN, OUTPUT);
   pinMode(ECHO_PIN, INPUT);
}


void loop () {
  unsigned int cm = DistanceSensor.ping_cm();
  number = cm;
  Serial.println(cm);
  
  if (number > detection){
   forward();
  }
 if (number < detection)
  {
    stop();
    delay(1000);
   DetectPath();
  }
}


void DetectPath () {
 digitalWrite(motor_pin2, HIGH);
 digitalWrite(motor_pin3, HIGH);
 delay(1000);
   Serial.println(cm);
  leftdetection = number;
  delay(1000);
 digitalWrite(motor_pin2, LOW);
 digitalWrite(motor_pin3, LOW);
 //turn left 90 degreas
 
 digitalWrite(motor_pin4, HIGH);
 digitalWrite(motor_pin1, HIGH);
 delay(1000);
  digitalWrite(motor_pin4, HIGH);
 digitalWrite(motor_pin1, HIGH);
 delay(1000);
  // straten up
 delay(500);
 digitalWrite(motor_pin4, LOW);
 digitalWrite(motor_pin1, LOW);
 delay(1000);
 Serial.println(cm);
 rightdetection = number;
 //turn right 90 degrese
 digitalWrite(motor_pin2, HIGH);
 digitalWrite(motor_pin3, HIGH);
 delay(1000);
 digitalWrite(motor_pin2, LOW);
 digitalWrite(motor_pin3, LOW);
 //straten up 
//////////////////////////////////////////////////////


  if (leftdetection > rightdetection){
  digitalWrite(motor_pin1, HIGH);
  delay(1000);
  digitalWrite(motor_pin1, LOW);
  }
  else if (rightdetection > leftdetection) {
  digitalWrite(motor_pin3, HIGH);
  delay(1000);
  digitalWrite(motor_pin3, LOW);
  }
  loop ();
}

void turnleftstay() {
  digitalWrite(motor_pin1, HIGH);
  digitalWrite(motor_pin3, HIGH);
  delay(1000);
}

void turnrightstay () {
  digitalWrite(motor_pin2, HIGH);
  digitalWrite(motor_pin4, HIGH);
  delay(1000);
}

void forward() {
  digitalWrite(motor_pin2, HIGH);
  digitalWrite(motor_pin4, HIGH);
  }
  
 void stop () {
    digitalWrite(motor_pin2, LOW);
  digitalWrite(motor_pin4, LOW);
  }

Assuming there’s actually a question in there somewhere, do you mean you want this line:

unsigned int cm = DistanceSensor.ping_cm();

… to be called every second?

First remember it’s running every time thru loop() anyway.

Second, you can call things on any interval you like by using millis() and comparing the time it last fired to the time now. That is well documented in the Blink Without Delay example in the IDE: Examples > 2. Digital

You have a bunch of delays in there.

Remember, every time you reach a "delay" the arduino doesn't do ANYTHING except wait for the delay to pass. If you put a delay in there of 10000 then nothing happens for 10 seconds. So for each delay you have in there, you lose a second of activity.

Bittsen: You have a bunch of delays in there.

Remember, every time you reach a "delay" the arduino doesn't do ANYTHING except wait for the delay to pass. If you put a delay in there of 10000 then nothing happens for 10 seconds. So for each delay you have in there, you lose a second of activity.

And the student becomes the teacher, exactly as it should be.... ;)

Ultimately you need to use "states". This is a way of executing little pieces of code each loop and not causing excessive delays each loop..

If you look at this simple wall-avoiding sketch: https://github.com/solderspot/WallieBot/blob/master/WallBot_v1/WallBot_v1.ino you can see that the code is broken up into the states: IDLE, TURNING, MOVING, STOPPING.

So instead of having long sequences of explicit code, your logic is now controlled through variables that trigger which pieces of code need to be executed. The code is then said to be state driven and involves a lot more thought and work.

The code is then said to be state driven and involves a lot more thought and work.

You put the thought into the design of the program before you write it. This should always be the case but it is all too easy to dive in and start writing code only to find later that you are "lost in a maze of twisty passages".

Breaking the code into sections that are only executed when a controlling variable has a particular value allows you to isolate problem areas and test the code in any/all states of your choice. I much prefer a state driven program (often called a state machine) written using switch/case, to spaghetti code full of if/else constructs, often nested to obscurity, which are difficult to understand and debug.

Overall more thought and work ? No, I don't think so.

Overall more thought and work ? No, I don't think so.

Of course, for experience programmers we strive to minimize chaos and confusion. But for people starting out programming, learning to think modular and state driven takes time and effort. My point to the OP was that he's going to have to think about and design his states and just get used to thinking differently about how to code his solution. It's not as simple as throwing in some variables.

In no way am I trying to disparage people from good software practices. Sorry if I implied that.

he's going to have to think about and design his states and just get used to thinking differently about how to code his solution

Agreed, but with some changes.

He's going to have to think about and design his program and get used to thinking about how to code his solution. Of course, that applies however it is coded.