Membuat Robot Line Follower / Robot Line Tracer
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah robot line follower dengan menggunakan arduino nano sebagai kendalinya. jadi robot ini hanya bergerak sesuai dengan jalur yang sudah diberikan dengan jalur bewarna hitam putih. untuk sensor yang dipakai yaitu 8 buah photodioda dengan 8 buah led putih super bright. untuk lebih jelasnya berikut adalah koding dan skemanya.
a. Komponen dan skemanya
b. Program Arduino IDE
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int mtrkanan1 = 8;
int mtrkanan2 = 11;
int mtrkiri1 = 12;
int mtrkiri2 = 13;
int kecmtr1 = 9;
int kecmtr2 = 10;
int kecepatan = 255;
float batas1;
float batas2;
float batas3;
float batas4;
float batas5;
float batas6;
float batas7;
float batas8;
int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;
int addr5 = 4;
int addr6 = 5;
int addr7 = 6;
int addr8 = 7;
float simpan1;
float simpan2;
float simpan3;
float simpan4;
float simpan5;
float simpan6;
float simpan7;
float simpan8;
int sensor1;
int sensor2;
int sensor3;
int sensor4;
int sensor5;
int sensor6;
int sensor7;
int sensor8;
int a,b,c,d,e,f,g,h;
int btset = 0;
int btsetx;
int btok = 1;
int btokx;
float batas1a, batas1b;
float batas2a, batas2b;
float batas3a, batas3b;
float batas4a, batas4b;
float batas5a, batas5b;
float batas6a, batas6b;
float batas7a, batas7b;
float batas8a, batas8b;
void setup() {
batas1 = EEPROM.read(addr1);
batas2 = EEPROM.read(addr2);
batas3 = EEPROM.read(addr3);
batas4 = EEPROM.read(addr4);
batas5 = EEPROM.read(addr5);
batas6 = EEPROM.read(addr6);
batas7 = EEPROM.read(addr7);
batas8 = EEPROM.read(addr8);
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
pinMode(mtrkanan1,OUTPUT);
pinMode(mtrkanan2,OUTPUT);
pinMode(mtrkiri1,OUTPUT);
pinMode(mtrkiri2,OUTPUT);
pinMode(kecmtr1,OUTPUT);
pinMode(kecmtr2,OUTPUT);
pinMode(btset,INPUT);
pinMode(btok,INPUT);
}
void loop() {
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
if(btokx == 0){
lcd.clear();
delay(1000);
mulai();
}
sensor1 = analogRead(A0);
sensor2 = analogRead(A1);
sensor3 = analogRead(A2);
sensor4 = analogRead(A3);
sensor5 = analogRead(A4);
sensor6 = analogRead(A5);
sensor7 = analogRead(A6);
sensor8 = analogRead(A7);
if(btsetx == 0){
delay(2000);
lcd.clear();
kalsenput1();
kalsenhit1();
kalsenput2();
kalsenhit2();
kalsenput3();
kalsenhit3();
kalsenput4();
kalsenhit4();
kalsenput5();
kalsenhit5();
kalsenput6();
kalsenhit6();
kalsenput7();
kalsenhit7();
kalsenput8();
kalsenhit8();
}
if(sensor1 > batas1){
a = 1;
}
if(sensor1 < batas1){
a = 0;
}
if(sensor2 > batas2){
b = 1;
}
if(sensor2 < batas2){
b = 0;
}
if(sensor3 > batas3){
c = 1;
}
if(sensor3 < batas3){
c = 0;
}
if(sensor4 > batas4){
d = 1;
}
if(sensor4 < batas4){
d = 0;
}
if(sensor5 > batas5){
e = 1;
}
if(sensor5 < batas5){
e = 0;
}
if(sensor6 > batas6){
f = 1;
}
if(sensor6 < batas6){
f = 0;
}
if(sensor7 > batas7){
g = 1;
}
if(sensor7 < batas7){
g = 0;
}
if(sensor8 > batas8){
h = 1;
}
if(sensor8 < batas8){
h = 0;
}
lcd.setCursor(3, 0);
lcd.print(a);lcd.print(b);lcd.print(c);lcd.print(d);lcd.print(e);lcd.print(f);lcd.print(g);lcd.print(h);
lcd.setCursor(0, 1);
lcd.print("KAL");
lcd.setCursor(12, 1);
lcd.print("START");
delay(10);
}
void mulai(){
sensor1 = analogRead(A0);
sensor2 = analogRead(A1);
sensor3 = analogRead(A2);
sensor4 = analogRead(A3);
sensor5 = analogRead(A4);
sensor6 = analogRead(A5);
sensor7 = analogRead(A6);
sensor8 = analogRead(A7);
if(sensor1 > batas1){
a = 1;
}
if(sensor1 < batas1){
a = 0;
}
if(sensor2 > batas2){
b = 1;
}
if(sensor2 < batas2){
b = 0;
}
if(sensor3 > batas3){
c = 1;
}
if(sensor3 < batas3){
c = 0;
}
if(sensor4 > batas4){
d = 1;
}
if(sensor4 < batas4){
d = 0;
}
if(sensor5 > batas5){
e = 1;
}
if(sensor5 < batas5){
e = 0;
}
if(sensor6 > batas6){
f = 1;
}
if(sensor6 < batas6){
f = 0;
}
if(sensor7 > batas7){
g = 1;
}
if(sensor7 < batas7){
g = 0;
}
if(sensor8 > batas8){
h = 1;
}
if(sensor8 < batas8){
h = 0;
}
//maju
if((a == 1)&&(b == 1)&&(c == 0)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 0)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 0)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 0)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 0)&&(d == 0)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 0)&&(e == 0)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 0)&&(f == 0)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 0)&&(d == 0)&&(e == 0)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//maju
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 0)&&(e == 0)&&(f == 0)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//==============================================================================
//=======================================================================================
//kanan
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 0)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//kanan
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 0)&&(h == 0)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//kanan
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 0)&&(g == 0)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//kanan
if((a == 1)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 0)&&(g == 0)&&(h == 0)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
//=======================================================================================
//kiri
if((a == 0)&&(b == 1)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,LOW);
}
//kiri
if((a == 0)&&(b == 0)&&(c == 1)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,LOW);
}
//kiri
if((a == 1)&&(b == 0)&&(c == 0)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,LOW);
}
//kiri
if((a == 0)&&(b == 0)&&(c == 0)&&(d == 1)&&(e == 1)&&(f == 1)&&(g == 1)&&(h == 1)){
digitalWrite(kecmtr1,HIGH);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
digitalWrite(kecmtr2,HIGH);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,LOW);
}
//=======================================================================================
lcd.setCursor(3, 0);
lcd.print(a);lcd.print(b);lcd.print(c);lcd.print(d);lcd.print(e);lcd.print(f);lcd.print(g);lcd.print(h);
mulai();
}
void kalsenput1(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor1 = analogRead(A0);
lcd.setCursor(0, 0);
lcd.print("SENSOR1 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor1);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas1a = sensor1;
return;
}
delay(200);
kalsenput1();
}
void kalsenhit1(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor1 = analogRead(A0);
lcd.setCursor(0, 0);
lcd.print("SENSOR1 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor1);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas1b = sensor1;
batas1 = (batas1a + batas1b) / 2.0;
EEPROM.write(addr1, batas1);
return;
}
delay(200);
kalsenhit1();
}
void kalsenput2(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor2 = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("SENSOR2 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor2);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas2a = sensor2;
return;
}
delay(200);
kalsenput2();
}
void kalsenhit2(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor2 = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("SENSOR2 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor2);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas2b = sensor2;
batas2 = (batas2a + batas2b) / 2.0;
EEPROM.write(addr2, batas2);
return;
}
delay(200);
kalsenhit2();
}
void kalsenput3(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor3 = analogRead(A2);
lcd.setCursor(0, 0);
lcd.print("SENSOR3 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor3);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas3a = sensor3;
return;
}
delay(200);
kalsenput3();
}
void kalsenhit3(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor3 = analogRead(A2);
lcd.setCursor(0, 0);
lcd.print("SENSOR3 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor3);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas3b = sensor3;
batas3 = (batas3a + batas3b) / 2.0;
EEPROM.write(addr3, batas3);
return;
}
delay(200);
kalsenhit3();
}
void kalsenput4(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor4 = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print("SENSOR4 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor4);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas4a = sensor4;
return;
}
delay(200);
kalsenput4();
}
void kalsenhit4(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor4 = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print("SENSOR4 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor4);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas4b = sensor4;
batas4 = (batas4a + batas4b) / 2.0;
EEPROM.write(addr4, batas4);
return;
}
delay(200);
kalsenhit4();
}
void kalsenput5(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor5 = analogRead(A4);
lcd.setCursor(0, 0);
lcd.print("SENSOR5 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor5);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas5a = sensor5;
return;
}
delay(200);
kalsenput5();
}
void kalsenhit5(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor5 = analogRead(A4);
lcd.setCursor(0, 0);
lcd.print("SENSOR5 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor5);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas5b = sensor5;
batas5 = (batas5a + batas5b) / 2.0;
EEPROM.write(addr5, batas5);
return;
}
delay(200);
kalsenhit5();
}
void kalsenput6(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor6 = analogRead(A5);
lcd.setCursor(0, 0);
lcd.print("SENSOR6 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor6);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas6a = sensor6;
return;
}
delay(200);
kalsenput6();
}
void kalsenhit6(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor6 = analogRead(A5);
lcd.setCursor(0, 0);
lcd.print("SENSOR6 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor6);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas6b = sensor6;
batas6 = (batas6a + batas6b) / 2.0;
EEPROM.write(addr6, batas6);
return;
}
delay(200);
kalsenhit6();
}
void kalsenput7(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor7 = analogRead(A6);
lcd.setCursor(0, 0);
lcd.print("SENSOR7 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor7);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas7a = sensor7;
return;
}
delay(200);
kalsenput7();
}
void kalsenhit7(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor7 = analogRead(A6);
lcd.setCursor(0, 0);
lcd.print("SENSOR7 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor7);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas7b = sensor7;
batas7 = (batas7a + batas7b) / 2.0;
EEPROM.write(addr7, batas7);
return;
}
delay(200);
kalsenhit7();
}
void kalsenput8(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor8 = analogRead(A7);
lcd.setCursor(0, 0);
lcd.print("SENSOR8 PUTIH");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor8);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas8a = sensor8;
return;
}
delay(200);
kalsenput8();
}
void kalsenhit8(){
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
sensor8 = analogRead(A7);
lcd.setCursor(0, 0);
lcd.print("SENSOR8 HITAM");
lcd.setCursor(0, 1);
lcd.print("NILAI:");
lcd.print(sensor8);
lcd.print(" ");
if(btokx == 0){
delay(2000);
lcd.clear();
batas8b = sensor8;
batas8 = (batas8a + batas8b) / 2.0;
EEPROM.write(addr8, batas8);
return;
}
delay(200);
kalsenhit8();
}
c. VIDEO HASILNYA
No comments:
Post a Comment