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;
}