How can i make my robot check sensors while mooving fluently

Hello everyone,

firstly, i m beginner. Because of that i didn't understand exactly where the problem is, but i think its somewhere in my code.The project I want to do is as follows:
A robot will be created, which will systematically navigate a white square area (1.5m x 1.5m) with 3 cm long black dots on it. While the robot is walking around (zigzagging like a vacuum cleaner), if it detects a black dot on the ground, it will wait without moving for 1 second, make a sound from the buzzer for 2 seconds and continue searching for the dots (3 seconds in total). I thought about the movement algorithm of this robot as follows:
Start moving forward, if you see a black dot, wait for 1 second, ring the buzzer for 2 seconds and continue on your way. If the distance between you and the wall is less than 5 cm, then check how many times you encounter the wall; If this number is even, turn left where you are, go forward 6-7 cm, turn left where you are again. But if the number of encounters with the wall is odd, then turn right where you are, move forward 6-7 cm, turn right where you are again (this will enable the scanning robot to scan the entire area systematically) but this code does not do what you want. The part I complain about in this code is that it progresses haltingly/almost never moves forward, there seems to be no problem in parts such as the wall encountering counter or turning parts (but I'm not sure, it may couse this problem too, as I said, I'm a beginer) What I want is for this robot to search for the points while moving forvard smoothly/constantly . I'd be happy if anyone helps.

the things i used:
2 dc motor
1 l298 motor driver
1 ultrasonic sensor (hc-SR04)
3 infared sensor and sensors driver (F233-01, generally used in line-following robots)
1 buzzer
and arduino uno

The code:

int WallDistance = 0;

const int MotorRight1 = 9;
const int MotorRight2 = 10;
const int MotorEnableRight = 4;  

const int MotorLeft1 = 2;
const int MotorLeft2 = 3;
const int MotorEnableLeft = 5;  

const int infraredSensorPin1 = A0;  
const int infraredSensorPin2 = A1;  
const int infraredSensorPin3 = A2;  
const int triggerPin = 7;           
const int echoPin = 8;              
const int buzzerPin = 6;

int EncounterWall = 0;  // Duvar ile karşılaşma sayısı

void setup() {
  
  pinMode(MotorRight1, OUTPUT);
  pinMode(MotorRight2, OUTPUT);
  pinMode(MotorEnableRight, OUTPUT);

  pinMode(MotorLeft1, OUTPUT);
  pinMode(MotorLeft2, OUTPUT);
  pinMode(MotorEnableLeft, OUTPUT);

  pinMode(infraredSensorPin1, INPUT);
  pinMode(infraredSensorPin2, INPUT);
  pinMode(infraredSensorPin3, INPUT);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {

  GoForward();
  WallDistance = MeasureDistance();

  if (WallDistance < 5){
    EncounterWall++;
    if(EncounterWall %2 == 1){
      TurnRight();
      
    }
    else {
      TurnLeft();
    }
  }

  else if (DetectBlackDot()){
    StopMotors();
    delay(1000);
    buzz();
  }
}

void GoForward() {
  // Motorlar ileri
  digitalWrite(MotorRight1, HIGH);
  digitalWrite(MotorRight2, LOW);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(MotorLeft2, LOW);
  analogWrite(MotorEnableLeft, 255);
  // here I'm trying to get it to do distance control and point detection while moving forward
  while (WallDistance >= 5 && !DetectBlackDot()) {
    delay(100); // Mesafe ve nokta algılama kontrolünü sık aralıklarla yapmak için
    WallDistance = MeasureDistance(); // Mesafe değerini güncelle
  }
}

void TurnRight() {
// turn right
  StopMotors();
  delay(200);
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, HIGH);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(MotorLeft2, LOW);
  analogWrite(MotorEnableLeft, 255);

  //Delay to rotate approximately 90 degrees (robot will rotate in place)
  delay(1000);
  StopMotors();
  delay(200);

  digitalWrite(MotorRight1, HIGH);
  digitalWrite(MotorRight2, HIGH);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(MotorLeft2, HIGH);
  analogWrite(MotorEnableLeft, 255);
  delay(700);

  StopMotors();
  delay(200);
    // turn right
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, HIGH);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(MotorLeft2, LOW);
  analogWrite(MotorEnableLeft, 255);

  //Delay to rotate approximately 90 degrees (robot will rotate in place)
  delay(1000);
  StopMotors();
  delay(200);
}

void TurnLeft() {
  // turn left
  StopMotors();
  delay(200);
  digitalWrite(MotorRight1, HIGH);
  digitalWrite(MotorRight2, LOW);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, HIGH);
  analogWrite(MotorEnableLeft, 255);

  //Delay to rotate approximately 90 degrees (robot will rotate in place)
  delay(1000);
  StopMotors();
  delay(200);

  digitalWrite(MotorRight1, HIGH); // biraz ,leri gidecek
  digitalWrite(MotorRight2, HIGH);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, HIGH);
  digitalWrite(MotorLeft2, HIGH);
  analogWrite(MotorEnableLeft, 255);
  delay(700);

  StopMotors();
  delay(200);
    // turn left
  digitalWrite(MotorRight1, HIGH);
  digitalWrite(MotorRight2, LOW);
  analogWrite(MotorEnableRight, 255);

  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, HIGH);
  analogWrite(MotorEnableLeft, 255);
  //Delay to rotate approximately 90 degrees (robot will rotate in place)
  delay(1000);
  StopMotors();
  delay(200);
}

void StopMotors() {
  digitalWrite(MotorRight1, LOW);
  digitalWrite(MotorRight2, LOW);
  digitalWrite(MotorLeft1, LOW);
  digitalWrite(MotorLeft2, LOW);
}

bool DetectBlackDot() {
  int infraredSensorValue1 = analogRead(infraredSensorPin1);// Blackdot control with infrared sensors
  int infraredSensorValue2 = analogRead(infraredSensorPin2);
  int infraredSensorValue3 = analogRead(infraredSensorPin3);
  int infraredThresholdValue = 500; // Set threshold value based on sensor behavior
  return (infraredSensorValue1 < infraredThresholdValue) || //Return true if at least one sensor detects a black dot
         (infraredSensorValue2 < infraredThresholdValue) || 
         (infraredSensorValue3 < infraredThresholdValue);
}

void buzz() {
  digitalWrite(buzzerPin, HIGH);
  delay(2000);
  digitalWrite(buzzerPin, LOW);
}

int MeasureDistance() { // measure distance with HC-SR04 
  digitalWrite(triggerPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  return pulseIn(echoPin, HIGH) * 0.0343 / 2;
}

  1. Get rid of ALL the delay() calls. Replace with ‘global’ millis timing.
  2. Create a state machine that checks & updates every time around loop,

Multiple timed events... this image shows TEMP is being read every 5 seconds, while LDR is being read every 1 second.

This page explains it very well, and further explains in more links...

And... an event timing example...

thank you very much, i didnt know what they are, But I know now thanks to you
Now my code seems working

ı learned today so much thing tahnks to you, thank you very much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.