Friday, 5 July 2024

Traffic Light Setup & Flasing

 

// TRIMAN      //
// JULI 2024 //
#include <Wire.h>
#include <EEPROM.h>
#include "RTClib.h"
#include <SPI.h>
#include "PCF8574.h"
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;
char namaHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
#define DS3231_I2C_ADDRESS 0x68
//0x20 untuk simulasi proteus jika real gunakan 0x27 atau 0x3F
#define DEVICE_2 B0100001
PCF8574 pcf8574( 0x27);
LiquidCrystal_I2C lcd(0x20,16,4); 
//LiquidCrystal_I2C lcd(0x38,16,2);
byte second, minute, hour, day, date, month, year;
int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;
int addr5 = 4;
int addr6 = 5;
int addr7 = 6;
int addr8 = 7;
int addr9 = 8;
int addr10 = 9;
int addr11 = 10;
int addr12 = 11;
int addr13 = 12;

int ledg1 = 2;
int ledy1 = 3;
int ledr1 = 4;
int ledg2 = 5;
int ledy2 = 6;
int ledr2 = 7;
int ledg3 = 8;
int ledy3 = 9;
int ledr3 = 10;
int ledg4 = 11;
int ledy4 = 12;
int ledr4 = 13;

// int ledg0 = A2;
// int ledr0 = A3; 

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 delayyellow = 400;
int delayyellow2 = 400;
// I1t delaygreen1 = 1000;
// int delaygreen2 = 1000;
// int delaygreen3 = 1000;
// int delaygreen4 = 1000;
int delaycross = 0;
// 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,startFlash,endFlash;
int green1,green2,green3,green4;
void setup()
{
  
   Serial.begin(9600);
  if (! rtc.begin()) {
    Serial.println("RTC TIDAK TERBACA");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//update rtc dari waktu komputer
  }

green1 = EEPROM.read(addr1);
green2 = EEPROM.read(addr2);
green3 = EEPROM.read(addr3);
green4 = EEPROM.read(addr4);
startFlash = EEPROM.read(addr5);
endFlash = EEPROM.read(addr6);

//startFlash = 9;
//endFlash = 10;  //
lcd.begin();
//lcd.init();                      // inisialisasi the lcd 
// lcd2.init();                      // inisialisasi the lcd 
lcd.noCursor();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Info: Triman");
lcd.setCursor(4,1);
lcd.print("081389248685");
  delay (1600);
lcd.clear();

    // Set pinMode to OUTPUT
pcf8574.pinMode(P2, OUTPUT);
pcf8574.pinMode(P1, OUTPUT);
pcf8574.begin();

// pinMode(ledg0,OUTPUT);
// pinMode(ledr0,OUTPUT);

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);
  //  DS3231 seconds, minutes, hours, day, date, month, year
//setDS3231time(0,40,9,2,7,11,23);  //===================================
  startFlash = 22;  //=======================================================
  endFlash = 6;  //=========================================================
    green1 = 1000;  //=========================================================
    green2 = 1000;  //=========================================================
    green3 = 1000;  //=========================================================
    green4 = 1000;  //=========================================================
   
   
btsetx = digitalRead(btset);
   
if(btsetx == 0)
{
   lcd.clear(); 
   green1 = 0;
   green2 = 0;
   green3 = 0;
   green4 = 0;
   
   startFlash = 0;
   endFlash = 0;
   
//   settingjam();
//   settingmenit();
//   settinghari();
//   setdelaygreen1();
//   setdelaygreen2();
//   setdelaygreen3();
//   setdelaygreen4();
   setstartFlash();
   setendFlash();
   EEPROM.write(addr1, green1);
   EEPROM.write(addr2, green2);
   EEPROM.write(addr3, green3);
   EEPROM.write(addr4, green4);
   EEPROM.write(addr5, startFlash);
   EEPROM.write(addr6, endFlash);
  // 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 day, byte
date, 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(day)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(date)); // 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 *day,
byte *date,
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);
  *day = bcdToDec(Wire.read());
  *date = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
 lcd.clear();
  lcd.setCursor(0,0);
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &day, &date, &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(14,0);
   lcd.print("RHL-01");
    
  lcd.setCursor(0,1);
  lcd.print(date, DEC);
  lcd.print("/");
  
  lcd.setCursor(3,1);
  lcd.print(month, DEC);
  lcd.print("/20");
  lcd.print(year, DEC);
  lcd.setCursor(16,1);
  lcd.print("3MAN");

//lcd.setCursor(0,2);
// lcd.print("Info:081389248685");


lcd.setCursor(0,1);
lcd.print("Flash =");
lcd.print(startFlash);
lcd.print("-");
lcd.print(endFlash);

lcd.setCursor(0,2);
lcd.print("G1= ");
lcd.print(green1);
lcd.print("  ");

lcd.setCursor(10,2);
lcd.print("G2= ");
lcd.print(green2);
lcd.print("  ");

lcd.setCursor(0,3);
lcd.print("G3= ");
lcd.print(green3);
lcd.print("");

lcd.setCursor(10,3);
lcd.print("G4= ");
lcd.print(green4);
lcd.print("");
  /*

lcd.setCursor(13,2);
lcd.print("Y  = ");
lcd.print(delayyellow);
lcd.print("");
  for (int i = 3; i < 32; i++) {
    lcd.scrollDisplayLeft();
  }
  */  
//lcd.print("endFlash= ");
// lcd.print(endFlash);
// lcd.print("   ");

}

void loop()
{
    DateTime now = rtc.now();
    Serial.print(namaHari[now.day()]);  
    Serial.print(',');    
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(" ");   
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
Serial.print("startFlash= ");
Serial.print(startFlash);
Serial.print("   ");

Serial.print("endFlash= ");
Serial.print(endFlash);
Serial.print("   ");

Serial.print("delayyellow= ");
Serial.print(delayyellow);
Serial.print("   ");

Serial.print("delaycross= ");
Serial.print(delaycross);
Serial.print("   ");

   
    delay(1000);
   displayTime();
   delay(10);
//JALURl 1   
digitalWrite(ledg1,LOW);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,LOW);
delay(delayyellow);

digitalWrite(ledg1,HIGH);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,LOW);
digitalWrite(ledg2,LOW);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,HIGH);
digitalWrite(ledg3,LOW);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,HIGH);
digitalWrite(ledg4,LOW);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,HIGH);
delay(green1);
//yellow Jalan 1 
digitalWrite(ledg1,LOW);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,LOW);
delay(delayyellow2);
// ZEBRA
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,HIGH);
pcf8574.digitalWrite(P1,HIGH);
pcf8574.digitalWrite(P2,LOW);
delay(delaycross);
pcf8574.digitalWrite(P1,LOW);
pcf8574.digitalWrite(P2,HIGH);
//JALURl 2   

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

digitalWrite(ledg1,LOW);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,HIGH);
digitalWrite(ledg2,HIGH);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,LOW);
digitalWrite(ledg3,LOW);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,HIGH);
digitalWrite(ledg4,LOW);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,HIGH);
delay(green2);

//yellow Jalan 2 
digitalWrite(ledg2,LOW);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,LOW);
delay(delayyellow2);

// ZEBRA
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,HIGH);
pcf8574.digitalWrite(P1,HIGH);
pcf8574.digitalWrite(P2,LOW);
delay(delaycross);
pcf8574.digitalWrite(P1,LOW);
pcf8574.digitalWrite(P2,HIGH);

//JALURl 3   

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

digitalWrite(ledg1,LOW);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,HIGH);
digitalWrite(ledg2,LOW);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,HIGH);
digitalWrite(ledg3,HIGH);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,LOW);
digitalWrite(ledg4,LOW);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,HIGH);
delay(green3);
//yellow Jalan 3 
digitalWrite(ledg3,LOW);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,LOW);
delay(delayyellow2);
   

// ZEBRA
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,HIGH);
pcf8574.digitalWrite(P1,HIGH);
pcf8574.digitalWrite(P2,LOW);
delay(delaycross);
pcf8574.digitalWrite(P1,LOW);
pcf8574.digitalWrite(P2,HIGH);

//JALURl 4   

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

digitalWrite(ledg1,LOW);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,HIGH);
digitalWrite(ledg2,LOW);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,HIGH);
digitalWrite(ledg3,LOW);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,HIGH);
digitalWrite(ledg4,HIGH);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,LOW);
delay(green4);
//yellow Jalan 4 
digitalWrite(ledg4,LOW);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,LOW);
delay(delayyellow2);
 

// ZEBRA
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,HIGH);
pcf8574.digitalWrite(P1,HIGH);
pcf8574.digitalWrite(P2,LOW);
delay(delaycross);
pcf8574.digitalWrite(P1,LOW);
pcf8574.digitalWrite(P2,HIGH);
if(hour == startFlash){
runFlash();
}
     
}
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 setdelaygreen1(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
lcd.setCursor(0,0);
lcd.print("delay1= ");
lcd.print(green1);
lcd.print("   ");
if(btupx == 0){
green1++;
}
if(btdownx == 0){
green1--;
}
if(green1 < 1){
green1 = 1;
}
if(btokx == 0){
lcd.clear();
delay(100);
green1 = green1 * 100;
return;
}
 
setdelaygreen1(); 
}
void setdelaygreen2(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
lcd.setCursor(0,0);
lcd.print("delay2= ");
lcd.print(green2);
lcd.print("   ");
if(btupx == 0){
green2++;
}
if(btdownx == 0){
green2--;
}
if(green2 < 1){
green2 = 1;
}
if(btokx == 0){
lcd.clear();
delay(100);
green2 = green2 * 100;
return;
}
 
setdelaygreen2(); 
}
void setdelaygreen3(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
lcd.setCursor(0,0);
lcd.print("delay3= ");
lcd.print(green3);
lcd.print("   ");
if(btupx == 0){
green3++;
}
if(btdownx == 0){
green3--;
}
if(green3 < 1){
green3 = 1;
}
if(btokx == 0){
lcd.clear();
delay(100);
green3 = green3 * 100;
return;
}
 
setdelaygreen3(); 
}
void setdelaygreen4(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
lcd.setCursor(0,0);
lcd.print("delay4= ");
lcd.print(green4);
lcd.print("   ");
if(btupx == 0){
green4++;
}
if(btdownx == 0){
green4--;
}
if(green4 < 1){
green4 = 1;
}
if(btokx == 0){
lcd.clear();
delay(100);
green4 = green4 * 100;
return;
}
 
setdelaygreen4(); 
}
void setstartFlash(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
lcd.setCursor(0,0);
lcd.print("startFlash= ");
lcd.print(startFlash);
lcd.print("   ");
if(btupx == 0){
startFlash++;
}
if(btdownx == 0){
startFlash--;
}
if(startFlash < 0){
startFlash = 23;
}
if(startFlash > 23){
startFlash = 0;
}
if(btokx == 0){
lcd.clear();
delay(100);
return;
}
 
setstartFlash(); 
}
void setendFlash(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
lcd.setCursor(0,0);
lcd.print("endFlash= ");
lcd.print(endFlash);
lcd.print("   ");
if(btupx == 0){
endFlash++;
}
if(btdownx == 0){
endFlash--;
}
if(endFlash < 0){
endFlash = 23;
}
if(endFlash > 23){
endFlash = 0;
}
if(btokx == 0){
lcd.clear();
delay(100);
return;
}
 
setendFlash(); 
}
void runFlash(){
 
displayTime();
delay(1);
 
digitalWrite(ledg1,LOW);
digitalWrite(ledy1,HIGH);
digitalWrite(ledr1,LOW);
digitalWrite(ledg2,LOW);
digitalWrite(ledy2,HIGH);
digitalWrite(ledr2,LOW);
digitalWrite(ledg3,LOW);
digitalWrite(ledy3,HIGH);
digitalWrite(ledr3,LOW);
digitalWrite(ledg4,LOW);
digitalWrite(ledy4,HIGH);
digitalWrite(ledr4,LOW);
delay(100);
digitalWrite(ledg1,LOW);
digitalWrite(ledy1,LOW);
digitalWrite(ledr1,LOW);
digitalWrite(ledg2,LOW);
digitalWrite(ledy2,LOW);
digitalWrite(ledr2,LOW);
digitalWrite(ledg3,LOW);
digitalWrite(ledy3,LOW);
digitalWrite(ledr3,LOW);
digitalWrite(ledg4,LOW);
digitalWrite(ledy4,LOW);
digitalWrite(ledr4,LOW);
delay(100);
if(hour == endFlash){
return;
}
runFlash();
}

No comments:

Post a Comment