Problem with ultrasonic distance error 999

hello there! im building sumo bot with two ultrasonics 1 line sensor with 5 heads and 2 ir sensors, voltage regulator, motor driver, arduino uno and sensor shield v5.0, two motors. when i test the ultrasonics it constantly says 999. what may be cause? how can i fix it? is it a code or something else?
im an amateur in these, just curios and want to finish it my only helper is gemini tho.

// ==========================================
//      ULTIMATE SUMO ROBOT BATTLE CODE
// ==========================================

// --- 1. MOTOR PINS (PWM Speed Control) ---
// Left Motor
const int ENA = 5;  // Speed
const int IN1 = 6;  // Direction 1
const int IN2 = 7;  // Direction 2

// Right Motor
const int IN3 = 8;  // Direction 1
const int IN4 = 9;  // Direction 2
const int ENB = 10; // Speed

// --- 2. SENSOR PINS ---
// Dual Ultrasonic Eyes
const int TRIG_L = 3;
const int ECHO_L = 4;
const int TRIG_R = A3; // Using Analog pin as Digital
const int ECHO_R = A4; // Using Analog pin as Digital

// Back Edge Sensors
const int BACK_IR_L = 2;
const int BACK_IR_R = 12;

// Front 5-Channel Line Sensor
const int LINE_1 = A0; // Far Left
const int LINE_2 = A1; // Left
const int LINE_3 = A2; // Center
const int LINE_4 = A5; // Right
const int LINE_5 = 11; // Far Right

// --- 3. SETTINGS & CALIBRATION ---
const int ATTACK_SPEED = 255;  // Max Power (0-255)
const int TURN_SPEED = 180;    // Turning Speed
const int SEARCH_SPEED = 150;  // Scouting Speed
const int ATTACK_DIST = 40;    // Distance to spot enemy (cm)

// LINE SENSOR LOGIC
// Check your specific sensors! 
// Usually: Seeing WHITE (Line) = LOW (0), Seeing BLACK (Floor) = HIGH (1).
// If your robot reverses on black, change this to HIGH.
const int LINE_FOUND = LOW; 

void setup() {
  // Motor Output Setup
  pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);

  // Sensor Setup
  pinMode(TRIG_L, OUTPUT); pinMode(ECHO_L, INPUT);
  pinMode(TRIG_R, OUTPUT); pinMode(ECHO_R, INPUT);
  
  pinMode(BACK_IR_L, INPUT); pinMode(BACK_IR_R, INPUT);
  
  pinMode(LINE_1, INPUT); pinMode(LINE_2, INPUT); pinMode(LINE_3, INPUT);
  pinMode(LINE_4, INPUT); pinMode(LINE_5, INPUT);

  // === SAFETY DELAY (REQUIRED FOR SUMO) ===
  // Robot must wait 5 seconds after switch on before moving.
  delay(5000); 
}

void loop() {
  // --- STEP A: READ ENVIRONMENT ---
  
  // Check Line Sensors (Returns true if line is seen)
  bool f1 = digitalRead(LINE_1) == LINE_FOUND;
  bool f2 = digitalRead(LINE_2) == LINE_FOUND;
  bool f3 = digitalRead(LINE_3) == LINE_FOUND;
  bool f4 = digitalRead(LINE_4) == LINE_FOUND;
  bool f5 = digitalRead(LINE_5) == LINE_FOUND;
  
  bool bL = digitalRead(BACK_IR_L) == LINE_FOUND;
  bool bR = digitalRead(BACK_IR_R) == LINE_FOUND;

  // Check Enemy Distances
  int distL = getDistance(TRIG_L, ECHO_L);
  int distR = getDistance(TRIG_R, ECHO_R);

  // --- STEP B: DECISION MAKING (PRIORITY LIST) ---

  // 1. SURVIVAL: FRONT EDGE DETECTED
  if (f1 || f2 || f3 || f4 || f5) {
    // Stop -> Reverse Fast -> Spin away
    stopMotors();
    moveBackward(255);
    delay(400); // Reverse for 0.4 seconds
    moveLeft(255); // Spin around
    delay(200);
  }
  
  // 2. SURVIVAL: BACK EDGE DETECTED
  else if (bL || bR) {
    // Stop -> Charge Forward
    stopMotors();
    moveForward(255);
    delay(400);
  }

  // 3. ATTACK: ENEMY ON LEFT
  else if (distL > 0 && distL < ATTACK_DIST) {
    moveLeft(TURN_SPEED); // Turn towards enemy
    delay(100);
    moveForward(ATTACK_SPEED); // RAM!
  }

  // 4. ATTACK: ENEMY ON RIGHT
  else if (distR > 0 && distR < ATTACK_DIST) {
    moveRight(TURN_SPEED); // Turn towards enemy
    delay(100);
    moveForward(ATTACK_SPEED); // RAM!
  }

  // 5. SEARCH: NO ENEMY, NO LINE
  else {
    // Standard Strategy: Spin in place to find someone
    moveLeft(SEARCH_SPEED);
  }
}

// --- HELPER FUNCTIONS ---

void moveForward(int speed) {
  analogWrite(ENA, speed);
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  analogWrite(ENB, speed);
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}

void moveBackward(int speed) {
  analogWrite(ENA, speed);
  digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
  analogWrite(ENB, speed);
  digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}

void moveLeft(int speed) {
  // Tank Turn (Left wheels back, Right wheels forward)
  analogWrite(ENA, speed);
  digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
  analogWrite(ENB, speed);
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}

void moveRight(int speed) {
  // Tank Turn (Left wheels forward, Right wheels back)
  analogWrite(ENA, speed);
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  analogWrite(ENB, speed);
  digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}

void stopMotors() {
  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
}

int getDistance(int trig, int echo) {
  digitalWrite(trig, LOW); delayMicroseconds(2);
  digitalWrite(trig, HIGH); delayMicroseconds(10);
  digitalWrite(trig, LOW);
  
  long duration = pulseIn(echo, HIGH, 6000); // 6ms timeout (approx 1 meter)
  if (duration == 0) return 999; // No object
  return duration * 0.034 / 2;
}

Being an amateur, does that mean you actually tested EACH of your devices, individually, before trying to combine them? Did each work properly?

1 Like

did not tested them individually actually

  • Did you write this sketch ?

its my first attept to work with arduino and coding also i just gave directions to gemini with which pins are connecting to the parts and it wrote its code so just a directions

So, how will a helper know where to start helping?

1 Like
  • As mentioned, to narrow down the problem, check each section of code individually.
1 Like

how will i know which section triggers the sensors or anything

  • First things first, Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.

  • We suggest for each component/sensor/etc., write a sketch that just tests that hardware part.
    When you have proven the Arduino can read/write to the hardware, then and only then start writing the project sketch.

  • For example, if a switch requires a pull-up resistor and you don’t have one, you will see intermittent values when the switch is read.

  • You might find a special library is required or the example sketch that comes with a library cannot detect a device.

  • What does this imply ?
    long duration = pulseIn(echo, HIGH, 6000); // 6ms timeout (approx 1 meter)
    if (duration == 0) return 999; // No object
    return duration * 0.034 / 2;

2 Likes

Welcome!

While you are at it have Gemini generate an annotated schematic so we can better understand how you have wired your project, this was suggested previously by @LarryD. Be sure to show all connections, power sources, grounds etc. Also posting links to each of the hardware devices will help solve your project problems. Then test each individual function as asked by @Paul_KD7HB, reason we have no idea if it is wired properly or will work. I am assuming you are using an UNO as you did not specify which Arduino you are using, all of this is important.

1 Like

Hi, @sababero
Welcome to the forum.

Can I suggest you use the NewPing Library for your ultrasonics.
The library even has an example of using more than one unit.
It does all the calculations for you.
https://docs.arduino.cc/libraries/newping/

Tom.... :smiley: :+1: :coffee: :australia:

1 Like