Line follower and obstacle avoider code issue

Hello community, my problem is this, although my previous project was simply a robot that was suppose to follow a black tap and it went very well, now that I decided to incorporate the obstacle avoider feature he does not follow the line neither dodge, after a period of calibration he simply spins around.

My components are: Arduino Uno, Motor Driver l293d, QTR-8RC sensor, 2x DC motored wheels,
HC-SR04 ultrasonic sensor. I have a extra servo motor and ultrasonic sensor if necessary.

My connections are: QTR sensor A0-A4, echo pin: 13, trigPin: 10

I have seen a lot of videos on youtube but all of them use IR sensors instead of QTR sensors. Because of this I decided to mix codes, the one that I already have plus the avoiding code. For this I created a funcion called lineFollower that most likely has something wrong.

The code:
gabrielcode.ino (4.3 KB)
Thank you, Gabriel Santos!

Hello, Gabriel.

In general, troubleshooting issues like what you’re describing can be difficult to do by just looking at code, so a video showing the problem (or any future changes) would probably be helpful. However, the first thing I would suggest is simplifying and trying to test one thing at a time. For example, if you add the a simple while loop to the start of your loop() function that just calls your line following function continuously, does it follow the line without problems?

void loop()
{
  while(1){
    linefollower();
  }
...

One thing in your code that stood out to me is that you are using pulseIn() to read your ultrasonic sensor. Please note that pulseIn() is blocking and has a default timeout of 1 second, so if your sensor does not return anything (i.e. nothing is in range), your robot would only be sampling the line sensors at most once per second. pulseIn() does have an option for specifying the timeout with a third argument, so you could try setting it to something much lower. Also, it looks like you have the NewPing.h library included, but you are not using it. I am not as familiar with that library, but it claims to fix that problem.

Brandon