HC-SR04 wont continue working after one loop

So the code is copied below but im unsure how to keep the sensor checking for objects once it has done one loop of the MoveRobot function. Once this function is complete, the serial monitor/sensor is locked to a distance of 24 and I have to re-upload the code for it to work again.

I have tried a while and a for loop to no avail, any help would be greatly appreciated

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


#include <BraccioRobot.h>
#include <Servo.h>
#include <Braccio.h>

Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;

if (safetyDistance <= 5){
  MoveRobot();
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
  return(loop());
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Position pos, down, up, pos2;
void MoveRobot() {
  // Calculates the distance based on the duration of the echo pulse
  distance = duration * 0.034 / 2;
  safetyDistance = distance;
  if (safetyDistance <= 5) {
    //     (M1,  M2,  M3,  M4, M5,  M6)
    BraccioRobot.init();
  pos.set( 90,  90, 45, 45,  90,  20);
  down.set( 90, 90, 22, 22,  90,  70);
  up.set(90, 90, 90, 90, 90, 73);
  pos2.set(90, 90, 10, 10, 90, 20);

  // Move the robot to the position with a specified speed between 20-200 degrees per second
  BraccioRobot.moveToPosition(pos, 25);  

  //Wait 5 seconds
  delay(5000);

  BraccioRobot.moveToPosition(down.set(90, 90, 10, 10, 90, 20), 50);

  delay(5000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 50);

  //Wait 2 seconds
  delay(2000);

  BraccioRobot.moveToPosition(up, 50);

  delay(2000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 25);

  BraccioRobot.moveToPosition(pos2, 50);
  digitalWrite(ledPin, HIGH);
  }
  else {
    BraccioRobot.moveToPosition(up, 25);
    digitalWrite(ledPin, LOW); 
  }
 }

This line is between void loop() and moveRobot() so it never gets seen:

Position pos, down, up, pos2;

Was it meant to be in a function?

You read duration:

duration = pulseIn(echoPin, HIGH);

Then calculate distance:

  // Calculating the distance
  distance = duration * 0.034 / 2;

Then assign distance to safetyDistance

  safetyDistance = distance;

Then recalculate distance:

 distance = duration * 0.034 / 2;

Then re-assign distance to safetyDistance

  safetyDistance = distance;

Somewhere in the copy/paste, things did not go well.

BTW, do not use delay( ) as nothing else happens during these periods.

re-calculation was an error, as I have multiple test files trying a few things, fixed it now but still the same issues of once its done its movement, the serial monitor sticks to a value of 24

Fixed cop/paste:

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


#include <BraccioRobot.h>
#include <Servo.h>
#include <Braccio.h>

Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;

if (safetyDistance <= 5){
  MoveRobot();
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
  return(loop());
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

void MoveRobot() {
  Position pos, down, up, pos2;
    //     (M1,  M2,  M3,  M4, M5,  M6)
    BraccioRobot.init();
  pos.set( 90,  90, 45, 45,  90,  20);
  down.set( 90, 90, 22, 22,  90,  70);
  up.set(90, 90, 90, 90, 90, 73);
  pos2.set(90, 90, 10, 10, 90, 20);

  // Move the robot to the position with a specified speed between 20-200 degrees per second
  BraccioRobot.moveToPosition(pos, 25);  

  //Wait 5 seconds
  delay(5000);
  

  BraccioRobot.moveToPosition(down.set(90, 90, 10, 10, 90, 20), 50);

  delay(5000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 50);

  //Wait 2 seconds
  delay(2000);

  BraccioRobot.moveToPosition(up, 50);

  delay(2000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 25);

  BraccioRobot.moveToPosition(pos2, 50);
  digitalWrite(ledPin, HIGH);
  }
 

I know. Always re-compile after a copy/paste.

Okay. After you fixed it, how is it doing?

Maybe start with their example:

The example is fine and all, just using basic movements to test rn. after fixing the few errors still does the same. Think what I need is a poop somewhere so that once the function is done, it loops back to keep checking the sensor value instead of locking it at 24

Thank you!! I’ll get rid of them but just using a basic movement to test out the sensor and try get the loop to work as intended

The measurement (sonar) is done inside the void loop(), so you already have a loop going. Check for issues like the code out-of-sight of any function and the re-re-defining. Just as the example works on one thing (position), make one part of the robot work, then move to the next part, and continue. The best way to do that is to do sub-movements inside their own function, then when you need that function (for example; turn_left_wrist, all you need to do is call it, and maybe pass an amount of "twist."

Right last post I made, very helpful replies although I don’t think I explained it as best as I could, basically to put short, once I place my hand in front of the sensor, it starts the movement (MoveRobot), once this is completed, unless I re-upload the code, it will constantly read a value of 24 on the serial monitor.
I put my hand in front or not the value stays at 24. If I re-upload, it works fine, bouncing between 50-60cm until I make it drop to below 5.

I guess what I’m trying to get at is why it locks itself, I’ve tried multiple ideas, loops, functions etc nothings seems to work for me. Thought I could get more insight (again last post very helpful replies, this post might explain better)


// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


#include <BraccioRobot.h>
#include <Servo.h>
#include <Braccio.h>

Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;

if (safetyDistance <= 5){
  MoveRobot();
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
  return(loop());
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

void MoveRobot() {
  Position pos, down, up, pos2;
    //     (M1,  M2,  M3,  M4, M5,  M6)
    BraccioRobot.init();
  pos.set( 90,  90, 45, 45,  90,  20);
  down.set( 90, 90, 22, 22,  90,  70);
  up.set(90, 90, 90, 90, 90, 73);
  pos2.set(90, 90, 10, 10, 90, 20);

  // Move the robot to the position with a specified speed between 20-200 degrees per second
  BraccioRobot.moveToPosition(pos, 25);  

  //Wait 5 seconds
  delay(5000);
  

  BraccioRobot.moveToPosition(down.set(90, 90, 10, 10, 90, 20), 50);

  delay(5000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 50);

  //Wait 2 seconds
  delay(2000);

  BraccioRobot.moveToPosition(up, 50);

  delay(2000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 25);

  BraccioRobot.moveToPosition(pos2, 50);
  digitalWrite(ledPin, HIGH);
  }

An int.

with float math

// Calculating the distance
distance= duration*0.034/2;

may not give the desired result set.

The distance doesn’t have to be perfect for me yet, just that once the first movement is done, that’s it the code won’t repeat until I re-upload and just chills at a value of 24 and I don’t get why it doesn’t loop back around

What value stays at 24?

Ok so the code is not looping, you say.
Lets test that

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


#include <BraccioRobot.h>
#include <Servo.h>
#include <Braccio.h>

Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {

Serial.println( "LOOP");
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;

if (safetyDistance <= 5){
  MoveRobot();
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
  return(loop());
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

void MoveRobot() {
  Position pos, down, up, pos2;
    //     (M1,  M2,  M3,  M4, M5,  M6)
    BraccioRobot.init();
  pos.set( 90,  90, 45, 45,  90,  20);
  down.set( 90, 90, 22, 22,  90,  70);
  up.set(90, 90, 90, 90, 90, 73);
  pos2.set(90, 90, 10, 10, 90, 20);

  // Move the robot to the position with a specified speed between 20-200 degrees per second
  BraccioRobot.moveToPosition(pos, 25);  

  //Wait 5 seconds
  delay(5000);
  

  BraccioRobot.moveToPosition(down.set(90, 90, 10, 10, 90, 20), 50);

  delay(5000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 50);

  //Wait 2 seconds
  delay(2000);

  BraccioRobot.moveToPosition(up, 50);

  delay(2000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 25);

  BraccioRobot.moveToPosition(pos2, 50);
  digitalWrite(ledPin, HIGH);
  }

With the above code does the word "LOOP" get printed out once or lots of times?

hmm so it does print out loop, even when the value gets stuck to 24, so I'm wondering what the issue could be. The loop is fine it just kind of gets stuck for some reason.
what the serial monitor is reading is below, 6 is when I put my hand in front of it, the 24 happens once the movement is done

Distance: 33
LOOP
Distance: 33
LOOP
Distance: 32
LOOP
Distance: 6
LOOP
LOOP
Distance: 24
LOOP
Distance: 24
LOOP
Distance: 24
LOOP
Distance: 24
LOOP
Distance: 24
LOOP

Good question but now we know that loop() is functioning.

lets see where the 24 is coming from

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


#include <BraccioRobot.h>
#include <Servo.h>
#include <Braccio.h>

Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() 
{

Serial.println( "LOOP");
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
Serial.print( "duration ");
Serial.print( duration );

Serial.print( " distance ");
Serial.print( distance );
Serial.println();

delay(50); //used for troubleshooting
/*
if (safetyDistance <= 5){
  MoveRobot();
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
  return(loop());
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
*/
}

void MoveRobot() {
  Position pos, down, up, pos2;
    //     (M1,  M2,  M3,  M4, M5,  M6)
    BraccioRobot.init();
  pos.set( 90,  90, 45, 45,  90,  20);
  down.set( 90, 90, 22, 22,  90,  70);
  up.set(90, 90, 90, 90, 90, 73);
  pos2.set(90, 90, 10, 10, 90, 20);

  // Move the robot to the position with a specified speed between 20-200 degrees per second
  BraccioRobot.moveToPosition(pos, 25);  

  //Wait 5 seconds
  delay(5000);
  

  BraccioRobot.moveToPosition(down.set(90, 90, 10, 10, 90, 20), 50);

  delay(5000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 50);

  //Wait 2 seconds
  delay(2000);

  BraccioRobot.moveToPosition(up, 50);

  delay(2000);

  BraccioRobot.moveToPosition(pos.set(90, 90, 10, 10, 90, 70), 25);

  BraccioRobot.moveToPosition(pos2, 50);
  digitalWrite(ledPin, HIGH);
  }

does that print 24's over and over again? What duration is being printed?

Did you try anything that I suggested in the post you abandoned?

This is a duplicate posting?

Crap it is.

I'm out.

yes like I said in the post, all was tried nothing seemed to work, I thanked all the people for the helpful replies, bc it steered me in the right direction just that nothing seems to be working, I try start from scratch I guess

What is a duplicate post am not sure. Sorry if it is a bad thing Im not well versed in forums

So I see in your other thread no one pointed you here:
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681317/2

Welcome, by the way, I see no one said that, either!

It's helpful if you keep posting in the same thread, so helpers don't have to re-question you for all the fundamentals of your problem.

Thanks