Boon Boon
/
traffic_light_simplephase_3tf_gatepj
Simple traffic light phase simulation
main.cpp@0:401494462563, 2018-07-02 (annotated)
- Committer:
- daklowprofile
- Date:
- Mon Jul 02 08:58:34 2018 +0000
- Revision:
- 0:401494462563
simple 3 traffic light phase in 3 junctions. no interrupt. Location: University Malaya Gate PJ
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daklowprofile | 0:401494462563 | 1 | #include "mbed.h" |
daklowprofile | 0:401494462563 | 2 | |
daklowprofile | 0:401494462563 | 3 | #define MAX_LEN 64 |
daklowprofile | 0:401494462563 | 4 | |
daklowprofile | 0:401494462563 | 5 | //T1 |
daklowprofile | 0:401494462563 | 6 | DigitalOut t1red(p5); |
daklowprofile | 0:401494462563 | 7 | DigitalOut t1yellow(p6); |
daklowprofile | 0:401494462563 | 8 | DigitalOut t1green(p7); |
daklowprofile | 0:401494462563 | 9 | //T2 |
daklowprofile | 0:401494462563 | 10 | DigitalOut t2red(p11); |
daklowprofile | 0:401494462563 | 11 | DigitalOut t2yellow(p12); |
daklowprofile | 0:401494462563 | 12 | DigitalOut t2green(p13); |
daklowprofile | 0:401494462563 | 13 | //T3 |
daklowprofile | 0:401494462563 | 14 | DigitalOut t3red(p18); |
daklowprofile | 0:401494462563 | 15 | DigitalOut t3yellow(p19); |
daklowprofile | 0:401494462563 | 16 | DigitalOut t3green(p20); |
daklowprofile | 0:401494462563 | 17 | |
daklowprofile | 0:401494462563 | 18 | //switch |
daklowprofile | 0:401494462563 | 19 | //DigitalIn switcht1(p28); |
daklowprofile | 0:401494462563 | 20 | DigitalIn switcht2(p29); |
daklowprofile | 0:401494462563 | 21 | DigitalIn switcht3(p30); |
daklowprofile | 0:401494462563 | 22 | |
daklowprofile | 0:401494462563 | 23 | |
daklowprofile | 0:401494462563 | 24 | |
daklowprofile | 0:401494462563 | 25 | void t1light(char a){ |
daklowprofile | 0:401494462563 | 26 | if (a == 'R'){ |
daklowprofile | 0:401494462563 | 27 | t1red = 1; |
daklowprofile | 0:401494462563 | 28 | t1yellow = 0; |
daklowprofile | 0:401494462563 | 29 | t1green = 0; |
daklowprofile | 0:401494462563 | 30 | }else if(a == 'Y'){ |
daklowprofile | 0:401494462563 | 31 | t1red = 0; |
daklowprofile | 0:401494462563 | 32 | t1yellow = 1; |
daklowprofile | 0:401494462563 | 33 | t1green = 0; |
daklowprofile | 0:401494462563 | 34 | }else if(a == 'G'){ |
daklowprofile | 0:401494462563 | 35 | t1red = 0; |
daklowprofile | 0:401494462563 | 36 | t1yellow = 0; |
daklowprofile | 0:401494462563 | 37 | t1green = 1; |
daklowprofile | 0:401494462563 | 38 | }else{} |
daklowprofile | 0:401494462563 | 39 | } |
daklowprofile | 0:401494462563 | 40 | |
daklowprofile | 0:401494462563 | 41 | void t2light(char a){ |
daklowprofile | 0:401494462563 | 42 | if (a == 'R'){ |
daklowprofile | 0:401494462563 | 43 | t2red = 1; |
daklowprofile | 0:401494462563 | 44 | t2yellow = 0; |
daklowprofile | 0:401494462563 | 45 | t2green = 0; |
daklowprofile | 0:401494462563 | 46 | }else if(a == 'Y'){ |
daklowprofile | 0:401494462563 | 47 | t2red = 0; |
daklowprofile | 0:401494462563 | 48 | t2yellow = 1; |
daklowprofile | 0:401494462563 | 49 | t2green = 0; |
daklowprofile | 0:401494462563 | 50 | }else if(a == 'G'){ |
daklowprofile | 0:401494462563 | 51 | t2red = 0; |
daklowprofile | 0:401494462563 | 52 | t2yellow = 0; |
daklowprofile | 0:401494462563 | 53 | t2green = 1; |
daklowprofile | 0:401494462563 | 54 | }else{} |
daklowprofile | 0:401494462563 | 55 | } |
daklowprofile | 0:401494462563 | 56 | |
daklowprofile | 0:401494462563 | 57 | void t3light(char a){ |
daklowprofile | 0:401494462563 | 58 | if (a == 'R'){ |
daklowprofile | 0:401494462563 | 59 | t3red = 1; |
daklowprofile | 0:401494462563 | 60 | t3yellow = 0; |
daklowprofile | 0:401494462563 | 61 | t3green = 0; |
daklowprofile | 0:401494462563 | 62 | }else if(a == 'Y'){ |
daklowprofile | 0:401494462563 | 63 | t3red = 0; |
daklowprofile | 0:401494462563 | 64 | t3yellow = 1; |
daklowprofile | 0:401494462563 | 65 | t3green = 0; |
daklowprofile | 0:401494462563 | 66 | }else if(a == 'G'){ |
daklowprofile | 0:401494462563 | 67 | t3red = 0; |
daklowprofile | 0:401494462563 | 68 | t3yellow = 0; |
daklowprofile | 0:401494462563 | 69 | t3green = 1; |
daklowprofile | 0:401494462563 | 70 | }else{} |
daklowprofile | 0:401494462563 | 71 | } |
daklowprofile | 0:401494462563 | 72 | |
daklowprofile | 0:401494462563 | 73 | void traffic_default (char t1, char t2, char t3, int time){ |
daklowprofile | 0:401494462563 | 74 | t1light(t1); |
daklowprofile | 0:401494462563 | 75 | t2light(t2); |
daklowprofile | 0:401494462563 | 76 | t3light(t3); |
daklowprofile | 0:401494462563 | 77 | wait(time); |
daklowprofile | 0:401494462563 | 78 | } |
daklowprofile | 0:401494462563 | 79 | void normal_traffic(){ |
daklowprofile | 0:401494462563 | 80 | traffic_default('G','R','R',47); |
daklowprofile | 0:401494462563 | 81 | traffic_default('Y','R','R',3); |
daklowprofile | 0:401494462563 | 82 | traffic_default('R','G','R',38); |
daklowprofile | 0:401494462563 | 83 | traffic_default('R','Y','R',3); |
daklowprofile | 0:401494462563 | 84 | traffic_default('R','R','G',70); |
daklowprofile | 0:401494462563 | 85 | traffic_default('R','R','Y',3); |
daklowprofile | 0:401494462563 | 86 | } |
daklowprofile | 0:401494462563 | 87 | |
daklowprofile | 0:401494462563 | 88 | int main() { |
daklowprofile | 0:401494462563 | 89 | while(1){ |
daklowprofile | 0:401494462563 | 90 | normal_traffic(); |
daklowprofile | 0:401494462563 | 91 | } |
daklowprofile | 0:401494462563 | 92 | } |