I am trying to make a soccer bot and the arduino is making a beeping sound now and nothing is working

i am new over here and and i am trying to make a soccer bot with 2 ir sensor and a ultrasonic sensor using two motors to steer the robot and i am using a l293d ic to drive the motors this is not working and arduino is making a strange beeping sound

i took help of chat gpt as well please help me troubleshoot
but nothing is working please help

#include <Servo.h>
#include <math.h>

const int motor1Pin1 = 5;
const int motor1Pin2 = 6;
const int motor2Pin1 = 9;
const int motor2Pin2 = 10;
const int trigPin = 3;
const int echoPin = 4;
const int irFront = A0;
const int irBack = A1;

Servo kicker;

// Goalpost Coordinates (Assume center of one side)
const float goalX = 0;
const float goalY = 100;

// Bot Position and Orientation (starting values)
float botX = 0;
float botY = 0;
float botAngle = 0; // Bot's orientation in degrees

void setup() {
    // Motor Pins
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(irFront, INPUT);
    pinMode(irBack, INPUT);
    kicker.attach(7);
    kicker.write(0);
    delay(500);
    Serial.begin(9600);
    Serial.println("Arduino ready!");
}

long measureDistance() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    long duration = pulseIn(echoPin, HIGH);
    return duration * 0.034 / 2;
}

void moveBot(int leftSpeed, int rightSpeed) {
    if (leftSpeed > 0) {
        analogWrite(motor1Pin1, leftSpeed);
        analogWrite(motor1Pin2, 0);
    } else {
        analogWrite(motor1Pin1, 0);
        analogWrite(motor1Pin2, -leftSpeed);
    }

    if (rightSpeed > 0) {
        analogWrite(motor2Pin1, rightSpeed);
        analogWrite(motor2Pin2, 0);
    } else {
        analogWrite(motor2Pin1, 0);
        analogWrite(motor2Pin2, -rightSpeed);
    }
}

void alignWithBall() {
    int frontSignal = analogRead(irFront);
    int backSignal = analogRead(irBack);

    while (abs(frontSignal - backSignal) > 50) { // Threshold for alignment
        if (frontSignal > backSignal) {
            moveBot(100, -100); // Rotate clockwise
        } else {
            moveBot(-100, 100); // Rotate counterclockwise
        }
        delay(100);
        frontSignal = analogRead(irFront);
        backSignal = analogRead(irBack);
    }
    moveBot(0, 0); // Stop once aligned
}

void moveToBall() {
    long distance = measureDistance();
    while (distance > 20) { // Stop 20cm from the ball
        moveBot(200, 200); // Move forward
        distance = measureDistance();
        delay(100);
    }
    moveBot(0, 0); // Stop at the ball
}

void turnTowardGoal() {
    // Calculate angle to the goalpost
    float deltaX = goalX - botX;
    float deltaY = goalY - botY;
    float targetAngle = atan2(deltaY, deltaX) * 180 / PI; // Convert radians to degrees

    // Rotate bot to face the goal
    while (abs(targetAngle - botAngle) > 5) { // Allowable angle error
        if (targetAngle > botAngle) {
            moveBot(100, -100); // Rotate clockwise
        } else {
            moveBot(-100, 100); // Rotate counterclockwise
        }
        delay(100);
        botAngle += (targetAngle > botAngle) ? 5 : -5; // Simulated update
    }
    moveBot(0, 0); // Stop once aligned
}

void kickBall() {
    kicker.write(90); // Move servo to the "kick" position
    delay(300);       // Hold for a short moment
    kicker.write(0);  // Return to the resting position
    delay(500);       // Allow time to reset
}

void moveLeft() {
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}

void moveRight() {
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}

void moveForward() {
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
}
void moveBackward() {
  digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
}

void stopMotors() {
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
}

void loop() 
{
    if (Serial.available()) 
    {
        char x = Serial.read(); // Read character from ESP8266
        Serial.print("Received: ");
        Serial.println(x);
        if (x == 'w') moveForward();
        else if (x == 'a') moveLeft();
        else if (x == 'd') moveRight();
        else if (x == 's') moveBackward();
        else if (x == ' ') kickBall();
    } 
    else 
    {
        int frontSignal = analogRead(irFront);
        int backSignal = analogRead(irBack);

        if (frontSignal > 100 || backSignal > 100) 
        {   // Ball detected
            Serial.println("Ball Detected!");
            alignWithBall(); // Align with the ball using IR sensors
            Serial.println("Aligned with Ball!");

            // Measure distance using the ultrasonic sensor only after alignment
            long distance = measureDistance();
            if (distance > 20) 
            {
                moveForward();
                while (distance > 20) 
                { 
                    delay(100); // Short delays to avoid over-polling
                    distance = measureDistance();
                }
                stopMotors();
            }

            Serial.println("Reached Ball!");
            turnTowardGoal();
            Serial.println("Aligned with Goal!");
            kickBall();
            Serial.println("Kicked the Ball!");
            delay(2000); // Wait for 2 seconds
        } 
        else 
        {
            Serial.println("Searching for Ball...");
            moveBot(100, -100); // Rotate to search
            delay(500);
        }
    }
}

Your Arduino is in pain :slight_smile:

How are you powering the Arduino?
How are you powering the servos?

i am powering the arduino by the power jack
and servos by the 5V and Gnd Pin

I suspect that you're overheating the 5V voltage regulator and hence it's screaming at you. Does the problem go away if you fully disconnect the servos?

You should power the servos from their own power supply, not from the Arduino's 5V pin which can only deliver limited power.

The usual beginner mistake. Let's hope your Arduino survived the abuse!

Always power motors and servos separately, and don't forget to connect the grounds.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.