Arduino Engelden Kaçan Robot Versiyon 2

Bir önceki çalışmanın linki:
https://mucitpilot.blogspot.com/2020/07/engelden-kacan-robot-yapmnda-hc-sr04.html
Proje Videosu:
Arduino Kodu:
//Mucit Pilot 2020 Engelden Kaçan Robot Projesi v2
//motor pinlerini tanımladık
#define MotorL1 6
#define MotorL2 9
#define MotorLE 5
#define MotorR1 2
#define MotorR2 4
#define MotorRE 3
//ultrasonic sensör pinleri
#define trigPinR 7
#define echoPinR 8
#define trigPinL 12
#define echoPinL 13
//değişkenler
int mesafe = 100;
int solspeed=0;
int sagspeed=0;
long gecenSure;
int motoryon;
int motorhiz;
int motordelay;
int solyon, soldelay, sagyon, sagdelay;
void setup() {
pinMode(MotorR1, OUTPUT);
pinMode(MotorR2, OUTPUT);
pinMode(MotorL1, OUTPUT);
pinMode(MotorL2, OUTPUT);
pinMode(trigPinR, OUTPUT);
pinMode(echoPinR, INPUT);
pinMode(trigPinL, OUTPUT);
pinMode(echoPinL, INPUT);
Serial.begin(9600);
delay(1000);
}
void loop() {
digitalWrite(trigPinL,LOW);
delayMicroseconds(2);
digitalWrite(trigPinL,HIGH);
delayMicroseconds(10);
digitalWrite(trigPinL,LOW);
gecenSure= pulseIn(echoPinL, HIGH);
int mesafeL= (gecenSure/2) / 29.1;
mesafeL=constrain(mesafeL,1,100);
digitalWrite(trigPinR,LOW);
delayMicroseconds(2);
digitalWrite(trigPinR,HIGH);
delayMicroseconds(10);
digitalWrite(trigPinR,LOW);
gecenSure= pulseIn(echoPinR, HIGH);
int mesafeR= (gecenSure/2) / 29.1;
mesafeR=constrain(mesafeR,1,100);
Serial.print(mesafeR);
Serial.print(",");
Serial.println(mesafeL);
solspeed = motorhizi(mesafeR);
solyon = motoryon;
soldelay= motordelay;
sagspeed = motorhizi(mesafeL);
sagyon = motoryon;
sagdelay= motordelay;
// Serial.print(solspeed);
// Serial.print(",");
// Serial.print(solyon);
// Serial.print(",");
// Serial.println(soldelay);
if((solyon==0)&&(sagyon==0)) //her iki motor da yakınlık nedeni ile durmuş ise önce geri git sonra açıklığa göre dön
{
geri();
delay(400);
if(mesafeL >= mesafeR){
solaDon();
delay(900);
Serial.println("soladön çalıştı");
}
else {
sagaDon();
delay(900);
Serial.println("sagadön çalıştı");
}
}
else //yukarıdaki durumun haricindeki tüm durumlarda ilerle
{
motorcalistir(solspeed, sagspeed, solyon, sagyon, soldelay, sagdelay);
}
}
int motorhizi(int distance)
{
if(distance >=63)
{
motoryon = 1;
motorhiz = map(distance,63,100,220,255);
motordelay = 10;
}
else if(distance >=53)
{
motoryon = 1;
motorhiz = map(distance,53,62,180,219);
motordelay = 20;
}
else if(distance >=43)
{
motoryon = 1;
motorhiz = map(distance,43,52,130,179);
motordelay = 30;
}
else if(distance >=33)
{
motoryon = 1;
motorhiz = map(distance,33,42,90,129);
motordelay = 40;
}
else if(distance >=23)
{
motoryon = 1;
motorhiz = map(distance,23,32,60,89);
motordelay =50;
}
else if(distance >=13)
{
motoryon = 1;
motorhiz = map(distance,13,22,40,60);
motordelay = 60;
}
else if(distance >=6)//8
{
motoryon = 0;
motorhiz = 40;
motordelay = 50;
}
else if(distance >=1)
{
motoryon = 2;
motorhiz = map(distance,2,5,40,60);
motordelay = 40;
}
else
{
motoryon = 0;
motorhiz = 200;
motordelay = 50;
}
return motorhiz;
}
void motorcalistir(int solhiz, int saghiz, int solyon, int sagyon, int solbekleme, int sagbekleme) {
int d = max(solbekleme,sagbekleme);
if (solyon==1){
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, solhiz);
delay(d);
}
else if (solyon==0){
digitalWrite(MotorL1, LOW);
digitalWrite(MotorL2, LOW);
delay(d);
}
else {
digitalWrite(MotorL1, LOW);
digitalWrite(MotorL2, HIGH);
analogWrite(MotorLE, solhiz);
delay(d);
}
if (sagyon==1){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, saghiz);
delay(d);
}
else if (sagyon==0){
digitalWrite(MotorR1, LOW);
digitalWrite(MotorR2, LOW);
delay(d);
}
else {
digitalWrite(MotorR1, LOW);
digitalWrite(MotorR2, HIGH);
analogWrite(MotorRE, saghiz);
delay(d);
}
}
void geri() {
digitalWrite(MotorR1, LOW);
digitalWrite(MotorR2, HIGH);
analogWrite(MotorRE, 100);
digitalWrite(MotorL1, LOW);
digitalWrite(MotorL2, HIGH);
analogWrite(MotorLE,100);
}
void sagaDon() {
digitalWrite(MotorR1, LOW);
digitalWrite(MotorR2, HIGH);
analogWrite(MotorRE, 150);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, 150);
}
void solaDon() {
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, 150);
digitalWrite(MotorL1, LOW);
digitalWrite(MotorL2, HIGH);
analogWrite(MotorLE, 150);
}
Yorumlar
Yorum Gönder