Ultrasonic sensor signal keeps dropping, HELP

i am making an animatronic rainbow stage beetle that will follow light. I am using to PR sensors for light, one ultrasonic sensors for obstacle detecting. the signal keeps dropping and I dont want to rely on AI to help fix my problem. Heres my code. HELP ME PLSSSSSSS.

#include <Servo.h>

Servo left;

Servo right;

int trigPin = 9;

int echoPin = 10;

int PRpin = A5;

int PRpin2 = A4;

long duration;

int distance;

int PRvalue;

int PRvalue2;

int baselineLight = 0; // for the photoresistors to take a baseline reading of the light.

int currentLight = 0;

int threshold = 50;

void setup() {

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(PRpin, INPUT);

pinMode(PRpin2, INPUT);

Serial.begin(9600);

right.attach(A0);

left.attach(A2);

Serial.print("Calibrating, please wait.");

delay(3000);

baselineLight = analogRead(PRpin);

Serial.print("Baseline light level set:");

Serial.print (baselineLight);

}

void loop() {

digitalWrite(trigPin, LOW);// let it send out the signal

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn (echoPin, HIGH, 30000);

if (distance > 0) {

distance = (duration/2)/29.1;

Serial.print("Distance");

Serial.print(distance);

Serial.println(" cm");

} else {

distance = 401;

}

delay(100);

PRvalue = analogRead(PRpin);

PRvalue2 = analogRead(PRpin2);

Serial.print ("Current light:");

Serial.println(PRvalue);

Serial.print("Current light2:");

Serial.println(PRvalue2);

delay(1000);

if (distance <= 10){

right.write(90);

left.write(90);

}

else if (distance <=400 && distance >= 100){

right.write(0);

left.write(180);

}

else if (PRvalue > PRvalue2){

right.write(90);

left.write(180);

}

else if(PRvalue < PRvalue2){

right.write(0);

left.write(180);

}

delay(3000);

}

What signal and what does "dropping" mean, and how do you know this is what is happening?

In the serial moniter, i have it coded to where it can tell me the distance in CM. However, the distance number keeps dropping form 250 cm to 0 periodically, causing the robot to stop. it doesnt stay stopped, it starts up after a little while but the number and signal always drops to 0.

First things first. You need to properly post your code so that it is readable and without errors caused by pasting.

In the IDE click on Edit, then click on Copy for Forum, that will actually copy your code for posting on this forum. Then come back here and just do a paste. Notice a difference now?

// #include <NewPing.h>
#include <Servo.h>
Servo left;
Servo right;
int trigPin = 9; 
int echoPin = 10;
int PRpin = A5;
int PRpin2 = A4;
long duration; 
int distance;
int PRvalue;
int PRvalue2;
int baselineLight = 0; // for the photoresistors to take a baseline reading of the light.
int currentLight = 0;
int threshold = 50;






void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(PRpin, INPUT);
pinMode(PRpin, INPUT);
Serial.begin(9600);

right.attach(A0);
left.attach(A2);
Serial.print("Calibrating, please wait.");

delay(3000);

baselineLight = analogRead(PRpin);
Serial.print("Baseline light level set:");
Serial.print (baselineLight);
}


void loop() {
digitalWrite(trigPin, LOW);// let it send out the signal
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn (echoPin, HIGH, 30000);

distance = (duration/2)/29.1;
if (distance > 0) {
 
Serial.print("Distance");
Serial.print(distance);
Serial.println(" cm");
} else {
  distance = 401;
}

delay(100);


PRvalue = analogRead(PRpin);
PRvalue2 = analogRead(PRpin2);
Serial.print ("Current light:");
Serial.println(PRvalue);
Serial.print("Current light2:");
Serial.println(PRvalue2);
delay(1000);

if (distance <= 10){
right.write(90);
left.write(90);
}

else if (distance <=400 && distance >= 100){
  right.write(0);
  left.write(180);
  
}

else if (PRvalue > PRvalue2){
  right.write(90);
  left.write(180);
}

else if(PRvalue < PRvalue2){
  right.write(0);
  left.write(180);
}

delay(3000);

}



Very nice!
Now I can copy and paste into my IDE

thanks jim-p!!!

Which arduino are you using?

arduino IDE 2.3.9, there was a new update days ago.

Which board, uno, mega?
What do mean the signal keeps dropping, does the beetle crash into things?

I am using uno, the beetle doesnt really crash into things but it does completley stop whenever the signal drops. I can include a screenshot of the serial moniter where you can see the distance number go from 185 to 0 without turning off the beetle.

what i mean by signal drops is that in the serial moniter, you can see the distance value which is in the 100s or 200s, then the number just drops to a 0, stopping everything, then it starts back up again.

i was advised to restructure my code to make it more cohesive so that the robot doesnt crash out over jumbled instructions, but I struggling to understand how to make it as cohesive and organized as possible

Your code only prints the distance if it's greater than 0 and if it was 0 you make it 401.
Are you sure you posted the right code?

the serial moniters shows its current distance when theres nothing in front of it. so it shows a triple digit. if the value was less then 10, then the robot will stop. that part that you were talking about was code i found online, but since it didnt fix anything i comma'd it out.

Post the code you are actually using that does not work.
Also when you say PIR do you mean LDR (Light, Dependant Resistor)?

Does the beetle follow a light that you move around?

Are there motors?

If I recall correctly, a time-out will return a -1. Why are you not checking for a sensor time-out?

That is a lifetime learning experience.

Try this code first to see if your ultrasonic sensor is working correctly. Move you hand back and forth and see if the distance is correct.

// Jim-P
// Speed of sound in air at 25C is 343m/s
// or 0.343mm/us or 0.0343 cm/us

// Change to whatever pins you are using
#define trigger_pin 2
#define echo_pin 3

unsigned long echo_time;
float distance;

void setup()
{
  pinMode(trigger_pin, OUTPUT);
  pinMode(echo_pin, INPUT);
  digitalWrite(trigger_pin, LOW);
  delay(2);
  Serial.begin(9600);
  Serial.println("Started");
}

void loop()
{
  digitalWrite(trigger_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_pin, LOW);
  echo_time = pulseIn(echo_pin, HIGH, 30000);
  distance = (echo_time / 2) / 29.1; // distance in cm
  
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.print("      Time = ");
  Serial.println(echo_time);

  delay(250);
}
//

Your 9vdc battery can not move two 360 servos at full speed for very long.

Rather than using "0" (full CC) and "180" (full CW), try something close to "90" (stop)

Running Current (at no load): 100mA (4.8v) / 120mA (6v)
Stall Current: 550mA (4.8v) / 650mA (6v)