Obstacle avoiding robot doesn't detect black dots

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 (by escaping walls), 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,go backward a little bit, turn right (there is no problem at that part) but this code does not do what I want. The part I complain about in this code is that The infrared sensor does not detecting black dots and does not perform the function we want when detect black dots. its just keep going like nothing there 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:

const int SagMotorileri = 9;
const int SagMotorgeri = 10;
const int MotorEnableSag = 6;

const int SolMotorileri = 2;
const int SolMotorgeri = 3;
const int MotorEnableSol = 5;

const int infraredSensorPin1 = 4;
const int infraredSensorPin2 = 12;
const int infraredSensorPin3 = 13;
const int triggerPin = 7;
const int echoPin = 8;
const int buzzerPin = 11;



int DuvarMesafe = 0;

void setup() {
  pinMode(SagMotorileri, OUTPUT);
  pinMode(SagMotorgeri, OUTPUT);
  pinMode(MotorEnableSag, OUTPUT);

  pinMode(SolMotorileri, OUTPUT);
  pinMode(SolMotorgeri, OUTPUT);
  pinMode(MotorEnableSol, OUTPUT);

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

void loop() {
  DuvarMesafe = MesafeOlc();
  
  if (IRAlgila()) {
    buzz(); // Siyah nokta algılandığında sadece buzzer çalacak
  } else {
    ileriGit();
  }
  if(DuvarMesafe < 10){
    SagaDon();
    }
}

bool IRAlgila() {
  return digitalRead(infraredSensorPin1) == LOW || 
         digitalRead(infraredSensorPin2) == LOW ||
         digitalRead(infraredSensorPin3) == LOW;
}

void ileriGit() {
  digitalWrite(SagMotorileri, HIGH);
  digitalWrite(SagMotorgeri, LOW);
  analogWrite(MotorEnableSag, 130);

  digitalWrite(SolMotorileri, HIGH);
  digitalWrite(SolMotorgeri, LOW);
  analogWrite(MotorEnableSol, 115);

  while (DuvarMesafe >= 10 ) {
    delay(50);
    DuvarMesafe = MesafeOlc();
  }
}

void SagaDon() {
   digitalWrite(SagMotorileri, LOW);
  digitalWrite(SagMotorgeri, HIGH);
  analogWrite(MotorEnableSag, 130);

  digitalWrite(SolMotorileri, LOW);
  digitalWrite(SolMotorgeri, HIGH);
  analogWrite(MotorEnableSol, 115);
  delay(500);
  MotorlariDurdur();
  delay(200);
  
  digitalWrite(SagMotorileri, LOW);
  digitalWrite(SagMotorgeri, HIGH);
  analogWrite(MotorEnableSag, 130);

  digitalWrite(SolMotorileri, HIGH);
  digitalWrite(SolMotorgeri, LOW);
  analogWrite(MotorEnableSol, 115);
  delay(800);
  MotorlariDurdur();
  delay(200);
  
}


void MotorlariDurdur() {
  digitalWrite(SagMotorileri, LOW);
  digitalWrite(SagMotorgeri, LOW);
  digitalWrite(SolMotorileri, LOW);
  digitalWrite(SolMotorgeri, LOW);
}



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

}

int MesafeOlc() {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH) * 0.0343 / 2;
}

I suggest to write a very simple program that just reads the sensor and reports the result on the serial monitor. Test the sensor function to see if it is working correctly.

When it is working correctly, place to robot on the intended surface, move it around by hand, and tune the program to detect black dots.

as ı said i m begginer i dont know how to do that can you give me an example

Your posted program has code in it to read the sensors. Make a new program, removing from the original all code that is not required to read the sensor, and test it.

That way you will start to learn something about programming Arduinos, which is a valuable skill!

does this work? if it is, my sensor is giving me just random numbers

const int infraredPin = A0; // Analog pin to which the sensor is connected

void setup() {
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  int sensorValue = analogRead(infraredPin); // Read the sensor value
  Serial.print("Infrared Sensor Value: ");
  Serial.println(sensorValue); // Print the sensor value to the serial monitor
  delay(1000); // Delay for 1 second before the next reading
}

Did you forget to study the posted code?

It defines these pins as as sensor pins, and reads them using digitalRead().

const int infraredSensorPin1 = 4;
const int infraredSensorPin2 = 12;
const int infraredSensorPin3 = 13;

It would be a good idea to check if those pins are actually connected to sensors.

Then how will you write this big complex thing if you can't even write a code just to read the sensor?

i also tried this but nothing changed. it still cant understand black dots


int infraredSensorPin1 = 4;
int infraredSensorPin2 = 12;
int infraredSensorPin3 = 13;

const int buzzerPin = 11;

void setup() {

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

void loop() {

  
  if (IRAlgila()) {
    buzz() 
  } 

    
}

bool IRAlgila() {
  return digitalRead(infraredSensorPin1) == LOW || 
         digitalRead(infraredSensorPin2) == LOW ||
         digitalRead(infraredSensorPin3) == LOW;
}

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

}

What does it see instead? Use Serial.print() to see the sensor readings. For example

Serial.print("Sensor1 = "); 
Serial.println(digitalRead(infraredSensorPin1));

You will need to add Serial.begin() to setup function, with an appropriate serial Baud rate.

Basics first!

Talk about your sensor? Does it read LoW or HIGH over a black dot? What does it read over white?

it read every where white! it just keep going

That wasn't the question. The question is, when the sensor is over black should it read LOW or HIGH?

How do you know that?

What does Serial.print() report, for each of the three individual sensors?

In your first piece of code you use digitalRead() to read your IR sensor. In your second piece of code you use analogRead(). Why is that?

If you can't tell the answer: you really need to first learn the basics, and get some understanding on how an Arduino reads inputs, and what it means. Start maybe by asking whoever gave you the robot and code for a more detailed explanation.

The project you have sounds like a great learning project, it has a lot in it, far more than you appear to understand. You have to understand basics like pin numbers, analog vs. digital inputs, the meaning of HIGH and LOW, etc. Otherwise you're just poking around in the dark randomly and all that happens is either you get frustrated because nothing seems to work, or you destroy the board by making a mistake in your wiring and then you get frustrated about that. Neither is useful.

Maybe start out clean on a simpler, less complex version of your project first, and then add on to it so you don't have a huge burden all at once.

Hello @clkerdmmmm - Do you have problems or questions about your robot and the black dot sensor (or code, DC motors, L298N, HC-SR04, buzzer or Uno)?

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