persimpangan 4 dg firebase

mail@pastecode.io avatar
unknown
lua
3 years ago
13 kB
10
Indexable
Never
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_HOST "smarttrafficambulance21-default-rtdb.asia-southeast1.firebasedatabase.app"
#define FIREBASE_AUTH "gGP1uXTAzbmUB6aToRrFodTwOB0r5581xlyuveWL"
#define WIFI_SSID "Gardjito"
#define WIFI_PASSWORD "gardjito2608"

String pathArah = "traffic-lights/2/lamp/kodeArah";
String pathLampu = "traffic-lights/2/lamp/kodeLampu";

int D0 = 16;
int D1 = 5;
int D2 = 4;
int D3 = 0;
int D4 = 2;
int D5 = 14;
int D6 = 12;
int D7 = 13;
int D8 = 15;

int dataPin[] = {D0, D3};
int latchPin[] = {D1, D4};
int clockPin[] = {D2, D5};

unsigned long waktuSekarang = 0;
unsigned long waktuSebelumnya = 0;

int lampStat1;
int lampArah1;

void setup() {
  Serial.begin(9600);
  pinMode(dataPin[0], OUTPUT);
  pinMode(dataPin[1], OUTPUT);
  pinMode(latchPin[0], OUTPUT);
  pinMode(latchPin[1], OUTPUT);
  pinMode(clockPin[0], OUTPUT);
  pinMode(clockPin[1], OUTPUT);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop() {
  //state1_red-red-red-green
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 15000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001); //001 001 - green=mati yellow=mati red=hidup green=mati yellow=mati red=hidup
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B100001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (15000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state1_red-red-red-green");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state2_red-red-red-yellow
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
                                               //gyrgyr
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B010001);
                                               //gyrgyr
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state2_red-red-red-yellow");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }
  //state3_yellow-red-red-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001010);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state3_yellow-red-red-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state4_green-red-red-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 15000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001100);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (15000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state4_green-red-red-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state5_yellow-red-red-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001010);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state5_yellow-red-red-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state6_red-yellow-red-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B010001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state6_red-yellow-red-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state7_red-green-red-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 15000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B100001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (15000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state7_red-green-red-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state8_red-yellow-red-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B010001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state8_red-yellow-red-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state9_red-red-yellow-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001010);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state9_red-red-yellow-redd");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state10_red-red-green-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 15000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001100);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (15000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state10_red-red-green-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state11_red-red-yellow-red
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001010);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state11_red-red-yellow-red");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }

  //state12_red-red-red-yellow
  waktuSekarang = millis();
  waktuSebelumnya = millis();
  for (waktuSekarang; millis() - waktuSebelumnya <= 3000; waktuSekarang = millis()) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B010001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    getStatus();
    forceGreen();
    int a = (3000 - (millis() - waktuSebelumnya))/1000;
    Serial.println("state12_red-red-red-yellow");
    Serial.print("Countdown : ");
    Serial.print(a);
    Serial.println("s");
  }
}

void getStatus() {
  lampArah1 = Firebase.getInt(pathArah);
  lampStat1 = Firebase.getInt(pathLampu);
  Serial.print("\nlampStat1 : ");
  Serial.println(lampStat1);
  Serial.print("lampArah1 : ");
  Serial.println(lampArah1);
}

void forceGreen() {
  if(lampStat1 == 1 && lampArah1 == 0) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001010);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(3000);
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001100);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(30000);
    Firebase.setInt(pathArah, 4);
    Firebase.setInt(pathLampu, 4);
  }

  else if(lampStat1 == 1 && lampArah1 == 1) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B010001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(3000);
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B100001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(30000);
    Firebase.setInt(pathArah, 4);
    Firebase.setInt(pathLampu, 4);
  }

  else if(lampStat1 == 1 && lampArah1 == 2) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001010);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(3000);
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B001100);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(30000);
    Firebase.setInt(pathArah, 4);
    Firebase.setInt(pathLampu, 4);
  }

  else if(lampStat1 == 1 && lampArah1 == 3) {
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B010001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(3000);
    digitalWrite(latchPin[0], LOW);
    digitalWrite(latchPin[1], LOW);
    shiftOut(dataPin[0], clockPin[0], MSBFIRST, B001001);
    shiftOut(dataPin[1], clockPin[1], MSBFIRST, B100001);
    digitalWrite(latchPin[0], HIGH);
    digitalWrite(latchPin[1], HIGH);
    delay(30000);
    Firebase.setInt(pathArah, 4);
    Firebase.setInt(pathLampu, 4);
  }
}