Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah robot yang dikendalikan menggunakan cahaya, robot ini seperti namanya light follower, dia akan mengikuti cahaya yang dipancarkan oleh center atau titik cahaya, jadi robot akan berjalan secara otomatis kearah titik cahaya tersebut. robot ini menggunakan tombol untuk kalibrasi terang dan redupnya cahaya sehingga user hanya perlu menekan tombol yang mana arduino akan otomatis mengolah data terang dan gelapnya. robot ini menggunakan sensor LDR atau bisa juga menggunakan photodioda. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. LCD 16x2
c. Sensor LDR
d. Motor DC + Driver L298
e. Buck Converter
f. Program Arduino IDE
#include "Wire.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define trigPin A5
#define echoPin A4
//een2 = 13
//en1 = 12
//input1 = 8
//input2 = 9
//input3 = 10
//input4 = 11
int redup1 = 0;
int redup2 = 0;
int redup3 = 0;
int baredup1,bwredup1;
int baredup2,bwredup2;
int baredup3,bwredup3;
int baterang1,bwterang1;
int baterang2,bwterang2;
int baterang3,bwterang3;
int terang1 = 0;
int terang2 = 0;
int terang3 = 0;
int x = 0;
int y = 0;
int z = 0;
int w = 0;
long duration, distance;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, INPUT);
digitalWrite(A0,HIGH);
}
void loop() {
w = digitalRead(A0);
if(w == LOW){
while(1){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
x = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("SET REDUP 1");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(x);
if(w == LOW){
redup1 = x;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
y = analogRead(A2);
lcd.setCursor(0, 0);
lcd.print("SET REDUP 2");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(y);
if(w == LOW){
redup2 = y;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
z = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print("SET REDUP 3");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(z);
if(w == LOW){
redup3 = z;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
x = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("SET TERANG 1");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(x);
if(w == LOW){
terang1 = x;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
y = analogRead(A2);
lcd.setCursor(0, 0);
lcd.print("SET TERANG 2");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(y);
if(w == LOW){
terang2 = y;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
z = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print("SET TERANG 3");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(z);
if(w == LOW){
terang3 = z;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
Serial.print("redup1 = ");
Serial.println(redup1);
Serial.print("redup2 = ");
Serial.println(redup2);
Serial.print("redup3 = ");
Serial.println(redup3);
Serial.print("terang1 = ");
Serial.println(terang1);
Serial.print("terang2 = ");
Serial.println(terang2);
Serial.print("terang3 = ");
Serial.println(terang3);
return;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
analogWrite(8,200);
analogWrite(9,200);
baredup1 = redup1 + 5;
bwredup1 = redup1 - 5;
baredup2 = redup2 + 5;
bwredup2 = redup2 - 5;
baredup3 = redup3 + 5;
bwredup3 = redup3 - 5;
baterang1 = terang1 + 5;
bwterang1 = terang1 - 5;
baterang2 = terang2 + 5;
bwterang2 = terang2 - 5;
baterang3 = terang3 + 5;
bwterang3 = terang3 - 5;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
x = analogRead(A1);
y = analogRead(A2);
z = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print(x);
lcd.print(" ");
lcd.print(y);
lcd.print(" ");
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("jarak= ");
lcd.print(distance);
if((x > bwredup1)&&(x < baredup1)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(10);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((y > bwredup2)&&(y < baredup2)&&(distance > 15 )){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(10);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((z > bwredup3)&&(z < baredup3)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(10);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((x > bwterang1)&&(x < baterang1)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(50);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((y > bwterang2)&&(y < baterang2)&&(distance > 15 )){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(50);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((z > bwterang3)&&(z < baterang3)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(50);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
}
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define trigPin A5
#define echoPin A4
//een2 = 13
//en1 = 12
//input1 = 8
//input2 = 9
//input3 = 10
//input4 = 11
int redup1 = 0;
int redup2 = 0;
int redup3 = 0;
int baredup1,bwredup1;
int baredup2,bwredup2;
int baredup3,bwredup3;
int baterang1,bwterang1;
int baterang2,bwterang2;
int baterang3,bwterang3;
int terang1 = 0;
int terang2 = 0;
int terang3 = 0;
int x = 0;
int y = 0;
int z = 0;
int w = 0;
long duration, distance;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, INPUT);
digitalWrite(A0,HIGH);
}
void loop() {
w = digitalRead(A0);
if(w == LOW){
while(1){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
x = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("SET REDUP 1");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(x);
if(w == LOW){
redup1 = x;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
y = analogRead(A2);
lcd.setCursor(0, 0);
lcd.print("SET REDUP 2");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(y);
if(w == LOW){
redup2 = y;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
z = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print("SET REDUP 3");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(z);
if(w == LOW){
redup3 = z;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
x = analogRead(A1);
lcd.setCursor(0, 0);
lcd.print("SET TERANG 1");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(x);
if(w == LOW){
terang1 = x;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
y = analogRead(A2);
lcd.setCursor(0, 0);
lcd.print("SET TERANG 2");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(y);
if(w == LOW){
terang2 = y;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
while(1){
w = digitalRead(A0);
z = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print("SET TERANG 3");
lcd.setCursor(0, 1);
lcd.print("ADC = ");
lcd.print(z);
if(w == LOW){
terang3 = z;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LOADING....");
delay(3000);
lcd.clear();
Serial.print("redup1 = ");
Serial.println(redup1);
Serial.print("redup2 = ");
Serial.println(redup2);
Serial.print("redup3 = ");
Serial.println(redup3);
Serial.print("terang1 = ");
Serial.println(terang1);
Serial.print("terang2 = ");
Serial.println(terang2);
Serial.print("terang3 = ");
Serial.println(terang3);
return;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
analogWrite(8,200);
analogWrite(9,200);
baredup1 = redup1 + 5;
bwredup1 = redup1 - 5;
baredup2 = redup2 + 5;
bwredup2 = redup2 - 5;
baredup3 = redup3 + 5;
bwredup3 = redup3 - 5;
baterang1 = terang1 + 5;
bwterang1 = terang1 - 5;
baterang2 = terang2 + 5;
bwterang2 = terang2 - 5;
baterang3 = terang3 + 5;
bwterang3 = terang3 - 5;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
x = analogRead(A1);
y = analogRead(A2);
z = analogRead(A3);
lcd.setCursor(0, 0);
lcd.print(x);
lcd.print(" ");
lcd.print(y);
lcd.print(" ");
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("jarak= ");
lcd.print(distance);
if((x > bwredup1)&&(x < baredup1)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(10);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((y > bwredup2)&&(y < baredup2)&&(distance > 15 )){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(10);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((z > bwredup3)&&(z < baredup3)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(10);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((x > bwterang1)&&(x < baterang1)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(50);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((y > bwterang2)&&(y < baterang2)&&(distance > 15 )){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(50);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
if((z > bwterang3)&&(z < baterang3)){
for (int i=0; i <= 10; i++){
//kiri
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(50);
//kiri
digitalWrite(12,LOW);
digitalWrite(13,LOW);
//kanan
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(20);
}
}
}
g. VIDEO HASILNYA