Traffic control with push botton

โปรแกรมควบคุมสัญญาณไฟจราจร พร้อมปุ่มกดคนข้ามถนน

แผนผังวงจร Circute diagram

Video Traffic Control with Push botton

//กำหนดเส้นทางออกของสัญญาณจราจร

#define led_red 11

#define led_yellow 12

#define led_green 13

//กำหนดตัวแปรตัวนับ digit

#define a 2

#define b 3

#define c 4

#define d 5

#define e 6

#define f 7

#define g 8

#define push_btt 9

#define push_green 10

int ledState = LOW;

unsigned long previousMillis = 0;

const long interval = 1000;

int push_btt_state = 0;

int push_btt_counter=0;

int push_btt_laststate=0;

void setup() //โปรดกดติดตาม กดแจ้งเตือน Facebook @เรียนคอมบาทเดียว, Youtube channel Corecomcenter th

{

pinMode(led_red, OUTPUT);

pinMode(led_yellow, OUTPUT);

pinMode(led_green, OUTPUT);

pinMode(a, OUTPUT);

pinMode(b, OUTPUT);

pinMode(c, OUTPUT);

pinMode(d, OUTPUT);

pinMode(e, OUTPUT);

pinMode(f, OUTPUT);

pinMode(g, OUTPUT);

pinMode(push_btt, INPUT_PULLUP);

pinMode(push_green, OUTPUT);

Serial.begin(9600);


}


void blink10(int ledState)

{

unsigned long currentMillis = millis();


if (currentMillis - previousMillis >= interval)

{

previousMillis = currentMillis;

if (ledState == LOW)

{

ledState = HIGH;

}

else

{

ledState = LOW;

}

digitalWrite(push_green, ledState);

}

}


void loop()

{

if(digitalRead(push_btt)==HIGH)

{

PushBotton(push_btt_counter);

}

if(digitalRead(push_btt)==LOW);

{

PushBotton(push_btt_counter);

}

}


void traffic_run(int icount)

{

int i;int istate; int green_sec=9000; int yellow_sec=4000; int red_sec=9000;

for(i=1; i<=4; i++)//coding by corecomcenter th

{

if(digitalRead(push_btt)==LOW)

{

switch(i)

{//coding by corecomcenter th

case 1:

istate = i;

digitalWrite(led_green, HIGH);

digit_count(green_sec);

digitalWrite(led_green, LOW);

break;//coding by corecomcenter th

case 2:

istate = i;

digitalWrite(led_yellow, HIGH);

digit_count(yellow_sec);

digitalWrite(led_yellow, LOW);

break;

case 3://coding by corecomcenter th

istate = i;

digitalWrite(led_red, HIGH);

digit_count(red_sec);

digitalWrite(led_red, LOW);

break;

}

}else

{

switch(istate)

{

case 1:

for(int pushGreen=1; pushGreen<=10; pushGreen++)

{

digitalWrite(led_green, HIGH);

delay(500);

digitalWrite(led_green, LOW);

delay(500);

}

for(int pushYellow=1; pushYellow<=4; pushYellow++)

{

digitalWrite(led_yellow, HIGH);

delay(500);

digitalWrite(led_yellow, LOW);

delay(500);

}

digitalWrite(led_red, HIGH);//coding by corecomcenter th

delay(5000);//หยุด 3 วินาที เพื่อรอไฟเขียวฝั่งคนข้าม

blink10(ledState);

delay(20000); //ให้คนข้าม 20 วินาที

blink10(HIGH); //ดับไฟคนข้าม

delay(3000); // รอ 3 วินาที

digitalWrite(led_red, LOW);//ดับไฟแดง

traffic_run(3); //สั่งเปิดไฟเขียว

break;

case 2:

for(int pushYellow=1; pushYellow<=5; pushYellow++)

{

digitalWrite(led_yellow, HIGH);

delay(500);

digitalWrite(led_yellow, LOW);

delay(500);

}

digitalWrite(led_red, HIGH);

delay(5000); //หยุดรอ 5 วินาที

blink10(LOW); //สั่งเปิดไฟคนข้าม

delay(20000); //รอให้คนข้าม 20 วินาที//coding by corecomcenter th

blink10(HIGH); //ดับไฟคนข้ามถนน

delay(3000); //รอ 3 วินาที

digitalWrite(led_red, LOW);//ดับไฟแดง

traffic_run(3); //สั่งเปิดไฟเขียว

break;

case 3:

for(int pushRed=1; pushRed<=5; pushRed++)

{

digitalWrite(led_red, HIGH);

delay(500);

digitalWrite(led_red, LOW);

delay(500);

}

digitalWrite(led_red, HIGH);

delay(5000); //หยุดรอ 5 วินาที

blink10(LOW); //สั่งเปิดไฟคนข้าม//coding by corecomcenter th

delay(20000); //รอให้คนข้าม 20 วินาที

blink10(HIGH); //ดับไฟคนข้ามถนน

delay(3000); //รอ 3 วินาที

digitalWrite(led_red, LOW);//ดับไฟแดง

traffic_run(3); //สั่งเปิดไฟเขียว

break;//coding by corecomcenter th

}

}

}

}


void digit_count(int delay_led)

{

if(delay_led)

{

int delay_led_sec = delay_led/1000;

for(delay_led_sec; delay_led_sec>=0; delay_led_sec--)

{

if(digitalRead(push_btt)==LOW)

{

switch(delay_led_sec)

{

case 9:

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, HIGH);

digitalWrite(e, LOW);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

delay(1000);

break;

case 8:

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

delay(1000);

break;

case 7:

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

delay(1000);

break;

case 6:

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, HIGH);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

delay(1000);

break;

case 5:

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, HIGH);

digitalWrite(d, HIGH);

digitalWrite(e, LOW);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

delay(1000);

break;

case 4:

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

delay(1000);

break;

case 3:

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, HIGH);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, HIGH);

delay(1000);

break;

case 2:

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, LOW);

digitalWrite(g, HIGH);

delay(1000);

break;

case 1:

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

delay(1000);

break;

case 0://coding by corecomcenter th

digitalWrite(a, HIGH);

digitalWrite(b, HIGH);

digitalWrite(c, HIGH);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, LOW);

delay(1000);

break;

}

}else

{

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, HIGH);

}

}

}

}


void PushBotton (int PushValue)

{

if(digitalRead(push_btt)== LOW)

{

traffic_run(3);//coding by corecomcenter th

}

else

{

//blink10(ledState);

}

}

// coding by corecomcenter th