Translate

Simulasi Traffic Light Simpang 4 Proteus 7 dan ARDUINO

Simulasi Traffic Light Simpang 4 Proteus 7 dan ARDUINO


               Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat atau simulasi yang berfungsi sebagaimana kinerja dari traffic light atau lampu merah di perkotaan. alat ini menggunakan arduino dan interface lcd i2c serta dilengkapi dengan RTC. untuk lebih jelasnya berikut komponen dan kodingnya.



a. Arduino




b. Lcd 16x2 + I2C




c. RTC Ds1307




d. Led




e. Push Button






f. Program Arduino IDE

#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#define DS3231_I2C_ADDRESS 0x68
//0x20 untuk simulasi proteus jika real gunakan 0x27 atau 0x3F
LiquidCrystal_I2C lcd(0x20,16,2); 
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;
int addr5 = 4;
int addr6 = 5;

int ledg1 = 0;
int ledy1 = 1;
int ledr1 = 2;

int ledg2 = 3;
int ledy2 = 4;
int ledr2 = 5;

int ledg3 = 6;
int ledy3 = 7;
int ledr3 = 8;

int ledg4 = 9;
int ledy4 = 10;
int ledr4 = 11;

int btset = A0;
int btup = A1;
int btdown = A2;
int btok = A3;

int btsetx = 0;
int btupx = 0;
int btdownx = 0;
int btokx = 0;

int delaykuning = 300;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}

int jam,menit,hari,awalflash,akhirflash;
int hijau1,hijau2,hijau3,hijau4;


void setup()
{
  
hijau1 = EEPROM.read(addr1);
hijau2 = EEPROM.read(addr2);
hijau3 = EEPROM.read(addr3);
hijau4 = EEPROM.read(addr4);
awalflash = EEPROM.read(addr5);
akhirflash = EEPROM.read(addr6);

lcd.begin();   
lcd.noCursor();
lcd.clear();

pinMode(ledg1,OUTPUT);
pinMode(ledy1,OUTPUT);
pinMode(ledr1,OUTPUT);

pinMode(ledg2,OUTPUT);
pinMode(ledy2,OUTPUT);
pinMode(ledr2,OUTPUT);

pinMode(ledg3,OUTPUT);
pinMode(ledy3,OUTPUT);
pinMode(ledr3,OUTPUT);

pinMode(ledg4,OUTPUT);
pinMode(ledy4,OUTPUT);
pinMode(ledr4,OUTPUT);

pinMode(btset,INPUT_PULLUP);
pinMode(btup,INPUT_PULLUP);
pinMode(btdown,INPUT_PULLUP);
pinMode(btok,INPUT_PULLUP);

  Wire.begin();
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(0,47,11,6,23,11,18);
   
btsetx = digitalRead(btset);
   
if(btsetx == 0)
{
   lcd.clear();  
   hijau1 = 0;
   hijau2 = 0;
   hijau3 = 0;
   hijau4 = 0;
   
   awalflash = 0;
   akhirflash = 0;
   
   settingjam();
   settingmenit();
   settinghari();
   setdelayhijau1();
   setdelayhijau2();
   setdelayhijau3();
   setdelayhijau4();
   setawalflash();
   setakhirflash();
   EEPROM.write(addr1, hijau1);
   EEPROM.write(addr2, hijau2);
   EEPROM.write(addr3, hijau3);
   EEPROM.write(addr4, hijau4);
   EEPROM.write(addr5, awalflash);
   EEPROM.write(addr6, akhirflash);
  // DS3231 seconds, minutes, hours, day, date, month, year
   setDS3231time(0,menit,jam,hari,23,11,20);
   
}


void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  lcd.setCursor(0,0);
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  lcd.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second<10)
  {
    lcd.print("0");
  }
  lcd.print(second, DEC);
  lcd.print(" ");
/* 
  lcd.setCursor(0,1);
  lcd.print(dayOfMonth, DEC);
  lcd.setCursor(0,2);
  lcd.print(month, DEC);
  lcd.print(" / ");
  lcd.print(year, DEC);
  lcd.setCursor(0,3);
  //lcd.print(" Day of week: ");
 
  switch(dayOfWeek){
  case 1:
    lcd.print("Sunday");
    break;
  case 2:
    lcd.print("Monday");
    break;
  case 3:
    lcd.print("Tuesday");
    break;
  case 4:
    lcd.print("Wednesday");
    break;
  case 5:
    lcd.print("Thursday");
    break;
  case 6:
    lcd.print("Friday");
    break;
  case 7:
    lcd.print("Saturday");
    break;
  }
*/  

lcd.setCursor(0,1);
lcd.print("TEXT BISA EDIT");

}


void loop()
{
   displayTime();
   delay(1);

//hijau apil 1   
digitalWrite(ledg1,LOW);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,HIGH);

digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,LOW);

digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,LOW);

digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,LOW);
delay(hijau1);

//kuning apil 1 
digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,HIGH);
delay(delaykuning);


//hijau apil 2   
digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,LOW);

digitalWrite(ledg2,LOW);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,HIGH);

digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,LOW);

digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,LOW);
delay(hijau2);

//kuning apil 2 
digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,HIGH);
delay(delaykuning);


//hijau apil 3   
digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,LOW);

digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,LOW);

digitalWrite(ledg3,LOW);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,HIGH);

digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,LOW);
delay(hijau3);

//kuning apil 3 
digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,HIGH);
delay(delaykuning);
   

//hijau apil 4   
digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,LOW);

digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,LOW);

digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,LOW);

digitalWrite(ledg4,LOW);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,HIGH);
delay(hijau4);

//kuning apil 4 
digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,HIGH);
delay(delaykuning);   

if(hour == awalflash){
mulaiflash();
}
      
}


void settingjam(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("Jam = ");
lcd.print(jam);
lcd.print("   ");

if(btupx == 0){
delay(10);
jam++;
}
if(btdownx == 0){
delay(10);
jam--;
}

if(jam > 23){
jam = 0;
}

if(jam < 0){
jam = 23;
}

if(btokx == 0){
lcd.clear();
delay(100);
return;
}
  
settingjam();  
}


void settingmenit(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("menit = ");
lcd.print(menit);
lcd.print("   ");

if(btupx == 0){
menit++;
}
if(btdownx == 0){
menit--;
}

if(menit > 59){
menit = 0;
}

if(menit < 0){
menit = 59;
}

if(btokx == 0){
lcd.clear();
delay(100);
return;
}
  
settingmenit();  
}



void settinghari(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("hari= ");

if(hari == 1){
lcd.print("senin     ");
}
if(hari == 2){
lcd.print("selasa   ");
}
if(hari == 3){
lcd.print("rabu     ");
}
if(hari == 4){
lcd.print("kamis    ");
}
if(hari == 5){
lcd.print("jumat     ");
}
if(hari == 6){
lcd.print("sabtu    ");
}
if(hari == 7){
lcd.print("minggu    ");
}

if(btupx == 0){
hari++;
}
if(btdownx == 0){
hari--;
}

if(hari < 1){
hari = 7;
}

if(hari > 7){
hari = 1;
}

if(btokx == 0){
lcd.clear();
delay(100);
return;
}
  
settinghari();  
}



void setdelayhijau1(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("delay1= ");
lcd.print(hijau1);
lcd.print("   ");

if(btupx == 0){
hijau1++;
}
if(btdownx == 0){
hijau1--;
}

if(hijau1 < 1){
hijau1 = 1;
}


if(btokx == 0){
lcd.clear();
delay(100);
hijau1 = hijau1 * 100; 
return;
}
  
setdelayhijau1();  
}



void setdelayhijau2(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("delay2= ");
lcd.print(hijau2);
lcd.print("   ");

if(btupx == 0){
hijau2++;
}
if(btdownx == 0){
hijau2--;
}

if(hijau2 < 1){
hijau2 = 1;
}


if(btokx == 0){
lcd.clear();
delay(100);
hijau2 = hijau2 * 100; 
return;
}
  
setdelayhijau2();  
}


void setdelayhijau3(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("delay3= ");
lcd.print(hijau3);
lcd.print("   ");

if(btupx == 0){
hijau3++;
}
if(btdownx == 0){
hijau3--;
}

if(hijau3 < 1){
hijau3 = 1;
}


if(btokx == 0){
lcd.clear();
delay(100);
hijau3 = hijau3 * 100; 
return;
}
  
setdelayhijau3();  
}


void setdelayhijau4(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("delay4= ");
lcd.print(hijau4);
lcd.print("   ");

if(btupx == 0){
hijau4++;
}
if(btdownx == 0){
hijau4--;
}

if(hijau4 < 1){
hijau4 = 1;
}


if(btokx == 0){
lcd.clear();
delay(100);
hijau4 = hijau4 * 100; 
return;
}
  
setdelayhijau4();  
}



void setawalflash(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("awalflash= ");
lcd.print(awalflash);
lcd.print("   ");

if(btupx == 0){
awalflash++;
}
if(btdownx == 0){
awalflash--;
}

if(awalflash < 0){
awalflash = 23;
}

if(awalflash > 23){
awalflash = 0;
}

if(btokx == 0){
lcd.clear();
delay(100);
return;
}
  
setawalflash();  
}


void setakhirflash(){

btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("akhirflash= ");
lcd.print(akhirflash);
lcd.print("   ");

if(btupx == 0){
akhirflash++;
}
if(btdownx == 0){
akhirflash--;
}

if(akhirflash < 0){
akhirflash = 23;
}

if(akhirflash > 23){
akhirflash = 0;
}

if(btokx == 0){
lcd.clear();
delay(100);
return;
}
  
setakhirflash();  
}




void mulaiflash(){
  
displayTime();
delay(1);
  
digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,HIGH);

digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,HIGH);

digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,HIGH);

digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,HIGH);
delay(200);

digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,HIGH);

digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,HIGH);

digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,HIGH);

digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,HIGH);
delay(200);

if(hour == akhirflash){
return;
}

mulaiflash();
}






g. VIDEO HASILNYA






No comments:

Post a Comment