Etrafındaki engellerin yönlerini ve mesafelerini tespit eden robota CEYD-A ilerlemesini söylediğinde robot, eğer önünde engel var ise duruyor. Sorulduğunda ise etrafındaki engellerin mesafelerini söylüyor.
Gereksinimler
Arduino Uno | |
---|---|
HC 05 Bluetooth Modülü | |
1K Ohm ve 2.2K Ohm Dirençler | |
Breadboard | |
Jumper Kablolar (Erkek-erkek ve dişi-erkek) | |
Arduino Robot Araba Kiti 2wd (2 tekerli + 1 sarhoş teker) | |
AA Piller 4’Lü Paket | |
SG-90 Servo Motor | |
HC-SR04 | |
CEYD-A uygulaması |
Ek bilgi: Arduino’yu beslemek için videoda Powerbank kullanılmıştır. Motor sürücü için ayrıca 6V (4x1.5V AA Pil) yeterlidir veya devre şemasındaki gibi 9V kare pil kullanılabilir.
Devre Şeması
Arduino IDE Kodu:
#include <Servo.h> //Servo motor library. This is standard library
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX, TX
char data;
const int LeftMotorForward = 5;
const int LeftMotorBackward = 4;
const int RightMotorForward = 6;
const int RightMotorBackward = 7;
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
boolean goesForward = false;
int distance = 100;
Servo servo_motor; //our servo name
void setup(){
Serial.begin(9600);
BTSerial.begin(9600);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
servo_motor.attach(11); //our servo pin
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
if (distance <= 10){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight(false);
delay(300);
distanceLeft = lookLeft(false);
delay(300);
if (distance >= distanceLeft){
turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
if (BTSerial.available())
{
data = BTSerial.read();
if (data == '2')
{
moveForward();
Serial.println("İlerliyorum");
}
if (data == '3')
{
turnRight();
Serial.println("Sağa döndüm");
}
if (data == '4')
{
turnLeft();
Serial.println("Sola döndüm");
}
if (data == '5')
{
moveStop();
Serial.println("Durdum");
}
if (data == '6')
{
lookLeft(true);
}
if (data == '7')
{
lookRight(true);
}
if (data == '8')
{
distanceRight = lookRight(false);
delay(300);
distanceLeft = lookLeft(false);
delay(300);
if((distance>distanceLeft)&&(distanceRight>distanceLeft))
{
Serial.print("Solumda.");
Serial.print(distanceLeft);
Serial.println("cm mesafede engel var");
}
if((distance>distanceRight)&&(distanceLeft>distanceRight))
{
Serial.print("Sağımda.");
Serial.print(distanceRight);
Serial.println("cm mesafede engel var");
}
if((distanceLeft>distance)&&(distanceRight>distance))
{
Serial.print("Önümde.");
Serial.print(distance);
Serial.println("cm mesafede engel var");
}
}
if (data == '9')
{
moveBackward();
Serial.println("Geri çekildim");
}
}
if (Serial.available())
{
BTSerial.write(Serial.read());
}
}
distance = readPing();
}
int lookRight(boolean write){
servo_motor.write(50);
delay(100);
int distance1 = readPing();
if(write)
Serial.println(distance1);
delay(200);
servo_motor.write(115);
return distance1;
}
int lookLeft(boolean write){
servo_motor.write(170);
delay(100);
int distance1 = readPing();
if(write)
Serial.println(distance1);
delay(200);
servo_motor.write(115);
return distance1;
}
int readPing()
{
long duration, distance1;
digitalWrite(trig_pin,LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
duration = pulseIn(echo_pin, HIGH);
distance1 = duration / 58.2;
delay(50);
if(distance1>400)
return 400;
return distance1;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
CEYD-A Kodları:
Bluetooth bağlan, Bluetooth tan bilgi al, Bluettooha 2 gönder gibi bluetooth komutlarını tüm CEYD-A’lar desteklemektedir. Bunlar için komut oluşturmak gerekmemektedir. Aşağıdaki komutlar sola bak, sağa bak, etrafında ne var gibi söylemler için oluşturulmuştur.
SORU: bak%,6|
SABLON: {HER}(sol|sağ)a bak{HER}$
CEVAP:
{!IF $1==sol??{!SET ignored={!RET bluetootha 6 gönder!}!}::!}
{!IF $1==sağ??{!SET ignored={!RET bluetootha 7 gönder!}!}::!}
{!RET cmdmessagebox $1 tarafa bakıyorum!}
{!SET response={!RET bluetooth bilgi al!}!}
{!SET value={!RET cmdfetchnumeric {!GET response!}!}!} Engel {!GET value!} cm ötede.
SORU: etrafında,6|
SABLON: {HER}(ne var|engel var mı){HER}$
CEVAP: {!SET ignored={!RET bluetootha 8 gönder!}!}
{!RET cmdmessagebox Şimdi sağımı, önümü ve solumu inceliyorum.!}
{!SET response={!RET bluetooth bilgi al!}!}
{!GET response!}