Autonomous mobile robot with obstacle avoidance

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

If you have any questions just post the questions

best regards Stefan

1 Like