Self Driving Rc Car - Steering Proplem

i’m trying to build a self driving car, like it’s described in the following instructables:

In my case the car is only steering away from the wall, if the distance to the wall is about 2cm or less.
If the distance is larger than that, the car is steering towards the wall, instead of driving straight.

I would be very thankful, if anybody could help me solve this proplem.
i’m sorry for my bad english, but i’m from germany ,still in school and working on improving it

Code is attached

AutoCar2.ino (13.2 KB)

Hi —

The example program you found includes functions to read the ultrasonic sensors, compare the readings and then control the motors based on these comparisons.

It should be noted that the code might be a bit more complicated than it needs to be and appears to have a bug in the readSidesAndFront() function where it attempts to perform a "triple check" of the left and right sensors that lead to a leftTurn() or rightTurn(). Instead, the code will end up doing a single check because it is re-checking the wrong sensor. I don't think this is causing your issue, but it could affect the sensitivity of the readings.

If you are seeing the motor driver not behave as expected, I would recommend:

  • Uncomment the Serial.print lines so that you can see what each of the 5 sensor readings report
  • Do your testing with the car in a large room or even outside (no obstacles around it), and confirm that the car drives forward (no turn). If it is turning, then determine which type of turn is happening (ie. LEFT TURN, LONG LEFT TURN, FRONT LEFT TURN, etc.).
  • It may be easiest to elevate the bottom of the car so that the wheels are off the ground, enabling you to move objects towards each sensor to mimic it approaching a wall. This way you can take your time to see how it behaves.
  • If the car was attempting to drive forward (correctly) in the empty room, then with the car elevated, try bringing an object towards the front to see what it does. Then continue on with the front left / right.
  • With the car elevated, see how much the sensor readings fluctuate when no objects are moving around it. What range in values are you seeing? These ranges will help guide you to know what values to change in the Threshold settings. You may need to increase these values and then gradually decrease them until it responds how you want.
  • Once you see a behavior that doesn't appear to make sense (eg. object in front right at distance X, steering towards right), note down which scenario it was and what sensor seemed to cause it. From this, it should be easier to narrow down the problem in the code.
  • Due to some complexities in the code, I would recommend focusing on the behavior of one sensor at a time (ensuring it works correctly) before having multiple objects / walls detected around the car.

The HC-SR04 sensor readings can often be a bit “noisy” and may require some filtering (eg. Median filter) in order to get a reliable distance measurement. It looks like the original code attempted to do some “quadruple confirming” of the measurements as a rudimentary filter. It is hard to say if your sensor readings are giving you values that match what the code expects to see with the default config. This will take some experimentation. Performing the check above (where you note down what ranges in values each sensor gives with a non-moving scene) will help determine this. If a particular sensor is giving large ranges in values despite no changes in the distance, then you may need to add in a sensor measurement "filtering" function.

Looks like this was also posted to the German forum (Autonom fahrendes Auto - Problem mit der Lenkung - #3 by fuchsbandworm - Deutsch - Arduino Forum) and received feedback there, so I'll step aside to avoid duplicating efforts. Good luck with the project.