Robot Pendeteksi Warna Arduino
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana membuat sebuah robot beroda yang dapat mendeteksi beberapa warna sekaligus. alat yang dideteksi yaitu kuning, merah, hijau, hitam, biru. alat ini menggunakan sensor Tcs3200 dan Arduino Uno. untuk lebih jelasnya berikut adalah koding dan komponennya.
a. Komponen
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define S0 12
#define S1 11
#define S2 8
#define S3 7
#define sensorOut 9
int frequency = 0;
int fmerah;
int fgreen;
int fblue;
int btkuning = 2;
int bthitam = 3;
int bthijau = 4;
int btbiru = 5;
int btmerah = 6;
int btkuningx;
int bthitamx;
int bthijaux;
int btbirux;
int btmerahx;
int btonmotor = 13;
int btonmotorx;
int motorkiri1 = A3;
int motorkiri2 = A2;
int motorkanan1 = A0;
int motorkanan2 = A1;
int fmerahatasku,fmerahatashi,fmerahatashij,fmerahatasbi,fmerahatasme;
int fmerahbawahku,fmerahbawahhi,fmerahbawahhij,fmerahbawahbi,fmerahbawahme;
int fgreenatasku,fgreenatashi,fgreenatashij,fgreenatasbi,fgreenatasme;
int fgreenbawahku,fgreenbawahhi,fgreenbawahhij,fgreenbawahbi,fgreenbawahme;
int fblueatasku,fblueatashi,fblueatashij,fblueatasbi,fblueatasme;
int fbluebawahku,fbluebawahhi,fbluebawahhij,fbluebawahbi,fbluebawahme;
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
pinMode(btkuning,INPUT_PULLUP);
pinMode(bthitam,INPUT_PULLUP);
pinMode(bthijau,INPUT_PULLUP);
pinMode(btbiru,INPUT_PULLUP);
pinMode(btmerah,INPUT_PULLUP);
pinMode(btonmotor,INPUT_PULLUP);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
pinMode(motorkiri1, OUTPUT);
pinMode(motorkiri2, OUTPUT);
pinMode(motorkanan1, OUTPUT);
pinMode(motorkanan2, OUTPUT);
}
void loop(){
btonmotorx = digitalRead(btonmotor);
btkuningx = digitalRead(btkuning);
bthitamx = digitalRead(bthitam);
bthijaux = digitalRead(bthijau);
btbirux = digitalRead(btbiru);
btmerahx = digitalRead(btmerah);
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
frequency = pulseIn(sensorOut, LOW);
fmerah = frequency;
lcd.setCursor(0,0);
lcd.print(fmerah);
lcd.print(" ");
delay(100);
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
frequency = pulseIn(sensorOut, LOW);
fgreen = frequency;
lcd.print(fgreen);
lcd.print(" ");
delay(100);
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
frequency = pulseIn(sensorOut, LOW);
fblue = frequency;
lcd.print(fblue);
lcd.print(" ");
delay(800);
if(btkuningx == 0){
delay(200);
fmerahatasku = fmerah + 5;
fmerahbawahku = fmerah - 5;
fgreenatasku = fgreen + 5;
fgreenbawahku = fgreen - 5;
fblueatasku = fblue + 5;
fbluebawahku = fblue - 5;
}
if(bthitamx == 0){
delay(200);
fmerahatashi = fmerah + 5;
fmerahbawahhi = fmerah - 5;
fgreenatashi = fgreen + 5;
fgreenbawahhi = fgreen - 5;
fblueatashi = fblue + 5;
fbluebawahhi = fblue - 5;
}
if(bthijaux == 0){
delay(200);
fmerahatashij = fmerah + 5;
fmerahbawahhij = fmerah - 5;
fgreenatashij = fgreen + 5;
fgreenbawahhij = fgreen - 5;
fblueatashij = fblue + 5;
fbluebawahhij = fblue - 5;
}
if(btbirux == 0){
delay(200);
fmerahatasbi = fmerah + 5;
fmerahbawahbi = fmerah - 5;
fgreenatasbi = fgreen + 5;
fgreenbawahbi = fgreen - 5;
fblueatasbi = fblue + 5;
fbluebawahbi = fblue - 5;
}
if(btmerahx == 0){
delay(200);
fmerahatasme = fmerah + 5;
fmerahbawahme = fmerah - 5;
fgreenatasme = fgreen + 5;
fgreenbawahme = fgreen - 5;
fblueatasme = fblue + 5;
fbluebawahme = fblue - 5;
}
if(btonmotorx == 0){
delay(200);
digitalWrite(motorkiri1,HIGH);
digitalWrite(motorkiri2,LOW);
digitalWrite(motorkanan1,HIGH);
digitalWrite(motorkanan2,LOW);
}
if(btonmotorx == 1){
delay(200);
digitalWrite(motorkiri1,LOW);
digitalWrite(motorkiri2,LOW);
digitalWrite(motorkanan1,LOW);
digitalWrite(motorkanan2,LOW);
}
if((fmerah < fmerahatasku)&&(fmerah > fmerahbawahku)&&(fgreen < fgreenatasku)&&(fgreen > fgreenbawahku)&&(fblue < fblueatasku)&&(fblue > fbluebawahku)){
lcd.setCursor(0,1);
lcd.print("KUNING ");
}
if((fmerah < fmerahatashi)&&(fmerah > fmerahbawahhi)&&(fgreen < fgreenatashi)&&(fgreen > fgreenbawahhi)&&(fblue < fblueatashi)&&(fblue > fbluebawahhi)){
lcd.setCursor(0,1);
lcd.print("HITAM ");
}
if((fmerah < fmerahatashij)&&(fmerah > fmerahbawahhij)&&(fgreen < fgreenatashij)&&(fgreen > fgreenbawahhij)&&(fblue < fblueatashij)&&(fblue > fbluebawahhij)){
lcd.setCursor(0,1);
lcd.print("HIJAU ");
}
if((fmerah < fmerahatasbi)&&(fmerah > fmerahbawahbi)&&(fgreen < fgreenatasbi)&&(fgreen > fgreenbawahbi)&&(fblue < fblueatasbi)&&(fblue > fbluebawahbi)){
lcd.setCursor(0,1);
lcd.print("BIRU ");
}
if((fmerah < fmerahatasme)&&(fmerah > fmerahbawahme)&&(fgreen < fgreenatasme)&&(fgreen > fgreenbawahme)&&(fblue < fblueatasme)&&(fblue > fbluebawahme)){
lcd.setCursor(0,1);
lcd.print("MERAH ");
}
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define S0 12
#define S1 11
#define S2 8
#define S3 7
#define sensorOut 9
int frequency = 0;
int fmerah;
int fgreen;
int fblue;
int btkuning = 2;
int bthitam = 3;
int bthijau = 4;
int btbiru = 5;
int btmerah = 6;
int btkuningx;
int bthitamx;
int bthijaux;
int btbirux;
int btmerahx;
int btonmotor = 13;
int btonmotorx;
int motorkiri1 = A3;
int motorkiri2 = A2;
int motorkanan1 = A0;
int motorkanan2 = A1;
int fmerahatasku,fmerahatashi,fmerahatashij,fmerahatasbi,fmerahatasme;
int fmerahbawahku,fmerahbawahhi,fmerahbawahhij,fmerahbawahbi,fmerahbawahme;
int fgreenatasku,fgreenatashi,fgreenatashij,fgreenatasbi,fgreenatasme;
int fgreenbawahku,fgreenbawahhi,fgreenbawahhij,fgreenbawahbi,fgreenbawahme;
int fblueatasku,fblueatashi,fblueatashij,fblueatasbi,fblueatasme;
int fbluebawahku,fbluebawahhi,fbluebawahhij,fbluebawahbi,fbluebawahme;
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
pinMode(btkuning,INPUT_PULLUP);
pinMode(bthitam,INPUT_PULLUP);
pinMode(bthijau,INPUT_PULLUP);
pinMode(btbiru,INPUT_PULLUP);
pinMode(btmerah,INPUT_PULLUP);
pinMode(btonmotor,INPUT_PULLUP);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
pinMode(motorkiri1, OUTPUT);
pinMode(motorkiri2, OUTPUT);
pinMode(motorkanan1, OUTPUT);
pinMode(motorkanan2, OUTPUT);
}
void loop(){
btonmotorx = digitalRead(btonmotor);
btkuningx = digitalRead(btkuning);
bthitamx = digitalRead(bthitam);
bthijaux = digitalRead(bthijau);
btbirux = digitalRead(btbiru);
btmerahx = digitalRead(btmerah);
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
frequency = pulseIn(sensorOut, LOW);
fmerah = frequency;
lcd.setCursor(0,0);
lcd.print(fmerah);
lcd.print(" ");
delay(100);
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
frequency = pulseIn(sensorOut, LOW);
fgreen = frequency;
lcd.print(fgreen);
lcd.print(" ");
delay(100);
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
frequency = pulseIn(sensorOut, LOW);
fblue = frequency;
lcd.print(fblue);
lcd.print(" ");
delay(800);
if(btkuningx == 0){
delay(200);
fmerahatasku = fmerah + 5;
fmerahbawahku = fmerah - 5;
fgreenatasku = fgreen + 5;
fgreenbawahku = fgreen - 5;
fblueatasku = fblue + 5;
fbluebawahku = fblue - 5;
}
if(bthitamx == 0){
delay(200);
fmerahatashi = fmerah + 5;
fmerahbawahhi = fmerah - 5;
fgreenatashi = fgreen + 5;
fgreenbawahhi = fgreen - 5;
fblueatashi = fblue + 5;
fbluebawahhi = fblue - 5;
}
if(bthijaux == 0){
delay(200);
fmerahatashij = fmerah + 5;
fmerahbawahhij = fmerah - 5;
fgreenatashij = fgreen + 5;
fgreenbawahhij = fgreen - 5;
fblueatashij = fblue + 5;
fbluebawahhij = fblue - 5;
}
if(btbirux == 0){
delay(200);
fmerahatasbi = fmerah + 5;
fmerahbawahbi = fmerah - 5;
fgreenatasbi = fgreen + 5;
fgreenbawahbi = fgreen - 5;
fblueatasbi = fblue + 5;
fbluebawahbi = fblue - 5;
}
if(btmerahx == 0){
delay(200);
fmerahatasme = fmerah + 5;
fmerahbawahme = fmerah - 5;
fgreenatasme = fgreen + 5;
fgreenbawahme = fgreen - 5;
fblueatasme = fblue + 5;
fbluebawahme = fblue - 5;
}
if(btonmotorx == 0){
delay(200);
digitalWrite(motorkiri1,HIGH);
digitalWrite(motorkiri2,LOW);
digitalWrite(motorkanan1,HIGH);
digitalWrite(motorkanan2,LOW);
}
if(btonmotorx == 1){
delay(200);
digitalWrite(motorkiri1,LOW);
digitalWrite(motorkiri2,LOW);
digitalWrite(motorkanan1,LOW);
digitalWrite(motorkanan2,LOW);
}
if((fmerah < fmerahatasku)&&(fmerah > fmerahbawahku)&&(fgreen < fgreenatasku)&&(fgreen > fgreenbawahku)&&(fblue < fblueatasku)&&(fblue > fbluebawahku)){
lcd.setCursor(0,1);
lcd.print("KUNING ");
}
if((fmerah < fmerahatashi)&&(fmerah > fmerahbawahhi)&&(fgreen < fgreenatashi)&&(fgreen > fgreenbawahhi)&&(fblue < fblueatashi)&&(fblue > fbluebawahhi)){
lcd.setCursor(0,1);
lcd.print("HITAM ");
}
if((fmerah < fmerahatashij)&&(fmerah > fmerahbawahhij)&&(fgreen < fgreenatashij)&&(fgreen > fgreenbawahhij)&&(fblue < fblueatashij)&&(fblue > fbluebawahhij)){
lcd.setCursor(0,1);
lcd.print("HIJAU ");
}
if((fmerah < fmerahatasbi)&&(fmerah > fmerahbawahbi)&&(fgreen < fgreenatasbi)&&(fgreen > fgreenbawahbi)&&(fblue < fblueatasbi)&&(fblue > fbluebawahbi)){
lcd.setCursor(0,1);
lcd.print("BIRU ");
}
if((fmerah < fmerahatasme)&&(fmerah > fmerahbawahme)&&(fgreen < fgreenatasme)&&(fgreen > fgreenbawahme)&&(fblue < fblueatasme)&&(fblue > fbluebawahme)){
lcd.setCursor(0,1);
lcd.print("MERAH ");
}
}
c. VIDEO HASILNYA
No comments:
Post a Comment