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;
}