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
}