// Create Traffic Light control Intersection.
/* Setup and Designs systems LED 4 phase.
const int R[] = {};
const int Y[] = {};
const int G[] = {};
*/
// กำหนด Pins สำหรับสัญญาณไฟจราจรแต่ละแยก
// phase1
const int RED_1 = 2 ;
const int YELLOW_1 = 3 ;
const int GREEN_1 = 4 ;
// phase2
const int RED_2 = 5 ;
const int YELLOW_2 = 6 ;
const int GREEN_2 = 7 ;
// phase3
const int RED_3 = 8 ;
const int YELLOW_3 = 9 ;
const int GREEN_3 = 10 ;
// phase4
const int RED_4 = 11 ;
const int YELLOW_4 = 12 ;
const int GREEN_4 = 13 ;
struct TrafficPhase {
int greenTime;
int yellowTime;
int redTime;
};
int timer = millis();
int MS = timer;
int SEC = MS*1000 ;
// สร้าง array เพื่อเก็บข้อมูลระยะเวลาของแต่ละ Phase
TrafficPhase phases[4] = {
{120000, 5000, 280000}, // Phase 1 (G=120 , Y=5 , R=280)
{124000, 5000, 276000}, // Phase 2 (G=124 , Y=5 , R=276)
{65000, 5000, 335000}, // Phase 3 (G=65 , Y=5 , R=335)
{92000, 5000, 308000} // Phase 4 (G=92 , Y=5 , R=308)
};
// ฟังก์ชันสำหรับเปิดไฟ
void turnOn(int pin) {
digitalWrite(pin, HIGH);
}
// ฟังก์ชันสำหรับปิดไฟ
void turnOff(int pin) {
digitalWrite(pin, LOW);
}
void setup() {
Serial.begin(9600);
// กำหนดให้ Pins เป็น Output
//phase1
pinMode(RED_1, OUTPUT);
pinMode(YELLOW_1, OUTPUT);
pinMode(GREEN_1, OUTPUT);
//phase2
pinMode(RED_2, OUTPUT);
pinMode(YELLOW_2, OUTPUT);
pinMode(GREEN_2, OUTPUT);
//phase3
pinMode(RED_3, OUTPUT);
pinMode(YELLOW_3, OUTPUT);
pinMode(GREEN_3, OUTPUT);
//phase4
pinMode(RED_4, OUTPUT);
pinMode(YELLOW_4, OUTPUT);
pinMode(GREEN_4, OUTPUT);
turnOff(RED_1);
turnOff(YELLOW_1);
turnOff(GREEN_1);
turnOff(RED_2);
turnOff(YELLOW_2);
turnOff(GREEN_2);
turnOff(RED_3);
turnOff(YELLOW_3);
turnOff(GREEN_3);
turnOff(RED_4);
turnOff(YELLOW_4);
turnOff(GREEN_4);
}
void loop() {}