Program Lampu Merah 4 Jalur Arduino dengan fuzzy logic — {link}

Saya akan bagikan buat kalian program arduino projek lampu merah empat jalur, barat, timur, selatan dan utara. Kita akan gunakan fungsi timer dari arduino untuk melakukan pewaktuan hitung mundur, karena saya males merangkai nya jadi saya gunakan simulasi dengan software proteus 8 saja untuk menjalankan simulasi traffic light.

Oh iya menggunakan tinkercad juga bisa jika anda ingin membuat simulasi arduino traffic light di sana. bebas saja sih software simulasi arduino yang ingin digunakan asalkan rangkaian sama persis, program juga akan sama hasilnya.

Nah berikut saya gunakan software tinkercad untuk membuat rangkaian simulasi lampu merahnya, tapi tidak ditambahi seven segment untuk tampilan hitung mundur.

Berikut saya sertakan kode arduino lampu merah untuk simulasi dengan aplikasi tinkecad berbasis website online.

Arduino Code Traffic Light

#define selatan 0
#define barat 1
#define utara 2
#define timur 3

int pinLed[] = {2,3,4,5,6,7,8,9,10,11,12,13};

void lampuUtara(int R, int Y, int G){
digitalWrite(pinLed[0], R);
digitalWrite(pinLed[1], Y);
digitalWrite(pinLed[2], G);
}

void lampuBarat(int R, int Y, int G){
digitalWrite(pinLed[3], R);
digitalWrite(pinLed[4], Y);
digitalWrite(pinLed[5], G);
}

void lampuTimur(int R, int Y, int G){
digitalWrite(pinLed[6], R);
digitalWrite(pinLed[7], Y);
digitalWrite(pinLed[8], G);
}

void lampuSelatan(int R, int Y, int G){
digitalWrite(pinLed[9], R);
digitalWrite(pinLed[10], Y);
digitalWrite(pinLed[11], G);
}

void setup()
{
for (int i = 0; i<12; i++){
pinMode (pinLed[i], OUTPUT);
digitalWrite(pinLed[i], 1);
}
delay(1000);
for (int i = 0; i<12; i++){
pinMode (pinLed[i], OUTPUT);
digitalWrite(pinLed[i], 0);
}

}

unsigned long LastTime = 0;
int count;
int timeGreen = 10;
int timeToleransi = 2;
int mode = selatan;

void loop() {
if (millis() - LastTime >= 500) {
LastTime = millis();
count++;
switch (mode) {
case selatan:
if (timeGreen - count > timeToleransi) {
lampuSelatan(0, 0, 1);
lampuBarat(1, 0, 0);
lampuUtara(1, 0, 0);
lampuTimur(1, 0, 0);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 1, 0);
lampuUtara(1, 0, 0);
lampuTimur(1, 0, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 0, 0);
lampuBarat(0, 0, 1);
lampuUtara(1, 0, 0);
lampuTimur(1, 0, 0);
count = 0;
mode = barat;
}
break;
case barat:
if (timeGreen - count > timeToleransi) {
lampuSelatan(1, 0, 0);
lampuBarat(0, 0, 1);
lampuUtara(1, 0, 0);
lampuTimur(1, 0, 0);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 0, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 0, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 0, 0);
lampuUtara(0, 0, 1);
lampuTimur(1, 0, 0);
count = 0;
mode = utara;
}
break;
case utara:
if (timeGreen - count > timeToleransi) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 0, 0);
lampuUtara(0, 0, 1);
lampuTimur(1, 0, 0);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 0, 0);
lampuUtara(1, 0, 0);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 0, 0);
lampuUtara(1, 0, 0);
lampuTimur(0, 0, 1);
count = 0;
mode = timur;
}
break;
case timur:
if (timeGreen - count > timeToleransi) {
lampuSelatan(1, 0, 0);
lampuBarat(1, 0, 0);
lampuUtara(1, 0, 0);
lampuTimur(0, 0, 1);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 0, 0);
lampuUtara(1, 0, 0);
lampuTimur(1, 0, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(0, 0, 1);
lampuBarat(1, 0, 0);
lampuUtara(1, 0, 0);
lampuTimur(1, 0, 0);
count = 0;
mode = barat;
}
break;
}
}
}

Sedangkan berikut adalah kode untuk simulasi dengan proteus 8 yang sudah dilengkapi sensor ultrasonik yang tujuanya untuk mendeteksi banyak kendaraan meskipun menurut saya tidak berguna tapi itulah permintaan client.

Program Arduino Traficct Light dengan fuzzy logic

//LED RYG (Red | Yellow | Green)
const int signal1[] = {A0, A1, A2}; //Pin LED Jalur S
const int signal2[] = {A3, A4, A5}; //Pin LED Jalur B
const int signal3[] = {A6, A7, A8}; //Pin LED Jalur U
const int signal4[] = {A9, A10, A11}; //Pin LED Jalur T
//Pin Seven Segment Jalur S
const int sdiPin1 = 14;
const int sclkPin1 = 15;
const int loadPin1 = 16;
//Pin Seven Segment Jalur B
const int sdiPin2 = 17;
const int sclkPin2 = 18;
const int loadPin2 = 19;
//Pin Seven Segment Jalur U
const int sdiPin3 = 38;
const int sclkPin3 = 39;
const int loadPin3 = 40;
//Pin Seven Segment Jalur T
const int sdiPin4 = 41;
const int sclkPin4 = 42;
const int loadPin4 = 43;

//bilangan HEX Array
byte digit[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7, 0x7F, 0x6F}; //tanpa uln2003

int i;

void SegmentSelatan(int val) {
int j = val % 10 ;
int i = (val / 10) % 10 ;
digitalWrite(loadPin1, LOW);
shiftOut(sdiPin1, sclkPin1, MSBFIRST, ~digit[j]); // digitdua
shiftOut(sdiPin1, sclkPin1, MSBFIRST, ~digit[i]); // digitsatu
digitalWrite(loadPin1, HIGH);
}

void SegmentBarat(int val) {
int j = val % 10 ;
int i = (val / 10) % 10 ;
digitalWrite(loadPin2, LOW);
shiftOut(sdiPin2, sclkPin2, MSBFIRST, ~digit[j]); // digitdua
shiftOut(sdiPin2, sclkPin2, MSBFIRST, ~digit[i]); // digitsatu
digitalWrite(loadPin2, HIGH);
}

void SegmentUtara(int val) {
int j = val % 10 ;
int i = (val / 10) % 10 ;
digitalWrite(loadPin3, LOW);
shiftOut(sdiPin3, sclkPin3, MSBFIRST, ~digit[j]); // digitdua
shiftOut(sdiPin3, sclkPin3, MSBFIRST, ~digit[i]); // digitsatu
digitalWrite(loadPin3, HIGH);
}

void SegmentTimur(int val) {
int j = val % 10 ;
int i = (val / 10) % 10 ;
digitalWrite(loadPin4, LOW);
shiftOut(sdiPin4, sclkPin4, MSBFIRST, ~digit[j]); // digitdua
shiftOut(sdiPin4, sclkPin4, MSBFIRST, ~digit[i]); // digitsatu
digitalWrite(loadPin4, HIGH);
}

void lampuSelatan (boolean g, boolean y, boolean r) {
digitalWrite(signal1[0], !r);
digitalWrite(signal1[1], !y);
digitalWrite(signal1[2], !g);
}
void lampuBarat (boolean g, boolean y, boolean r) {
digitalWrite(signal2[0], !r);
digitalWrite(signal2[1], !y);
digitalWrite(signal2[2], !g);
}
void lampuUtara (boolean g, boolean y, boolean r) {
digitalWrite(signal3[0], !r);
digitalWrite(signal3[1], !y);
digitalWrite(signal3[2], !g);
}
void lampuTimur (boolean g, boolean y, boolean r) {
digitalWrite(signal4[0], !r);
digitalWrite(signal4[1], !y);
digitalWrite(signal4[2], !g);
}

void setup() {
for (int i = 0; i < 3; i++) {
pinMode(signal1[i], OUTPUT);
pinMode(signal2[i], OUTPUT);
pinMode(signal3[i], OUTPUT);
pinMode(signal4[i], OUTPUT);
}

pinMode(sdiPin1, OUTPUT);
pinMode(sclkPin1, OUTPUT);
pinMode(loadPin1, OUTPUT);
pinMode(sdiPin2, OUTPUT);
pinMode(sclkPin2, OUTPUT);
pinMode(loadPin2, OUTPUT);
pinMode(sdiPin3, OUTPUT);
pinMode(sclkPin3, OUTPUT);
pinMode(loadPin3, OUTPUT);
pinMode(sdiPin4, OUTPUT);
pinMode(sclkPin4, OUTPUT);
pinMode(loadPin4, OUTPUT);
}

#define selatan 0
#define barat 1
#define utara 2
#define timur 3

unsigned long LastTime = 0;
int count;
int timeGreen = 30;
int timeToleransi = 2;
int mode = selatan;

void loop() {
if (millis() - LastTime >= 500) {
LastTime = millis();
count++;
switch (mode) {
case selatan:
SegmentSelatan(timeGreen - count);
if (timeGreen - count > timeToleransi) {
lampuSelatan(0, 1, 1);
lampuBarat(1, 1, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 0, 1);
lampuBarat(1, 1, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 0, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 1, 0);
count = 0;
delay(500);
mode = barat;
}
break;
case barat:
SegmentBarat(timeGreen - count);
if (timeGreen - count > timeToleransi) {
lampuSelatan(1, 1, 0);
lampuBarat(0, 1, 1);
lampuUtara(1, 1, 0);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 0, 1);
lampuUtara(1, 1, 0);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 1, 0);
lampuUtara(1, 0, 0);
lampuTimur(1, 1, 0);
count = 0;
delay(500);
mode = utara;
}
break;
case utara:
SegmentUtara(timeGreen - count);
if (timeGreen - count > timeToleransi) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 1, 0);
lampuUtara(0, 1, 1);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 1, 0);
lampuUtara(1, 0, 1);
lampuTimur(1, 1, 0);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 1, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 0, 0);
count = 0;
delay(500);
mode = timur;
}
break;
case timur:
SegmentTimur(timeGreen - count);
if (timeGreen - count > timeToleransi) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 1, 0);
lampuUtara(1, 1, 0);
lampuTimur(0, 1, 1);
}
else if (timeGreen - count == timeToleransi && timeGreen - count > 0) {
lampuSelatan(1, 1, 0);
lampuBarat(1, 1, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 0, 1);
}
else if (timeGreen - count == 0) {
lampuSelatan(1, 0, 1);
lampuBarat(1, 1, 0);
lampuUtara(1, 1, 0);
lampuTimur(1, 1, 0);
count = 0;
delay(500);
mode = selatan;
}
break;
}
}
}

- — #lampumerah #lampumeraharduino #trafficlight #trafficlightarduino

--

--

--

Learn microcontroller with anakkendali.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anak Kendali

Anak Kendali

Learn microcontroller with anakkendali.com

More from Medium

How to install Metamask & add Polygon Network

Tellor Update January 28th 2022

How could you address the critical zero-day vulnerability CVE-2021–44228 affecting Apache Log4j2…

Ask Me Anything ( AMA ) Series #142 Crypto Zyte x FileCash On December, 08th, 2021.