{"id":28558467,"url":"https://github.com/jun0s2/maze-solving-micro-controller-mouse","last_synced_at":"2026-04-24T23:33:54.809Z","repository":{"id":173039551,"uuid":"465692011","full_name":"Jun0S2/Maze-Solving-Micro-controller-Mouse","owner":"Jun0S2","description":"Maze solving micro mouse project repository","archived":false,"fork":false,"pushed_at":"2022-04-19T18:19:25.000Z","size":875,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-10T08:09:19.926Z","etag":null,"topics":["maze-explorer","microcontroller","robotics"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jun0S2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-03-03T11:31:12.000Z","updated_at":"2022-03-23T14:40:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"0a12ff95-2e1f-4731-a712-d4025847949f","html_url":"https://github.com/Jun0S2/Maze-Solving-Micro-controller-Mouse","commit_stats":null,"previous_names":["jun0s2/maze-solving-micro-controller-mouse"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Jun0S2/Maze-Solving-Micro-controller-Mouse","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0S2%2FMaze-Solving-Micro-controller-Mouse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0S2%2FMaze-Solving-Micro-controller-Mouse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0S2%2FMaze-Solving-Micro-controller-Mouse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0S2%2FMaze-Solving-Micro-controller-Mouse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jun0S2","download_url":"https://codeload.github.com/Jun0S2/Maze-Solving-Micro-controller-Mouse/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jun0S2%2FMaze-Solving-Micro-controller-Mouse/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32245149,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-24T13:21:15.438Z","status":"ssl_error","status_checked_at":"2026-04-24T13:21:15.005Z","response_time":64,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["maze-explorer","microcontroller","robotics"],"created_at":"2025-06-10T08:08:57.062Z","updated_at":"2026-04-24T23:33:54.803Z","avatar_url":"https://github.com/Jun0S2.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Micro-controller Maze Solving Mouse\n\nIn this project, all algorithms are allowed to be used in the creation of the program processed by the microcontroller (MC912C32) in addition to the Dragonflybot board. Programming will be done with Freescale IDE that enables groups to develop embedded applications on HCS12 systems. The micromouse will solve a 6 x 13 maze with some rules discussed below.\n\n## Problem Statement\n\nThe micromouse in 6 x 13 maze is required to reach the center of the maze from bottom left.\nMicromouse must be self-contained without any remote controls and shall not damage or destroy the\nwall of the maze.\n\n![MazeRules](./assets/Rules.JPG)\n\n## Mechanical Platform and Electrical Platform\n\n### Discussion : Advantages and Disadvantages\n\nIn this project, stepper motor instead of DC motor. DC motors are cheaper and easy to use, however they\nare not very accurate and overheat easily when they run too fast compared to stepper motors. Because\nthe ultimate goal when building micromouse is to solve the maze accurately and fast, these features will\nprevent the micromouse from completing the maze in the most optimal manner.\n\n#### Pin-out Diagram showing pins used on HCS12\n\n![Figure1](./assets/Figure1.JPG)\n\n#### Micromouse Schematic\n\n![Figure2](./assets/Figure2.JPG)\n\n## Software Platform\n\n### Beans and Methods Used\n\n1. Project.c\n\n   ```\n   - void pollSensors() : It polls the sensor values from the micromouse\n   - void solveMaze() : Includes all the algorithms and methods to solve the maze for the project, including checking walls, checking left, front, and right, updating the distance values for the map, and checking directions where the micromouse is headed\n   - void powerMotors(int powers) : powers motor\n   - void steps(int numsteps) : methods for steps\n   - void Forward() : move forward\n   - void Left() : move left\n   - void Right() : move right\n   - void Turn() : Turn 180 degrees\n   - main() : It runs the solveMaze() with polling the sensors until the maze find  the destination\n\n   ```\n\n2. Event.c\n   ```\n   - void PID_OnInterrupt() : It implements the PID functions\n   - void MotorR_DriveTimer_OnInterrupt() : Driver timer for right motor\n   - void MotorL_DriveTimer_OnInterrupt() : Driver timer for left motor\n   - void MotorR_DutyTimer_OnInterrupt() : Duty timer for right motor\n   - void MotorL_DutyTimer_OnInterrupt() : Duty timer for left motor\n   ```\n\n## Key Function Descriptions\n\nThe micromouse implements the algorithm where it finds the shortest distance left from the goal. The\ndistance array is initially defined in the code and the code modifies the array by adding 4 to the place it\nwent. This prevents the micromouse from going back in the path it already went. The micromouse\nupdates wall values and uses the shortest distance available until it finds the goal.\n\nFunctions that control the motors, sensors, timers, and interrupts are all required in order to\nimplementate this algorithm. These auxiliary functions are used within the solve maze method\nalgorithm explained above.\n\n### Motor Control\n\nControlling motors in this project was very important for this project because it is the base\nfunction that needs to be used while solving the maze. In order to control the motor, functions\nsuch as power Motors that turns on the power, the steps function that is used for moving the\nmicromouse forward, right, left, and turn around functions are used in the project.\n\n```c\n\n//Motor Methods\nvoid powerMotors(int power){\n   if(power==1){\n    MotorR_EN_PutVal(1);\n    MotorL_EN_PutVal(0x03);\n    Cpu_Delay100US(40);//delay 4ms\n   }else{\n    MotorR_EN_PutVal(0);\n    MotorL_EN_PutVal(0);\n    Cpu_Delay100US(40);//delay 4ms\n   }\n}\n\nvoid steps(int numsteps){\n    int i=0;\n    powerMotors(1);\n    for(i=0; i\u003cnumsteps; i++){\n     motorR=(motorR+1)%8;\n     motorL--;\n     if(motorL\u003c0) motorL=7;\n     Cpu_Delay100US(40);//delay 4ms\n    }\n    powerMotors(0);\n}\n```\n\n[code 1] powerMotors and steps method\n\n```c\nvoid Forward(void) {\n    powerMotors(1);\n    stepsL=-367;\n    stepsR=367;\n    while(stepsR != 0 \u0026\u0026 stepsL != 0) {\n\n    }\n    powerMotors(0);\n}\n\nvoid Right(void) {\n    powerMotors(1);\n    stepsL=-200;\n    stepsR=-200;\n    while(stepsR != 0 \u0026\u0026 stepsL != 0) {\n\n    }\n    powerMotors(0);\n}\n\nvoid Left(void) {\n    powerMotors(1);\n    stepsL=200;\n    stepsR=200;\n    while(stepsR != 0 \u0026\u0026 stepsL != 0) {\n\n    }\n    powerMotors(0);\n}\n\nvoid Turn(void) {\n    powerMotors(1);\n    stepsL=400;\n    stepsR=400;\n    while(stepsR != 0 \u0026\u0026 stepsL != 0) {\n\n    }\n    powerMotors(0);\n}\n\n```\n\n[code 2] Motor direction methods\n\n### Sensors\n\npollSensors() is a crucial function for this project. It detects the distance of the wall that allows\nthe mouse to know where the walls are located in the maze. This is important so the\nmicromouse does not collide with the wall and finds the right direction.\n\n```c\nvoid pollSensors(){\n    Sensor_Measure(1);\n    Sensor_GetValue8(sensors);\n}\n```\n\n[code 3] pollSensors\n\n### Timer and Interrupt\n\nIn event.c, timers for motor such as left and right motor drive timer and duty timer are also\nused.\nWhen a timer interrupt occurs, MotorL_DriveTimer_OnInterrupt and\nmotorR_DriveTimer_OnInterrupt are called only when the interrupt event is enabled\n\n```c\nvoid MotorR_DriveTimer_OnInterrupt(void)\n{\n    /*\n** ===================================================================\n**     Event       :  MotorR_DriveTimer_OnInterrupt (module Events)\n**\n**     Component   :  MotorR_DriveTimer [TimerInt]\n**     Description :\n**         When a timer interrupt occurs this event is called (only\n**         when the component is enabled - \u003cEnable\u003e and the events are\n**         enabled - \u003cEnableEvent\u003e). This event is enabled only if a\n**         \u003cinterrupt service/event\u003e is enabled.\n**     Parameters  : None\n**     Returns     : Nothing\n** ===================================================================\n*/\n\n  if (stepsR \u003e 0) {\n     motorR=(motorR+1)%8;\n     stepsR--;\n  } else if (stepsR \u003c 0) {\n     motorR--;\n     if(motorR\u003c0) motorR=7;\n     stepsR++;\n  }\n\n}\n\n/*\n** ===================================================================\n**     Event       :  MotorL_DriveTimer_OnInterrupt (module Events)\n**\n**     Component   :  MotorL_DriveTimer [TimerInt]\n**     Description :\n**         When a timer interrupt occurs this event is called (only\n**         when the component is enabled - \u003cEnable\u003e and the events are\n**         enabled - \u003cEnableEvent\u003e). This event is enabled only if a\n**         \u003cinterrupt service/event\u003e is enabled.\n**     Parameters  : None\n**     Returns     : Nothing\n** ===================================================================\n*/\nvoid MotorL_DriveTimer_OnInterrupt(void)\n{\n  /* Write your code here ... */\n\n  if (stepsL \u003e 0) {\n     motorL=(motorL+1)%8;\n     stepsL--;\n  } else if (stepsL \u003c 0) {\n     motorL--;\n     if(motorL\u003c0) motorL=7;\n     stepsL++;\n  }\n\n}\n```\n\n[Code4] DriveTimer_OnInterrupt\n\nWhen a timer interrupt occurs, dutytimer_oninterrupt is called only if the interrupt event is\nenabled.\n\n```c\n/*\n** ===================================================================\n**     Event       :  MotorR_DutyTimer_OnInterrupt (module Events)\n**\n**     Component   :  MotorR_DutyTimer [TimerInt]\n**     Description :\n**         When a timer interrupt occurs this event is called (only\n**         when the component is enabled - \u003cEnable\u003e and the events are\n**         enabled - \u003cEnableEvent\u003e). This event is enabled only if a\n**         \u003cinterrupt service/event\u003e is enabled.\n**     Parameters  : None\n**     Returns     : Nothing\n** ===================================================================\n*/\nvoid MotorR_DutyTimer_OnInterrupt(void)\n{\n  /* Write your code here ... */\n\n  if(toggle2){\n    MotorR_WriteBits(0);\n    toggle2=0;\n } else{\n      MotorR_WriteBits(motorSeq[motorR]);\n      toggle2=1;\n }\n\n}\n\n/*\n** ===================================================================\n**     Event       :  MotorL_DutyTimer_OnInterrupt (module Events)\n**\n**     Component   :  MotorL_DutyTimer [TimerInt]\n**     Description :\n**         When a timer interrupt occurs this event is called (only\n**         when the component is enabled - \u003cEnable\u003e and the events are\n**         enabled - \u003cEnableEvent\u003e). This event is enabled only if a\n**         \u003cinterrupt service/event\u003e is enabled.\n**     Parameters  : None\n**     Returns     : Nothing\n** ===================================================================\n*/\nvoid MotorL_DutyTimer_OnInterrupt(void)\n{\n  /* Write your code here ... */\n\n  if(toggle1){\n    MotorL_WriteBits(0);\n    toggle1=0;\n } else{\n      MotorL_WriteBits(motorSeq[motorL]);\n      toggle1=1;\n }\n\n}\n\n```\n\n[Code5]DutyTimer_OnInterrupt\n\n### PID Interrupt\n\nPID interrupt helps the mouse to not collide with the wall by having timer interrupts when the\nthis interrupt is called.\n\n```c\nvoid PID_OnInterrupt(void)\n{\n  //int k = 20;   //change later\n  //int left = sensors[0];\n  //int right = sensors[2];\n  //int error = (left-right)/2;\n  //MotorR_DriveTimer_SetPeriodUS(4000+k*error);\n  //MotorL_DriveTimer_SetPeriodUS(4000-k*error) ;\n\n  int k = 30; //some value you fine tune\n  int ki = 3;\n  int left = sensors[0];\n  int right = sensors[2];\n  int error = (left - right) / 2;\n  if(abs(error) \u003e 20) {\n   k = 0;\n   ki = 0;\n  }\n  else if(abs(error \u003c 200))\n  {\n    k = 0;\n   ki = 0;\n  }\n  integral = integral + error;\n  MotorR_DriveTimer_SetPeriodUS(4000 + (error * k) + (integral * ki));\n  MotorL_DriveTimer_SetPeriodUS(4000 - (error * k) - (integral * ki));\n  if (left == right) {\n    integral = 0;\n  }\n\n}\n```\n\n[Code6] PID_OnInterrupt\n\n### Check Walls\n\nAfter changing the micromouse orientation value relative to the north, it checks for the wall.\nBelow is checking left walls. If the wall is detected, it modifies the distance to the goal\naccordingly and updates the array that contains wall values stored in the map. This is implemented for front and right as well.\n\n```c\n//Check Walls\n     //check left\n     if(sensors[0] \u003e 80)\n     {\n\n       int placeLeft = place;\n       placeLeft = place - 1;\n\n       if(placeLeft \u003c 0) {\n       //was 4 I think this would have caused array index outofbounds\n       placeLeft = 3;\n       }\n\n       maze[mouseX][mouseY][placeLeft] = 1;\n\n     }\n```\n\n[Code7] Check Left\n\n### Making Decisions for next step\n\nThe algorithm first checks where the direction is then check where the micromouse is physical\nheading. For example, if it is moving east and pointing north, it will turn right and go forward to\nmove east. Else if it is already heading east, it will go forward and the same for other directions.\n\n```c\n//if east move\n    if(direction[1] == 1)\n    {\n            //if pointing north\n           if(heading[0] == 1)\n           {\n            Right();\n            Forward();\n           }\n           else if(heading[1] == 1)\n           {\n               // if already pointing east\n               Forward();\n           }\n           else if(heading[2] == 1)\n           {\n               //if pointing south\n               Left();\n               Forward();\n           }\n           else\n           {\n                //if pointing west\n                Turn();\n                Forward();\n           }\n\n           mouseX++;\n           //Need to modify the heading array\n           heading[0] = 0;\n           heading[1] = 1;\n           heading[2] = 0;\n           heading[3] = 0;\n\n    }\n```\n\n[Code8] move east\n\n## Testing\n\nTesting for this project first required many debugging sessions to fix the errors with the code. After the\ncode ran without problem, while observing how the micromouse worked in the maze, many\noptimizations had to occur. PID functions were very inconsistent during the first implementation, the\nmicromouse collided with the wall. This issue was fixed by implementing integration into the control\nscheme. Another behavior that had to be troubleshooted was where it first stuck in an infinite loop of\ngoing back and forth between the goals but by including the position of the goal in the main method the\nproblem was solved.\n\n## Results\n\nMicromouse solved the maze correctly. The result code is included in this repository.\n\n## Conclusion\n\nWith this lab, the group learned about creating an algorithm that solves a maze with a micromouse\nwhere the group can implement sensors, motors, and interrupts. With implementing the algorithm, we\ncould think deeply about how the motors had to be moved in order to make the correct decision with\npid values and recognizing walls with the sensors. Having more working mice, or assigning a mouse per\neach group could help with the project in the future. We issues that were the result of switching back\nand forth between mice.\n\n# Improvements\n\nAfter I took education via Samsung Software Academy for Youth, I decided to tune up the codes that always bothered me. Although I could not test the code in real life below modification enhances code quality and liability.\n\n## Original Code :\n\n### Global Variables\n\n```C\nint heading[4] = {1, 0, 0, 0};\nint mouseX = 0;\nint mouseY = 0;\n\n// Final Project Global Material\nint maze[6][13][4] =\n    {\n        {{0, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {1, 0, 0, 1}},\n        {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}},\n        {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}},\n        {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}},\n        {{0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}},\n        {{0, 1, 1, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}},\n};\nint place;\n\nint mazeDist[6][13] =\n    {\n        {8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8},\n        {7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7},\n        {6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6},\n        {6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6},\n        {7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7},\n        {8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8},\n};\n```\n\n### Algorithms\n\n```C\n// Final Project Methods\nvoid solveMaze()\n{\n    int north = -1;\n    int south = -1;\n    int east = -1;\n    int west = -1;\n    // Larger for distance selection\n    int compare = 1000000;\n    int direction[4] = {0};\n\n    // Just Testing\n    // Forward();\n    // Testing\n\n    pollSensors();\n\n    // Cpu_Delay100US(750);\n\n    // Check Walls\n\n    // modifies orientation value relative to north\n\n    place = -1;\n\n    if (heading[0] == 1)\n    {\n\n        place = 0;\n    }\n    if (heading[1] == 1)\n    {\n\n        // what it was originaly // place = 1;\n        place = 1;\n    }\n    if (heading[2] == 1)\n    {\n\n        // what it was originaly //place = 2;\n        place = 2;\n    }\n    else if (heading[3] == 1)\n    {\n        // was three intially\n        place = 3;\n    }\n\n    // check left\n    if (sensors[0] \u003e 80)\n    {\n\n        int placeLeft = place;\n        placeLeft = place - 1;\n\n        if (placeLeft \u003c 0)\n        {\n            // was 4 I think this would have caused array index outofbounds\n            placeLeft = 3;\n        }\n\n        maze[mouseX][mouseY][placeLeft] = 1;\n    }\n\n    // Testing so that walls are never generated\n\n    // check front\n\n    if (sensors[1] \u003e 90)\n    {\n        int placeFront = place;\n\n        maze[mouseX][mouseY][placeFront] = 1;\n    }\n\n    // check right\n\n    // Had sensor value wrong\n\n    if (sensors[2] \u003e 80)\n    {\n\n        int placeRight = place;\n        placeRight = place + 1;\n\n        if (placeRight \u003e 3)\n        {\n            placeRight = 0;\n        }\n\n        maze[mouseX][mouseY][placeRight] = 1;\n    }\n\n    // Update the array\n\n    // Add two to the current position\n\n    mazeDist[mouseX][mouseY] = ((mazeDist[mouseX][mouseY]) + 4);\n\n    // Case Dead End\n    // Broke this by makeing -3\n    if (((maze[mouseX][mouseY][0]) + (maze[mouseX][mouseY][1]) + (maze[mouseX][mouseY][2]) + (maze[mouseX][mouseY][3])) == 5)\n    {\n        // Now check heading in order to determine which value to moddify the value of the current square to 1 more than the square behind\n\n        // Testing\n\n        // If robot is pointing North\n        if (heading[0] == 1)\n        {\n            mazeDist[mouseX][mouseY] = ((mazeDist[mouseX][mouseY - 1]) + 4);\n        }\n\n        // If robot is pointing East\n        if (heading[1] == 1)\n        {\n            mazeDist[mouseX][mouseY] = ((mazeDist[mouseX - 1][mouseY]) + 4);\n        }\n\n        // If robot is pointing South\n        if (heading[2] == 1)\n        {\n            mazeDist[mouseX][mouseY] = ((mazeDist[mouseX][mouseY + 1]) + 4);\n        }\n\n        // If robot is pointing West\n        if (heading[3] == 1)\n        {\n            mazeDist[mouseX][mouseY] = ((mazeDist[mouseX + 1][mouseY]) + 4);\n        }\n    }\n\n    // Removed Case two trying my modified wallflower\n\n    // move closest square with an emphasis on going left\n    // Make sure the ifs are checking the correct statement\n    // dosent implement left prefference\n\n    direction[0] = 0;\n    direction[1] = 0;\n    direction[2] = 0;\n    direction[3] = 0;\n\n    // check north\n    if (maze[mouseX][mouseY][0] == 0)\n    {\n\n        north = mazeDist[mouseX][mouseY + 1];\n\n        // Testing\n        // Forward();\n\n        // Used for best path slection\n        if (north \u003c compare)\n        {\n            compare = north;\n            direction[0] = 1;\n            direction[1] = 0;\n            direction[2] = 0;\n            direction[3] = 0;\n        }\n    }\n\n    // check south\n    // Add conditional to prevent index out of bounds\n    if (maze[mouseX][mouseY][2] == 0)\n    {\n\n        south = mazeDist[mouseX][mouseY - 1];\n\n        // used for best path selection\n        if (south \u003c compare)\n        {\n            compare = south;\n            direction[1] = 0;\n            direction[0] = 0;\n            direction[2] = 1;\n            direction[3] = 0;\n        }\n    }\n\n    // check east\n\n    if (maze[mouseX][mouseY][1] == 0)\n    {\n\n        east = mazeDist[mouseX + 1][mouseY];\n\n        // used for best path selection\n        if (east \u003c compare)\n        {\n            compare = east;\n            direction[0] = 0;\n            direction[2] = 0;\n            direction[1] = 1;\n            direction[3] = 0;\n        }\n    }\n\n    // check west\n\n    if (maze[mouseX][mouseY][3] == 0)\n    {\n\n        west = mazeDist[mouseX - 1][mouseY];\n\n        // used for best path selection\n        if (west \u003c compare)\n        {\n            compare = west;\n            direction[2] = 0;\n            direction[1] = 0;\n            direction[3] = 1;\n            direction[0] = 0;\n        }\n    }\n\n    // Find which is smallest Already done\n\n    // Testing\n\n    // direction[2] = 1;\n\n    Cpu_Delay100US(750);\n\n    // if want to move north move\n    if (direction[0] == 1)\n    {\n        // if already pointing north\n        if (heading[0] == 1)\n        {\n            Forward();\n        }\n        else if (heading[1] == 1)\n        {\n            // if pointing east\n\n            Left();\n            Forward();\n        }\n        else if (heading[2] == 1)\n        {\n            // if pointing south\n\n            Turn();\n            Forward();\n        }\n        else\n        {\n            // if pointing west\n\n            Right();\n            Forward();\n        }\n\n        mouseY++;\n\n        // Need to modify the heading array\n        heading[0] = 1;\n        heading[1] = 0;\n        heading[2] = 0;\n        heading[3] = 0;\n    }\n\n    // Testing\n    // heading[0] = 1;\n    // direction[1] = 1;\n\n    // if east move\n    if (direction[1] == 1)\n    {\n        // if pointing north\n        if (heading[0] == 1)\n        {\n            Right();\n            Forward();\n        }\n        else if (heading[1] == 1)\n        {\n            // if already pointing east\n            Forward();\n        }\n        else if (heading[2] == 1)\n        {\n            // if pointing south\n            Left();\n            Forward();\n        }\n        else\n        {\n            // if pointing west\n            Turn();\n            Forward();\n        }\n\n        mouseX++;\n        // Need to modify the heading array\n        heading[0] = 0;\n        heading[1] = 1;\n        heading[2] = 0;\n        heading[3] = 0;\n    }\n    // if south move\n    if (direction[2] == 1)\n    {\n        // if pointing north\n        if (heading[0] == 1)\n        {\n            Turn();\n            Forward();\n        }\n        else if (heading[1] == 1)\n        {\n            // if pointing east\n            Right();\n            Forward();\n        }\n        else if (heading[2] == 1)\n        {\n            // if already pointing south\n            Forward();\n        }\n        else\n        {\n            // if pointing west\n            Left();\n            Forward();\n        }\n\n        mouseY--;\n\n        // Need to modify the heading array\n        heading[0] = 0;\n        heading[1] = 0;\n        heading[2] = 1;\n        heading[3] = 0;\n    }\n\n    // if west move\n    if (direction[3] == 1)\n    {\n        // if pointing north\n        if (heading[0] == 1)\n        {\n            Left();\n            Forward();\n        }\n        else if (heading[1] == 1)\n        {\n            // if pointing east\n            Turn();\n            Forward();\n        }\n        else if (heading[2] == 1)\n        {\n            // if pointing south\n            Right();\n            Forward();\n        }\n        else\n        {\n            // if already pointing west\n            Forward();\n        }\n\n        // Need to modify the heading array\n        heading[0] = 0;\n        heading[1] = 0;\n        heading[2] = 0;\n        heading[3] = 1;\n\n        mouseX--;\n        // End\n    }\n    Cpu_Delay100US(1000);\n    return;\n}\n\n```\n\n## Ver2.0\n\n### Global Variable\n\n```C\nint mouseX = 0;\nint mouseY = 0;\n\n// Final Project Global Material\nint map[6][13] = {{0}};\nint dx[4] = {-1, 1, 0, 0}; //동서남북\nint dy[4] = {0, 0, -1, 1};\n```\n\n### Algorithms\n\n```C\n// Final Project Methods\nint nx, ny;\nvoid solveMaze()\n{\n    pollSensors();\n\n    //도착할 때 까지\n    while ((mouseX != 3 \u0026\u0026 mouseY != 6) || (mouseX != = 4 \u0026\u0026 mouseY != 6))\n    {\n        ///사방 탐색\n        for (int d = 0; d \u003c 4; d++)\n        {\n            nx = dx[d] + mouseX; // next direction\n            ny = dy[d] + mouseY;\n            /* array out of index */\n            if (nx \u003c 0 || ny \u003c 0 || nx \u003e= 6 || ny \u003e= 13)\n            {\n                map[mouseX][mouseY] = -1; //현재 위치에서 할수있는게 없음\n                Turn();                   //다시 뒤로간다\n                Forward();                //이전으로 돌아감\n                continue;\n            }\n            /* dead end */\n            else if (sensors[0] \u003e 80 \u0026\u0026 sensors[1] \u003e 90 \u0026\u0026 sensors[2] \u003e 80)\n            {\n                map[mouseX][mouseY] = -1; //현재 위치에서 할수있는게 없음\n                Turn();\n                Forward();\n                continue;\n            }\n            /* 한군데 이상 갈 수 있다. : 동 서 남 북 순으로 traverse하고 옮길 때 마다 1더해진다.*/\n            else\n            {\n                //왼쪽과 앞에 벽이라면오른쪽으로 회전\n                if (sensors[0] \u003e 80 \u0026\u0026 sensors[1] \u003e 90) Right();\n                //왼쪽과 오른쪽이 벽 -\u003e 회전할 필요 x\n                else if (sensors[0] \u003e 80 \u0026\u0026 sensors[2] \u003e 80){}\n                //앞쪽과 오른쪽이 벽 -\u003e 왼쪽으로 회전\n                else if (sensors[1] \u003e 90 \u0026\u0026 sensors[2] \u003e 80) Left();\n                //벽이 한쪽밖에 없거나 아예없으면 벽이 없는쪽 아무대나 괜찮다\n                else\n                {\n                    //벽이 없으면 break -\u003e i가 1 2 3인경우로 가면 된다.\n                    int i;\n                    for (i = 0; i \u003c 3; i++)\n                    {\n                        if (sensors[i] \u003e 80)break;\n                    }\n                    if (i == 1)Left();\n                    else if (i == 2){}\n                    else Right();\n                }\n                // mouseX와 mouseY의 위치에 1을 더했다 (왔다갔다는 뜻)\n                map[mouseX][mouseY]++;\n                // 직진한다.\n                Forward();\n                Cpu_Delay100US(750);\n            }\n        }\n    }\n    return;\n}\n```\n\n### Why Did I make Improvements ?\n\n1. COL : 308 Lines -\u003e 69 Lines\n2. 3 Arrays (heading[4], map[6][13][4], mazeDist[6][13]) -\u003e 3 Arrays(dx[4],dy[4],map[6][13])\n\nInstead of saving headings, directions encountered in map, and distance map,\n\n1. I used dx and dy to search East, West, North, and South\n\n   So I was able to remove heading and simplify map\n\n2. If robot encounters dead ends or wall, update map to -1 else add distance\n\n   In this way, I can remove mazeDistance array.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjun0s2%2Fmaze-solving-micro-controller-mouse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjun0s2%2Fmaze-solving-micro-controller-mouse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjun0s2%2Fmaze-solving-micro-controller-mouse/lists"}