How can I make my Arduino robot calculate and follow the shortest path on a 5x6 grid using sensors?

Hi Community I was given a task today,The lecturer build a maze and we saw it.

The task is that your code should contain a 2D Array of the maze, the robot should then go through the maze without touching the walls and always choosing the shortest path..

It is an arduino on a kuongshun AD118 robot.

It uses the Ultra Sonic sensors to navigate itself to avoid the walls and to detect an opening

Is there someone that can help me from scratch please..

#include <Arduino.h>

// Grid size
const int WIDTH = 5;
const int HEIGHT = 6;

// 0 = free space, 1 = wall
// Origin (0,0) = bottom-left
int grid[HEIGHT][WIDTH] = {
  {0,1,1,1,1}, // y = 0
  {1,1,0,0,1}, // y = 1
  {1,1,0,0,1}, // y = 2
  {0,1,1,1,1}, // y = 3
  {0,1,1,1,1}, // y = 4
  {0,0,0,1,1}  // y = 5
};

// Starting and goal coordinates
int startX = 0, startY = 5;
int goalX  = 2, goalY  = 5;

// Simple direction control
enum Direction {NORTH, EAST, SOUTH, WEST};
Direction heading = NORTH;

// Move 1 cell = 35 cm
void moveForward() {
  Serial.println("Moving forward 35cm...");
  delay(800); // Replace with motor move
}

void turnLeft() {
  Serial.println("Turning left 90°...");
  delay(400); // Replace with motor turn
}

void turnRight() {
  Serial.println("Turning right 90°...");
  delay(400); // Replace with motor turn
}

// Check if a cell is inside grid and open
bool isOpen(int x, int y) {
  return (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT && grid[y][x] == 0);
}

// Basic simulation of moving step by step from start to goal
void simulatePath() {
  Serial.println("=== START PATH SIMULATION ===");
  int x = startX;
  int y = startY;

  // Print grid for debugging
  for (int row = HEIGHT - 1; row >= 0; row--) {
    for (int col = 0; col < WIDTH; col++) {
      if (col == x && row == y) Serial.print("S ");
      else if (col == goalX && row == goalY) Serial.print("G ");
      else Serial.print(grid[row][col] == 0 ? ". " : "# ");
    }
    Serial.println();
  }

  Serial.println("Finding a simple path...");

  // Super simple path logic (example movement)
  // You can later replace this with A* if needed
  while (x != goalX || y != goalY) {
    if (x < goalX && isOpen(x + 1, y)) x++;
    else if (y > goalY && isOpen(x, y - 1)) y--;
    else if (y < goalY && isOpen(x, y + 1)) y++;
    else if (x > goalX && isOpen(x - 1, y)) x--;
    else {
      Serial.println("No path found!");
      return;
    }

    Serial.print("→ Step to (");
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Serial.println(")");
    moveForward();
  }

  Serial.println("=== GOAL REACHED! ===");
}

void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("Simplified Grid Navigation Demo");
  simulatePath();
}

void loop() {
  // Nothing here; just one run for demo
}

From scratch means you begin with the Ultra Sonic sensor and being able to detect walls and openings, not playing with grids and matrices.

Start by reading the pinned post re 'How to get the most from the forum'.
Do NOT use Dropbox, we have no idea what virus is hiding there, just post the code after you Auto Format it from the Tools menu then paste it here in code tags.
We are not a free design or coding shop, you do the work and when you get stuck making it work we can look at it and make a suggestion.

This is an important problem, and plenty of reading and viewing material has been posted on line for your edification. The most popular approach is called "flood fill".

To find examples, use the search phrase "robot algorithms solve maze".

Also covered in this excellent youtube video on the MicroMouse contest: https://www.youtube.com/watch?v=ZMQbHMgK2rw

From the description given it seems that the code contains an array holding the design of the array so there is no need to solve it as such, but to navigate through it

Having said that, the maze layout in the array posted makes no sense

@dykly Please clarify whether the robot is required to solve an unknown maze or to navigate through a maze whose design is already known

Finding the "shortest path" requires that the maze be examined in detail. Flood fill is one approach.

Hi, Yes The maze is already known

No, I mean From the Include part..

// 0 = free space, 1 = wall
// Origin (0,0) = bottom-left
int grid[HEIGHT][WIDTH] = {
  {0,1,1,1,1}, // y = 0
  {1,1,0,0,1}, // y = 1
  {1,1,0,0,1}, // y = 2
  {0,1,1,1,1}, // y = 3
  {0,1,1,1,1}, // y = 4
  {0,0,0,1,1}  // y = 5
};

I am probably misinterpreting the meaning of the array but at first sight I don't see how the robot can get out of the bottom/left corner of the maze

Getting from the start position to the goal position is simply 2 moves to the right, so it is soluble in this example:

However, this comment appears to be incorrect:

It should read top-left, I think.

Presumably the code will need to solve a maze which is less trivial but still soluble.

I agree, flood fill algo is the way to go.

How was the "grid" array in the code determined?

Does it correctly represent the maze that the instructor showed you?

Oh, so the ultrasonic sensor is already to go and can detect the walls and openings. I thought you were building the system from the top down, sorry, my error.

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