Automatic parallel parking

I am doing and automatic parallel parking project where the vehicle moves in a straight line, and using the ultrasonic sensors on its left it scans for parking space and measure if it is big enough to park in it. the image attached is how i set up my vehicle. Below is the code i am testing at the moment.

The components are:

  • Arduino Mega
  • L239d motor shield
  • 4 Dc Motor Robot Kit
  • 4 Ultrasonic sensors
    -infrared sensor
    -7.4v 1100mah battery

there are the connections:
Front Sensor => Trig Pin: D34, Echo Pin: D35

Left Front Sensor => Trig Pin: D36, Echo Pin: D37

Left Rear Sensor => Trig Pin: D38, Echo Pin: D39

Rear Sensor => Trig Pin: D40, Echo Pin: D41

Left Front Engine => M4

Right Front Engine => M3

Left Rear Engine => M1

Right Rear Engine => M2

infrared sensor:
VCC => 5V : OUT => D21 : GND => GND

there was multiple issues at the start:

1-the vehicle was not moving at the start unless i connected it to my laptop
2-the vehicle kept on rebooting once it reached the middle of the parking spot as shown in the image attached (also attached the serial monitor)


for this specific code testing i have removed the infrared sensor to focus on the rebooting issue for now. Use code tags to format code for the forumif possible please help me get rid of this rebooting issue, if any further information is needed please let me know

#incl`Use code tags to format code for the forum`ude <AFMotor.h>
#include "Ultrasonic.h"

AF_DCMotor Left_Rear_Motor(1);
AF_DCMotor Right_Rear_Motor(2);
AF_DCMotor Right_Front_Motor(3);
AF_DCMotor Left_Front_Motor(4);

Ultrasonic ultrasonic_arka(40, 41), ultrasonic_sol_arka(38, 39), ultrasonic_sol_on(36, 37), ultrasonic_on(34, 35);

#define left 0              // left direction command
#define right 1             // right direction command
#define Forward 2           // forward direction command
#define Back 3              // backward direction command
#define minimum_limit 15.6  // Width of the car (cm)
#define minimum_limit1 25.5 // length of the car (cm)

byte park_menu = 0;
int signalpin = 21;
volatile int val;

int sayac = 0;
int current_status = 0;
int previous_state = 0;

/**
 * Counts the number of times a signal is received and prints the count to the serial monitor.
 *
 * @param saydir the number of times to count the signal
 *
 * @throws None
 */
void count(int saydir)
{
    // Initialize variables
    int i = 0;
    int current_status = 0;
    int previous_state = 0;

    // Count the number of signals received
    for (; i <= saydir; ++i)
    {
        // Read the signal pin
        val = digitalRead(signalpin);

        // Update current_status based on signal pin value
        current_status = (val == LOW) ? 0 : 1;

        // Update sayac and print to serial monitor if signal state has changed
        if (current_status != previous_state)
        {
            if (current_status == 1)
            {
                ++sayac;
                Serial.println(sayac);
                ++i;
            }
            previous_state = current_status;
        }

        // Release the motors if the count has reached the desired value
        if (i == saydir)
        {
            Left_Front_Motor.run(RELEASE);
            Right_Front_Motor.run(RELEASE);
            Left_Rear_Motor.run(RELEASE);
            Right_Rear_Motor.run(RELEASE);
        }
    }
}

void motor_pinSetup()
{

    Left_Front_Motor.run(RELEASE);
    Right_Front_Motor.run(RELEASE);
    Left_Rear_Motor.run(RELEASE);
    Right_Rear_Motor.run(RELEASE);
}

/**
 * @brief Function for robot motion
 *
 * @param motor Motor direction
 * @param spd  Motor speed
 */
void Robot_Hareket(byte motor, byte spd)
{
    // Set motor speed
    Left_Front_Motor.setSpeed(spd);
    Right_Front_Motor.setSpeed(spd);
    Left_Rear_Motor.setSpeed(spd);
    Right_Rear_Motor.setSpeed(spd);

    // Set motor direction based on the motor direction parameter
    if (motor == Forward) // Forward motion
    {
        Left_Front_Motor.run(FORWARD);
        Right_Front_Motor.run(FORWARD);
        Left_Rear_Motor.run(FORWARD);
        Right_Rear_Motor.run(FORWARD);
    }
    if (motor == Back) // Backward motion
    {
        Left_Front_Motor.run(BACKWARD);
        Right_Front_Motor.run(BACKWARD);
        Left_Rear_Motor.run(BACKWARD);
        Right_Rear_Motor.run(BACKWARD);
    }
    if (motor == left) // Left motion
    {
        Left_Front_Motor.run(BACKWARD);
        Right_Front_Motor.run(FORWARD);
        Left_Rear_Motor.run(BACKWARD);
        Right_Rear_Motor.run(FORWARD);
    }
    if (motor == right) // Right motion
    {
        Left_Front_Motor.run(FORWARD);
        Right_Front_Motor.run(BACKWARD);
        Left_Rear_Motor.run(FORWARD);
        Right_Rear_Motor.run(BACKWARD);
    }
}

/**
 * Releases all the motors, stopping the robot.
 *
 * This function stops the robot by releasing all the motors.
 * It takes no parameters and returns no values.
 */
void Robot_Halt()
{
    // Release all the motors to stop the robot
    Left_Front_Motor.run(RELEASE);  // Release the left front motor
    Right_Front_Motor.run(RELEASE); // Release the right front motor
    Left_Rear_Motor.run(RELEASE);   // Release the left rear motor
    Right_Rear_Motor.run(RELEASE);  // Release the right rear motor
}

void read_ultrasonic()
{
    long on_Sensor = ultrasonic_on.Ranging(CM);
    long side_ultra1 = ultrasonic_sol_on.Ranging(CM);
    long side_ultra2 = ultrasonic_sol_arka.Ranging(CM);
    Serial.print("front Sensor1: ");
    Serial.print(on_Sensor);
    Serial.print("\t");
    Serial.print("side_ultra1: ");
    Serial.print(side_ultra1);
    Serial.print("\t");
    Serial.print("side_ultra2: ");
    Serial.print(side_ultra2);
    Serial.print("\t");
}
/**
 * Controls the parking space.
 *
 * This function searches for a parking space and updates the park_menu variable accordingly.
 * It takes no parameters and returns the updated park_menu value.
 *
 * @return The updated park_menu value.
 */
bool Parking_Place_Control()
{
    // Read sensor values
    long on_Sensor = ultrasonic_on.Ranging(CM);
    long side_ultra1 = ultrasonic_sol_on.Ranging(CM);
    long side_ultra2 = ultrasonic_sol_arka.Ranging(CM);

    // Check for parking space
    if ((side_ultra1 <= minimum_limit) && (side_ultra2 <= minimum_limit) && (park_menu == 0))
    {
        // read_ultrasonic();
        //  Move forward to find a parking space
        Robot_Hareket(Forward, 120);
        park_menu = 1;
        Serial.print("moving forward : ");
        Serial.println(park_menu);
    }

    if ((side_ultra1 > minimum_limit) && (side_ultra1 < minimum_limit1) && (side_ultra2 > minimum_limit) && (side_ultra2 < minimum_limit1) && (park_menu == 1))
    {
        // read_ultrasonic();
        // Continue moving forward
        Robot_Hareket(Forward, 120);
        park_menu = 2;
        Serial.print("still moving  forward : ");
        Serial.println(park_menu);
    }

    if ((side_ultra1 >= minimum_limit1) && (side_ultra2 >= minimum_limit1) && (park_menu == 1))
    {
        // Stop the robot and make a decision to park
        Serial.println("Stop the robot and make a decision to park");
        // read_ultrasonic();
        Robot_Halt();
        delay(1000);
        Serial.println("turn the robot left and make a decision to park");
        Robot_Hareket(left, 120);
        delay(1000);
        Robot_Halt();
        delay(500);
        park_menu = 3;
        Serial.println(park_menu);
    }

    if ((side_ultra1 <= minimum_limit) && (side_ultra2 <= minimum_limit) && (park_menu == 2))
    {
        // Make a decision to park parallel
        Serial.println("Make a decision to park parallel");
        park_menu = 3;
        Serial.println(park_menu);
    }

    return park_menu;
}

/**
 * This function is responsible for parking the vehicle.
 * It implements the parking logic and returns the current park_menu value.
 *
 * @return The current park_menu value.
 */
long Park_bul()
{
    // Control the parking space
    Parking_Place_Control();

    // Park parallel if space is found
    if (park_menu == 3)
    {
        // Stop the robot and park parallel
        Serial.println("after descion Stop the robot and park parallel ");
        Robot_Halt();
        Serial.println(park_menu);
        delay(400);
        park_menu = 4;
    }

    // Move back and park
    if (park_menu == 4)
    {
        Serial.println("Move back and park");
        Robot_Hareket(Back, 150); // Move back
        count(18);                // Wait for movement to complete
        Robot_Halt();             // Stop the robot
        Serial.println(park_menu);
        delay(500);
        Robot_Hareket(right, 150); // Move right
        count(9);                  // Wait for movement to complete
        Robot_Halt();              // Stop the robot
        delay(500);
        park_menu = 5;
    }

    // Measure distance and check if parallel parking is done
    if (park_menu == 5)
    {
        Serial.println("Measure distance and check if parallel parking is done");
        Robot_Hareket(Back, 120);                       // Move back
        long arka_Sensor = ultrasonic_arka.Ranging(CM); // Measure distance
        Serial.println(arka_Sensor);

        // If distance is within range, park is done
        if (arka_Sensor > 0 && arka_Sensor <= 13)
        {
            Robot_Halt();
            delay(400);
            park_menu = 6;
        }
        return arka_Sensor; // Return the measured distance
    }

    // Park facing left
    if (park_menu == 6)
    {
        Serial.println("Park facing left");
        Robot_Hareket(left, 150);                         // Move left
        long side_ultra1 = ultrasonic_sol_on.Ranging(CM); // Measure distance
        Serial.println(side_ultra1);
        long side_ultra2 = ultrasonic_sol_arka.Ranging(CM); // Measure distance
        Serial.println(side_ultra2);

        // If both distances are equal, parking is done
        if (side_ultra1 == side_ultra2)
        {
            Robot_Halt();
            park_menu = 7;
        }

        return side_ultra1, side_ultra2; // Return both distances
    }

    // Check if parking is done
    if (park_menu == 7)
    {
        Serial.println("Check if parking is done");
        long on_Sensor = ultrasonic_on.Ranging(CM); // Measure distance

        // If distance is within limit, parking is done
        if (on_Sensor <= 6)
        {
            Robot_Halt();
            park_menu = 8;
        }
        else
        {
            Robot_Hareket(Forward, 120); // Move forward
        }
        return on_Sensor; // Return the measured distance
    }

    // If parking is done, move left to complete the parking
    if (park_menu == 10)
    {
        Serial.println("If parking is done, move left to complete the parking");
        Robot_Hareket(left, 150); // Move left
        count(14);                // Wait for movement to complete
        Robot_Halt();             // Stop the robot
        delay(500);
        park_menu = 7;
    }
    return park_menu; // Return the current park_menu value
}

void setup()
{
    Serial.begin(9600);
    // attachInterrupt(5, count, CHANGE);
    pinMode(signalpin, INPUT);

    motor_pinSetup();
    /*
        // test out the ultrasonic sensors and the motors*/
    // testMotors();
    // testUltrasonic();
    Left_Front_Motor.run(RELEASE);
    Right_Front_Motor.run(RELEASE);
    Left_Rear_Motor.run(RELEASE);
    Right_Rear_Motor.run(RELEASE);
    Serial.println("rebooted or booting...setup done Starting...");
}

void loop()
{
    // Park_bul();
    //  Read sensor values
    long on_Sensor = ultrasonic_on.Ranging(CM);
    long side_ultra1 = ultrasonic_sol_on.Ranging(CM);
    long side_ultra2 = ultrasonic_sol_arka.Ranging(CM);

    // Check for parking space
    if ((side_ultra1 <= minimum_limit) && (side_ultra2 <= minimum_limit))
    {
        if (park_menu == 0)
        {
            read_ultrasonic();
            //  Move forward to find a parking space
            Robot_Hareket(Forward, 180);
            park_menu = 1;
            Serial.print("moving forwards : ");
            Serial.println(park_menu);
        }
    }
    if (side_ultra1 >= minimum_limit && side_ultra2 >= minimum_limit && on_Sensor > 10)
    {
        if (park_menu == 1)
        {
            // Stop the robot and make a decision to park
            Serial.println("Stop the robot and make a decision to park");
            // delay(1000);
            read_ultrasonic();
            // Robot_Halt();
            delay(1000);
            Serial.print("turn the robot left and make a decision to park  :");
            Robot_Hareket(left, 190);
            // delay(5000);
            // Robot_Halt();
            delay(500);
            park_menu = 3;
            Serial.println(park_menu);
        }
    }

    if (park_menu == 3 && on_Sensor < 10)
    {
        Serial.println("front sensor detected wall stop");
        Robot_Halt();
        park_menu = 4;
    }
}

void testMotors()
{
    Left_Front_Motor.setSpeed(120);
    Right_Front_Motor.setSpeed(120);
    Left_Rear_Motor.setSpeed(120);
    Right_Rear_Motor.setSpeed(120);
    Left_Front_Motor.run(RELEASE);
    Right_Front_Motor.run(RELEASE);
    Left_Rear_Motor.run(RELEASE);
    Right_Rear_Motor.run(RELEASE);
    uint8_t i;

    // Motor spinning clockwise
    Left_Front_Motor.run(FORWARD);
    Right_Front_Motor.run(FORWARD);
    Left_Rear_Motor.run(FORWARD);
    Right_Rear_Motor.run(FORWARD);

    // Speed up
    for (i = 0; i < 255; i++)
    {
        Left_Front_Motor.setSpeed(i);
        Right_Front_Motor.setSpeed(i);
        Left_Rear_Motor.setSpeed(i);
        Right_Rear_Motor.setSpeed(i);

        delay(10);
    }

    // Speed down
    for (i = 255; i != 0; i--)
    {
        Left_Front_Motor.setSpeed(i);
        Right_Front_Motor.setSpeed(i);
        Left_Rear_Motor.setSpeed(i);
        Right_Rear_Motor.setSpeed(i);
        delay(10);
    }

    // Motor spinning anti-clockwise
    Left_Front_Motor.run(BACKWARD);
    Right_Front_Motor.run(BACKWARD);
    Left_Rear_Motor.run(BACKWARD);
    Right_Rear_Motor.run(BACKWARD);

    // Speed up
    for (i = 0; i < 255; i++)
    {
        Left_Front_Motor.setSpeed(i);
        Right_Front_Motor.setSpeed(i);
        Left_Rear_Motor.setSpeed(i);
        Right_Rear_Motor.setSpeed(i);
        delay(10);
    }

    // Speed down
    for (i = 255; i != 0; i--)
    {
        Left_Front_Motor.setSpeed(i);
        Right_Front_Motor.setSpeed(i);
        Left_Rear_Motor.setSpeed(i);
        Right_Rear_Motor.setSpeed(i);
        delay(10);
    }

    // Now turn off motor
    Left_Front_Motor.run(RELEASE);
    Right_Front_Motor.run(RELEASE);
    Left_Rear_Motor.run(RELEASE);
    Right_Rear_Motor.run(RELEASE);
    delay(1000);
}

void testUltrasonic()
{
    while (1)
    {
        // Read sensor values
        long on_Sensor = ultrasonic_on.Ranging(CM);
        long side_ultra1 = ultrasonic_sol_on.Ranging(CM);
        long side_ultra2 = ultrasonic_sol_arka.Ranging(CM);
        long arka_Sensor = ultrasonic_arka.Ranging(CM);

        Serial.print("On Sensor1: ");
        Serial.print(on_Sensor);
        Serial.print("\t");
        Serial.print("Sag Sensor2: ");
        Serial.print(side_ultra1);
        Serial.print("\t");
        Serial.print("Sag Arka Sensor3: ");
        Serial.print(side_ultra2);
        Serial.print("\t");
        Serial.print("Arka Sensor:4 ");
        Serial.println(arka_Sensor);
        delay(1000); // delay for 1 sec
    }
}

Welcome to the forum.

You might want to look at this How to get the best out of this forum before you proceed any further.

You can not run a motor directly from an Arduino pin, first and foremost because there is not enough current from an output pin to drive motor. You will damage your Arduino if you insist on this.

Second all DC motors need a reverse biased diode to prevent voltage flyback damaging your Arduino.

Third posting a screen dump of anything is useless and just a waste of time. Just copy and past anything you want us to see. Just like you posted the code.

Finally I moved your post here because it has nothing to do with boot loaders.

When the Mega is powered from USB, you need to remove the Yellow jumper on the motor shield. When you disconnect the USB then you should replace the Yellow jumper.

You have to know how to parallel park before you try giving instructions to parallel park. It's not about "enough space," rather the right amount of steering at the right time.

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