ARABA YAPIMINDA KULLANILACAK MALZEMELER
Gerekli malzemeler:
- Arduino UNO
- Çok Amaçlı Robot Platformu
- L298N Voltaj Regülatörlü Çift Motor Sürücü Kartı
- HC06 Bluetooth Modülü
- Pil
- Jumper Kablo
ENGELDEN KAÇAN ARABANIN KODLARI
#define echoPin 12
#define trigPin 13
#define MotorR1 7
#define MotorR2 6
#define MotorRE 9
#define MotorL1 5
#define MotorL2 4
#define MotorLE 3
long sure, uzaklik;
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(MotorL1, OUTPUT);
pinMode(MotorL2, OUTPUT);
pinMode(MotorLE, OUTPUT);
pinMode(MotorR1, OUTPUT);
pinMode(MotorR2, OUTPUT);
pinMode(MotorRE, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
sure = pulseIn(echoPin, HIGH);
uzaklik = sure / 29.1 / 2;
Serial.println(uzaklik);
if (uzaklik < 15)
{
geri();
delay(150);
sag();
delay(250);
}
else {
ileri();
}
}
void ileri(){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, 150);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, 150);
}
void sag(){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, 0);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, 150);
}
void geri(){
digitalWrite(MotorR1, LOW);
digitalWrite(MotorR2, HIGH);
analogWrite(MotorRE, 150);
digitalWrite(MotorL1, LOW);
digitalWrite(MotorL2, HIGH);
analogWrite(MotorLE, 150);
}
BLUETOOTH İLE KONTROL EDİLEN ARABANIN KODLARI
const int motorA1 = 5;
const int motorA2 = 6;
const int motorB1 = 10;
const int motorB2 = 9;
int i=0; int j=0;
int state;
int vSpeed=255;
void setup() {
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
Serial.begin(9600);}
void loop() {
if(digitalRead(BTState)==LOW) { state='S'; }
if(Serial.available() > 0){
state = Serial.read(); }
if (state == '0'){
vSpeed=0;}
else if (state == '1'){
vSpeed=100;}
else if (state == '2'){
vSpeed=180;}
else if (state == '3'){
vSpeed=200;}
else if (state == '4'){
vSpeed=255;}
if (state == 'F') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, vSpeed);
analogWrite(motorB2, 0); }
else if (state == 'G') {
analogWrite(motorA1,vSpeed );
analogWrite(motorA2, 0);
analogWrite(motorB1, 100);
analogWrite(motorB2, 0); }
else if (state == 'I') {
analogWrite(motorA1, 100);
analogWrite(motorA2, 0);
analogWrite(motorB1, vSpeed);
analogWrite(motorB2, 0); }
else if (state == 'B') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0);
analogWrite(motorB2, vSpeed); }
else if (state == 'H') {
analogWrite(motorA1, 0);
analogWrite(motorB2, vSpeed); }
else if (state == 'H') {
analogWrite(motorA1, 0);
analogWrite(motorA2, 100);
analogWrite(motorB1, 0);
analogWrite(motorB2, vSpeed); }
else if (state == 'J') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0);
analogWrite(motorB2, 100); }
else if (state == 'L') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 150);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0); }
else if (state == 'R') {
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, vSpeed);
analogWrite(motorB2, 150); }
else if (state == 'S'){
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0); } }
ÇİZGİ İZLEYEN ROBOT KODLARI
#define SensorSol 10
#define SensorOrta 11
#define SensorSag 12
void setup() {
pinMode(SensorSol, INPUT);
pinMode(SensorOrta, INPUT);
pinMode(SensorSag, INPUT);
Serial.begin(9600);
Serial.println("Cizgi Sensoru Testi");
delay(2000);
}
void loop() {
Serial.print(digitalRead(SensorSol));
Serial.print(",");
Serial.print(digitalRead(SensorOrta));
Serial.print(",");
Serial.println(digitalRead(SensorOrta));
delay(100);
}
Sensörlerimizin siyah görüğünde “1” çıkış değerini aldığında oluşan mantık tablodaki gibidir.
Bu mantığa göre programlamaya başlıyoruz.
#define SensorSol 10
#define SensorOrta 11
#define SensorSag 12
#define MotorR1 6
#define MotorR2 7
#define MotorRE 9
#define MotorL1 5
#define MotorL2 4
#define MotorLE 3
void setup() {
pinMode(SensorSol, INPUT);
pinMode(SensorOrta, INPUT);
pinMode(SensorSag, INPUT);
pinMode(MotorR1, OUTPUT);
pinMode(MotorR2, OUTPUT);
pinMode(MotorL1, OUTPUT);
pinMode(MotorL2, OUTPUT);
}
void loop() {
if(digitalRead(SensorSol) == 0 && digitalRead(SensorOrta) == 1 && digitalRead(SensorSag) == 0){
ileri();
}
if(digitalRead(SensorSol) == 0 && digitalRead(SensorOrta) == 0 && digitalRead(SensorSag) == 1){
sag();
}
if(digitalRead(SensorSol) == 1 && digitalRead(SensorOrta) == 0 && digitalRead(SensorSag) == 0){
sol();
}
}
void ileri(){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, 150);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, 150);
}
void sag(){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, 0);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, 150);
}
void sol(){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
analogWrite(MotorRE, 150);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
analogWrite(MotorLE, 0);
}
void dur(){
digitalWrite(MotorR1, HIGH);
digitalWrite(MotorR2, LOW);
digitalWrite(MotorRE, LOW);
digitalWrite(MotorL1, HIGH);
digitalWrite(MotorL2, LOW);
digitalWrite(MotorLE, LOW);
}
Published: Apr 4, 2020
Latest Revision: Apr 4, 2020
Ourboox Unique Identifier: OB-765068
Copyright © 2020