# Autonomous mobile robot with obstacle avoidance

**URL:** <https://forum.arduino.cc/t/autonomous-mobile-robot-with-obstacle-avoidance/985240>\
**Category:** General Guidance\
**Created:** [April 26, 2022, 11:33am UTC](https://forum.arduino.cc/t/autonomous-mobile-robot-with-obstacle-avoidance/985240 "2022-04-26T11:33:55Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [April 21, 2022, 8:08am UTC](https://forum.arduino.cc/t/autonomous-mobile-robot-with-obstacle-avoidance/985240/10 "2022-04-21T08:08:37Z")

</div>

I recommend to take a different Stepper-Motor-library. It is called MobaTools and can be installed from the library-manager inside the Arduino-IDE

The MobaTools offer step-signal-creation "in the background" this means you can execute code in **parallel** to the step-signal-creation.  
The MobaTools offer a function to immidiately stop the stepper-motor regardless of how many steps are "left to do".  
It offers changing the stepper-speed with a function

As a general suggestion: using else or else if-conditions to activate different "operation-modes" require a lot of analysing and a lot of boolean flag-variables.

An easier way to program such things is a state-machine. I want to explain why

If you are driving straight forward you are driving forward straight forward  
If you are driving straight backward you are driving forward straight backward  
If you turn left, you turn left  
If you turn right, you turn right  
etc. etc.

you don't do turning left **and** driving straight forward at the **same time.**

This means you have different operation-modes

1. driving straight forward
2. driving straight backward,
3. turning left
4. turning right
5. stop

a state-machine sets your robot-car into one of these 5 operation-modes

A state-machine enables to execute code **mutually exclusive** of each operation-mode  
This reduces the required if-conditions to a minimum. Because the other "if-conditions" are replaced by the switch-case-break-statement.

You can easily add as many operation-modes as you want by simply adding additional "states" (like doing a sensing-survey while robot stand still etc.

You can do things in **parallel** to rotating the motors if you use step-signal creation in the backround like reading sensors, blinking LEDs etc.

here is a demo-code that shall explain how a state-machine works and how it is coded

> [@A Demo-Code explaining the switch-case state-machine and how to do things (almost) in parallel](https://forum.arduino.cc/t/a-demo-code-explaining-the-switch-case-state-machine-and-how-to-do-things-almost-in-parallel/888172):
>
> Hi everybody, if you look at the end of this first posting you see a list of links that show where this tutorial was used as a link. Go through this list to find additional information on how to apply this code This democode requires only the pure microcontroller without any hardware. You just open the serial monitor to watch the behaviour of the code. From time to time I enjoy writing demo-codes that want to explain programming-functionalities. There are a lot of different approachs to expl…

If you have any questions just post the questions

best regards Stefan

---

_[View the full topic](https://forum.arduino.cc/t/autonomous-mobile-robot-with-obstacle-avoidance/985240)._
