Robot line follower and obstacle avoiding using 1 arduino uno, 1 L293D motor driver shield, 1 servo, 1 sensor ultrasonic, 2 sensor infrared, 2 button

hi guys . im looking for someone who can help with my project. sorry for bad english

#include <NewPing.h>
#include <Servo.h>
#include <AFMotor.h>

//hc-sr04 sensor
#define TRIGGER_PIN A2
#define ECHO_PIN A3
#define max_distance 50

//ir sensor
#define irLeft A0
#define irRight A1

//tombol
int buttonskip = A5;
int buttonstop = A4;

//motor
#define MAX_SPEED 200
#define MAX_SPEED_OFFSET 20

Servo servo;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, max_distance);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);


int distance = 0;
int leftDistance;
int rightDistance;
boolean object;

void setup() {
  Serial.begin(9600);
  pinMode(irLeft, INPUT);
  pinMode(irRight, INPUT);
  servo.attach(10);
  servo.write(90);

  motor1.setSpeed(100);
  motor2.setSpeed(100);
  motor3.setSpeed(100);
  motor4.setSpeed(100);
}

void loop() {
  if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 0 ) {
   objectAvoid();

  }
  else if (digitalRead(irLeft) == 0 && digitalRead(irRight) == 1 ) {
    objectAvoid();
    Serial.println("TL");
    //leftturn
    moveLeft();
  }
  else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 0 ) {
    objectAvoid();
    Serial.println("TR");
    //rightturn
    moveRight();
  }
  else if (digitalRead(irLeft) == 1 && digitalRead(irRight) == 1 ) {
    //Stop
    temporary(); 
  }
}

void temporary(){
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  
  
  delay(1500);
  if (analogRead(buttonskip) == 1023) {
  delay(0);
  objectAvoid(); 
  }
  else if (analogRead(buttonstop) == 1023) {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  delay(+6000);
  objectAvoid();
  }
  else {
  delay(+3000);
  objectAvoid();
  }
}


void objectAvoid() {
  distance = getDistance();
  if (distance <= 15) {
    //stop
    Stop();
    Serial.println("Stop");

    lookLeft();
    lookRight();
    delay(100);
    if (rightDistance <= leftDistance) {
      //left
      object = true;
      turn();
      Serial.println("moveLeft");
    } else {
      //right
      object = false;
      turn();
      Serial.println("moveRight");
    }
    delay(100);
  }
  else {
    //forword
    Serial.println("moveforword");
    moveForward();
  }
}

int getDistance() {
  delay(50);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 100;
  }
  return cm;
}

int lookLeft () {
  //lock left
  servo.write(150);
  delay(500);
  leftDistance = getDistance();
  delay(100);
  servo.write(90);
  Serial.print("Left:");
  Serial.print(leftDistance);
  return leftDistance;
  delay(100);
}

int lookRight() {
  //lock right
  servo.write(30);
  delay(500);
  rightDistance = getDistance();
  delay(100);
  servo.write(90);
  Serial.print("   ");
  Serial.print("Right:");
  Serial.println(rightDistance);
  return rightDistance;
  delay(100);
}


void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
void moveForward() {
  motor1.setSpeed(100);
  motor1.run(FORWARD);
  motor2.setSpeed(100);
  motor2.run(FORWARD);
  motor3.setSpeed(100);
  motor3.run(FORWARD);
  motor4.setSpeed(100);
  motor4.run(FORWARD);
}
void moveBackward() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);

}
void turn() {
  if (object == false) {
    Serial.println("turn Right");
    moveLeft();
    delay(700);
    moveForward();
    delay(1000);
    moveRight();
    delay(1200);
    if (digitalRead(irRight) == 1) {
      loop();
    } else {
      moveForward();
    }
  }
  else {
    Serial.println("turn left");
    moveRight();
    delay(700);
    moveForward();
    delay(1000);
    moveLeft();
    delay(1200);
    if (digitalRead(irLeft) == 1) {
      loop();
    } else {
      moveForward();
    }
  }
}
void moveRight() {
  motor1.setSpeed(140);
  motor1.run(BACKWARD);
  motor2.setSpeed(140);
  motor2.run(BACKWARD);
  motor3.setSpeed(140);
  motor3.run(FORWARD);
  motor4.setSpeed(140);
  motor4.run(FORWARD);
  
}
void moveLeft() {
  motor1.setSpeed(140);
  motor1.run(FORWARD);
  motor2.setSpeed(140);
  motor2.run(FORWARD);
  motor3.setSpeed(140);
  motor3.run(BACKWARD);
  motor4.setSpeed(140);
  motor4.run(BACKWARD);
}

whaat im looking is when i push button it get more delay and if skip button get push it instanly start running again....
again sorri with my bad english. hope somebody can help me, thanks for your time

I suspect your main problem is the use of delay() calls in your motion functions.
Those will ‘block’ your code - inhibiting the buttons from being detected.

Sadly, it means you’ll have to rethink the code to avoid using delay()
The typical approach uses millis() timing to trigger the individual motion steps.

Take a look at the revered ‘Blink without Delay’‘ example in the IDE examples.

1 Like

I suspect that too, but don't know where to put it the millis(). I got this code on YouTube. this is actually my first project. so i dont really get it how it work. im just adding 2 button

Since this is your first code I highly recommend you get a copy of the Arduino Cookbook, skim it them read sections that discuss the parts of your project. There are many tutorials online but in my opinion they are not as good as the cookbook. Before you get super deep into your project build some of the basic projects in the samples that came with the IDE, and then change them to do different things. That is how many of us learned.

the same text in Uzbek

Bu faqat google-tarjimon qanchalik yaxshi ishlashini ko'rsatish uchun o'zbek tilida yozish uchun istisno. Agar siz o'zbek tilida yozsangiz, men bu xabarni e'tiborsiz qoldiraman.Siz ingliz tilida gaplashadigan forumda yozyapsiz. Shunday qilib, quyida tavsiflanganidek, hamma narsani ingliz tiliga tarjima qiling

Ko'p ko'p so'zlarni tarjima qilish, jumladan, ba'zi g'alati tarjima qilingan so'zlarni ham to'g'ridan-to'g'ri ingliz tilida yozilgan bir nechta so'zlardan foydalanishga qaraganda loyihangizda tezroq davom etishni anglatadi.

Siz menga haqiqatan ham ishonishingiz mumkin. Dasturlashda narsalarni aniq so'zlar bilan tasvirlash juda muhim. Va etarli so'zlar bilan. Agar siz ushbu maslahatga amal qilsangiz, loyihangizni tezroq tugatasiz.

Loyihangizning juda batafsil tavsifini oʻzbek tilida yozing va keyin tarjimani Google tarjima qilishiga ruxsat bering.

same text in english

This is an exception to write in uzbek only to demonstrate how well google-translate works. If you post in uzbek I will ignore the uzbek posts. You are writing in the english speaking forum. So translate everything into english like described below

Translating a lot of words - including some strange translated words - still means faster proceeding in your project compared to using few words directly written in english.

You can really believe me. In programming it is very very crucial that you describe things with precise words. And with enough words. You will finish your project faster if you follow this advice.

Write a very detailed description of your project in Uzbek and then let do google-translate the translation.

best regards Stefan

1 Like

@anwarmochtar Welcome to the forum..
Bots are fun.. :slight_smile:

Worked on one not too long ago, fraught with delays..
Taking them out usually requires developing a state machine or two..

Take a gander here.. run two loops at the same time..

compare original code to the code with delays removed..

Same principal can be applied you your code base..

have fun.. ~q

1 Like

It’s going to me a learning curve.