Anak Kendali
6 min readAug 10, 2022

--

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;
}
}
}

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;
}
}
}

--

--

Anak Kendali
0 Followers

Learn microcontroller with anakkendali.com