Line Follower

Hello, I was working on own line follower but when I turn on robot then he will go forward 1 second - BEEP - forward 1 sec - BEEP - forward 1 sec - BEEP … Where is problem in my code ? I want to learn it but I am trying to repair it 2 days :frowning: ( // comments are in English )

#include <QTRSensors.h>
#define led_buzzer 13
#define buton 7 // the pin number to which the start button is connected
#define iralici 2 // pin to connect the receiver
#define dip1 4 // mode switch 1 - we define the pin number to which the mode switches are connected
#define dip2 5 // mode switch 2 - we define the pin number to which the mode switches are connected
#define dip3 6  //mode switch 3 - we define the pin number to which the mode switches are connected
#define solmotor1 3 // left motor speed1 - the pins needed to control the left motor
#define solmotor2 11  // left motor speed2 - if you exit with one of these two pins analogwrite, the motor rotates in that direction. If the 2 pin is 0 or 1 at the same time, the motor stops.
#define sagmotor1 9  // Right engine speed 1 - Right pins needed to control the engine
#define sagmotor2 10 //right-hand engine speed2 - if you exit with one of these two pins, the motor rotates in that direction. If the 2 pin is 0 or 1 at the same time, the motor stops.
int basla = 0; // Flags
int i = 0;
QTRSensorsAnalog qtra((unsigned char[]) {A3, A2 , A1, A0}, 4, 4); //line sensors to be connected to these pins
unsigned int sensorValues[4]; // We are specifying that we have a number of pieces

int tabanhiz = 255; // the speed at which the robot is exactly centered. (up to 255)
int maxhiz = 255; // maximum speed to be applied to engines.
float Kp = 0.18;
float Kd = 3;
int sonhata = 0;
int hata = 0;
int sagmotorpwm = 0;
int solmotorpwm = 0;
int zemin = 1;// For the white line on the top of the black runway must be 1. Make 0 for black line on white runway.


void setup() {
  pinMode(buton, INPUT_PULLUP);
  pinMode(iralici, INPUT_PULLUP);
  pinMode(dip1, INPUT_PULLUP);
  pinMode(dip2, INPUT_PULLUP);
  pinMode(dip3, INPUT_PULLUP);
  pinMode(led_buzzer, OUTPUT);
  pinMode(solmotor1, OUTPUT);
  pinMode(solmotor2, OUTPUT);
  pinMode(sagmotor1, OUTPUT);
  pinMode(sagmotor2, OUTPUT);
  tone(led_buzzer, 500, 100);
  delay(50);
  noTone(led_buzzer);

  for (i = 0; i < 80; i++)
  {
    if (0 <= i && i < 5)  motorkontrol(-150, 150);
    if (5 <= i && i  < 15) motorkontrol(150, -150);
    if (15 <= i && i < 25)  motorkontrol(-150, 150);
    if (25 <= i && i < 35)  motorkontrol(150, -150);
    if (35 <= i && i < 45)  motorkontrol(-150, 150);
    if (45 <= i && i  < 55) motorkontrol(150, -150);
    if (55 <= i && i < 65)  motorkontrol(-150, 150);
    if (65 <= i && i < 70)  motorkontrol(150, -150);
    if ( i >= 70  )  {
      motorkontrol(0, 0);
      delay(5);
    }
    qtra.calibrate();
    delay(3);
  }

  motorkontrol(0, 0);
  tone(led_buzzer, 3000, 100);
  delay(50);
  noTone(led_buzzer);
}


void loop() {

  if (digitalRead(buton) == 0) {
    for (int a = 0; a < 5; a++) {
      tone(led_buzzer, 500, 100); delay(500); noTone(led_buzzer); delay(400); basla = 1;
    }
  }

  if (basla == 1) { // If the start variable is 1, the main program will be written here.
    hata = ( qtra.readLine(sensorValues, 1, zemin) - 1500);
    int duzeltmehizi = Kp * hata + Kd * (hata - sonhata);
    sonhata = hata;
    solmotorpwm = tabanhiz + duzeltmehizi  ;
    sagmotorpwm = tabanhiz - duzeltmehizi  ;
    solmotorpwm = constrain(solmotorpwm, -255, maxhiz); ///// In this case, the PWM values ​​to be applied to the motors are limited.
    sagmotorpwm = constrain(sagmotorpwm, -255, maxhiz);
    motorkontrol(sagmotorpwm, solmotorpwm);
  }
}


void motorkontrol(int sagmotorpwm, int solmotorpwm) {
  if (sagmotorpwm <= 0) {
    sagmotorpwm = abs(sagmotorpwm);
    analogWrite(sagmotor1, 0);
    analogWrite(sagmotor2, sagmotorpwm);
  }
  else {
    analogWrite(sagmotor1, sagmotorpwm);
    analogWrite(sagmotor2, 0);
  }

  if (solmotorpwm <= 0) {
    solmotorpwm = abs(solmotorpwm);
    analogWrite(solmotor1, solmotorpwm);
    analogWrite(solmotor2, 0);
  }
  else {
    analogWrite(solmotor1, 0);
    analogWrite(solmotor2, solmotorpwm);
  }

}

Video with robot : https://youtu.be/YRA_A3mweP0

Line_Follower.ino (3.93 KB)

Help us out. When you describe a problem with your sketch, do this:
State what you expected the sketch to do.
State what it actually did.
Explain how those 2 differ (unless it is obvious)

Did you not expect the beep? It looks to me like that is the first thing you do in the loop.
Read a button. If it is zero, spend about 5 seconds doing nothing but beeping 5 times. Oh, and set basla to 1.

I would like to start robot and he will follow black line on the floor, without pauses, without beeps .. Just follow the line, if he lost line on left side, right motor will stop, left motor will continue to line again. If he is on the black line he will continue..

And how does the code you have behave?

Yes, as AWOL points out, you did not follow the steps I listed.
You did state your expectation.
You did not state the actual results and how those 2 differ.

One thing that I noticed - nothing in the description of your expectations mentions reading a button and beeping if the button read returns zero.
If you do not expect that, why do you have code in there to do that?

It's because he copy pasted it from somewhere and now he wants us to adapt it to his needs for him.

what is the type of ur sensor that u r using?
better if u put sensor type and schematic or how do u connect it to give us a full imagination of wt u really have