Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:ff5c694af047, 2019-10-31 (annotated)
- Committer:
- etoms
- Date:
- Thu Oct 31 15:32:14 2019 +0000
- Revision:
- 0:ff5c694af047
- Child:
- 1:848c19e20191
Traffic Light
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| etoms | 0:ff5c694af047 | 1 | #include "mbed.h" |
| etoms | 0:ff5c694af047 | 2 | |
| etoms | 0:ff5c694af047 | 3 | |
| etoms | 0:ff5c694af047 | 4 | #define MAX_LEN 64 |
| etoms | 0:ff5c694af047 | 5 | //tester |
| etoms | 0:ff5c694af047 | 6 | //DigitalOut led1(LED1); |
| etoms | 0:ff5c694af047 | 7 | //InterruptIn button(p28); |
| etoms | 0:ff5c694af047 | 8 | |
| etoms | 0:ff5c694af047 | 9 | //Timer class |
| etoms | 0:ff5c694af047 | 10 | Timer timer_tf; |
| etoms | 0:ff5c694af047 | 11 | |
| etoms | 0:ff5c694af047 | 12 | //print for debug |
| etoms | 0:ff5c694af047 | 13 | Serial pc(USBTX, USBRX); |
| etoms | 0:ff5c694af047 | 14 | |
| etoms | 0:ff5c694af047 | 15 | |
| etoms | 0:ff5c694af047 | 16 | DigitalOut led1(LED1); |
| etoms | 0:ff5c694af047 | 17 | DigitalOut led2(LED2); |
| etoms | 0:ff5c694af047 | 18 | DigitalOut led3(LED3); |
| etoms | 0:ff5c694af047 | 19 | |
| etoms | 0:ff5c694af047 | 20 | //interrupt only on p13,14,27,28 |
| etoms | 0:ff5c694af047 | 21 | InterruptIn button1(p13); |
| etoms | 0:ff5c694af047 | 22 | InterruptIn button2(p14); |
| etoms | 0:ff5c694af047 | 23 | InterruptIn button3(p27); |
| etoms | 0:ff5c694af047 | 24 | InterruptIn button4(p28); |
| etoms | 0:ff5c694af047 | 25 | |
| etoms | 0:ff5c694af047 | 26 | //Traffic light 1+2 |
| etoms | 0:ff5c694af047 | 27 | DigitalOut t1red(p5); |
| etoms | 0:ff5c694af047 | 28 | DigitalOut t1yellow(p6); |
| etoms | 0:ff5c694af047 | 29 | DigitalOut t1green(p7); |
| etoms | 0:ff5c694af047 | 30 | //Traffic lights 2+4 |
| etoms | 0:ff5c694af047 | 31 | DigitalOut t2red(p11); |
| etoms | 0:ff5c694af047 | 32 | DigitalOut t2yellow(p12); |
| etoms | 0:ff5c694af047 | 33 | DigitalOut t2green(p30); |
| etoms | 0:ff5c694af047 | 34 | |
| etoms | 0:ff5c694af047 | 35 | |
| etoms | 0:ff5c694af047 | 36 | //switch |
| etoms | 0:ff5c694af047 | 37 | //DigitalIn button1(p26); |
| etoms | 0:ff5c694af047 | 38 | //DigitalIn button2(p27); |
| etoms | 0:ff5c694af047 | 39 | //DigitalIn button3(p28); |
| etoms | 0:ff5c694af047 | 40 | |
| etoms | 0:ff5c694af047 | 41 | |
| etoms | 0:ff5c694af047 | 42 | |
| etoms | 0:ff5c694af047 | 43 | |
| etoms | 0:ff5c694af047 | 44 | void t1light(char a){ |
| etoms | 0:ff5c694af047 | 45 | t1red = 0; |
| etoms | 0:ff5c694af047 | 46 | t1yellow = 0; |
| etoms | 0:ff5c694af047 | 47 | t1green = 0; |
| etoms | 0:ff5c694af047 | 48 | if (a == 'R'){ |
| etoms | 0:ff5c694af047 | 49 | t1red = 1; |
| etoms | 0:ff5c694af047 | 50 | t1yellow = 0; |
| etoms | 0:ff5c694af047 | 51 | t1green = 0; |
| etoms | 0:ff5c694af047 | 52 | }else if(a == 'F'){ |
| etoms | 0:ff5c694af047 | 53 | t1red = 1; |
| etoms | 0:ff5c694af047 | 54 | t1yellow = 1; |
| etoms | 0:ff5c694af047 | 55 | t1green = 0; |
| etoms | 0:ff5c694af047 | 56 | }else if(a == 'Y'){ |
| etoms | 0:ff5c694af047 | 57 | t1red = 0; |
| etoms | 0:ff5c694af047 | 58 | t1yellow = 1; |
| etoms | 0:ff5c694af047 | 59 | t1green = 0; |
| etoms | 0:ff5c694af047 | 60 | }else if(a == 'G'){ |
| etoms | 0:ff5c694af047 | 61 | t1red = 0; |
| etoms | 0:ff5c694af047 | 62 | t1yellow = 0; |
| etoms | 0:ff5c694af047 | 63 | t1green = 1; |
| etoms | 0:ff5c694af047 | 64 | }else{ |
| etoms | 0:ff5c694af047 | 65 | t1red = 0; |
| etoms | 0:ff5c694af047 | 66 | t1yellow = 0; |
| etoms | 0:ff5c694af047 | 67 | t1green = 0; |
| etoms | 0:ff5c694af047 | 68 | } |
| etoms | 0:ff5c694af047 | 69 | } |
| etoms | 0:ff5c694af047 | 70 | |
| etoms | 0:ff5c694af047 | 71 | void t2light(char a){ |
| etoms | 0:ff5c694af047 | 72 | t2red = 0; |
| etoms | 0:ff5c694af047 | 73 | t2yellow = 0; |
| etoms | 0:ff5c694af047 | 74 | t2green = 0; |
| etoms | 0:ff5c694af047 | 75 | if (a == 'R'){ |
| etoms | 0:ff5c694af047 | 76 | t2red = 1; |
| etoms | 0:ff5c694af047 | 77 | t2yellow = 0; |
| etoms | 0:ff5c694af047 | 78 | t2green = 0; |
| etoms | 0:ff5c694af047 | 79 | }else if(a == 'F'){ |
| etoms | 0:ff5c694af047 | 80 | t2red = 1; |
| etoms | 0:ff5c694af047 | 81 | t2yellow = 1; |
| etoms | 0:ff5c694af047 | 82 | t2green = 0; |
| etoms | 0:ff5c694af047 | 83 | }else if(a == 'Y'){ |
| etoms | 0:ff5c694af047 | 84 | t2red = 0; |
| etoms | 0:ff5c694af047 | 85 | t2yellow = 1; |
| etoms | 0:ff5c694af047 | 86 | t2green = 0; |
| etoms | 0:ff5c694af047 | 87 | }else if(a == 'G'){ |
| etoms | 0:ff5c694af047 | 88 | t2red = 0; |
| etoms | 0:ff5c694af047 | 89 | t2yellow = 0; |
| etoms | 0:ff5c694af047 | 90 | t2green = 1; |
| etoms | 0:ff5c694af047 | 91 | }else{ |
| etoms | 0:ff5c694af047 | 92 | t2red = 0; |
| etoms | 0:ff5c694af047 | 93 | t2yellow = 0; |
| etoms | 0:ff5c694af047 | 94 | t2green = 0; |
| etoms | 0:ff5c694af047 | 95 | } |
| etoms | 0:ff5c694af047 | 96 | } |
| etoms | 0:ff5c694af047 | 97 | |
| etoms | 0:ff5c694af047 | 98 | |
| etoms | 0:ff5c694af047 | 99 | |
| etoms | 0:ff5c694af047 | 100 | void traffic (char t1, char t2, int time_end){ |
| etoms | 0:ff5c694af047 | 101 | if(timer_tf.read() <= time_end){ |
| etoms | 0:ff5c694af047 | 102 | t1light(t1); |
| etoms | 0:ff5c694af047 | 103 | t2light(t2); |
| etoms | 0:ff5c694af047 | 104 | timer_tf.reset(); |
| etoms | 0:ff5c694af047 | 105 | } |
| etoms | 0:ff5c694af047 | 106 | //wait(time); |
| etoms | 0:ff5c694af047 | 107 | } |
| etoms | 0:ff5c694af047 | 108 | |
| etoms | 0:ff5c694af047 | 109 | void traffic_timer (char t1, char t2){ |
| etoms | 0:ff5c694af047 | 110 | t1light(t1); |
| etoms | 0:ff5c694af047 | 111 | t2light(t2); |
| etoms | 0:ff5c694af047 | 112 | } |
| etoms | 0:ff5c694af047 | 113 | |
| etoms | 0:ff5c694af047 | 114 | void traffic_default (char t1, char t2, int time){ |
| etoms | 0:ff5c694af047 | 115 | t1light(t1); |
| etoms | 0:ff5c694af047 | 116 | t2light(t2); |
| etoms | 0:ff5c694af047 | 117 | wait(time); |
| etoms | 0:ff5c694af047 | 118 | } |
| etoms | 0:ff5c694af047 | 119 | |
| etoms | 0:ff5c694af047 | 120 | |
| etoms | 0:ff5c694af047 | 121 | |
| etoms | 0:ff5c694af047 | 122 | void normal_traffic(){ |
| etoms | 0:ff5c694af047 | 123 | ///* |
| etoms | 0:ff5c694af047 | 124 | if(timer_tf.read() >= 0 && timer_tf.read() <=10){ |
| etoms | 0:ff5c694af047 | 125 | traffic_timer('G','R'); |
| etoms | 0:ff5c694af047 | 126 | } |
| etoms | 0:ff5c694af047 | 127 | if(timer_tf.read() >= 11 && timer_tf.read() <= 16){ |
| etoms | 0:ff5c694af047 | 128 | traffic_timer('Y','R'); |
| etoms | 0:ff5c694af047 | 129 | } |
| etoms | 0:ff5c694af047 | 130 | if(timer_tf.read() >= 17 && timer_tf.read() <= 23){ |
| etoms | 0:ff5c694af047 | 131 | traffic_timer('R','G'); |
| etoms | 0:ff5c694af047 | 132 | } |
| etoms | 0:ff5c694af047 | 133 | if(timer_tf.read() >= 24 && timer_tf.read() <= 30){ |
| etoms | 0:ff5c694af047 | 134 | traffic_timer('R','Y'); |
| etoms | 0:ff5c694af047 | 135 | } |
| etoms | 0:ff5c694af047 | 136 | if(timer_tf.read() >= 31 && timer_tf.read() <= 37){ |
| etoms | 0:ff5c694af047 | 137 | traffic_timer('F','Y'); |
| etoms | 0:ff5c694af047 | 138 | } |
| etoms | 0:ff5c694af047 | 139 | |
| etoms | 0:ff5c694af047 | 140 | /* |
| etoms | 0:ff5c694af047 | 141 | if(timer_tf.read() >= 0 && timer_tf.read() <= 5){ |
| etoms | 0:ff5c694af047 | 142 | traffic_timer('G','R'); |
| etoms | 0:ff5c694af047 | 143 | } |
| etoms | 0:ff5c694af047 | 144 | if(timer_tf.read() >= 6 && timer_tf.read() <= 10){ |
| etoms | 0:ff5c694af047 | 145 | traffic_timer('Y','R'); |
| etoms | 0:ff5c694af047 | 146 | } |
| etoms | 0:ff5c694af047 | 147 | if(timer_tf.read() >= 11 && timer_tf.read() <= 15){ |
| etoms | 0:ff5c694af047 | 148 | traffic_timer('R','G'); |
| etoms | 0:ff5c694af047 | 149 | } |
| etoms | 0:ff5c694af047 | 150 | if(timer_tf.read() >= 16 && timer_tf.read() <= 20){ |
| etoms | 0:ff5c694af047 | 151 | traffic_timer('R','Y',; |
| etoms | 0:ff5c694af047 | 152 | } |
| etoms | 0:ff5c694af047 | 153 | |
| etoms | 0:ff5c694af047 | 154 | */ |
| etoms | 0:ff5c694af047 | 155 | if(timer_tf.read() >= 170){ |
| etoms | 0:ff5c694af047 | 156 | timer_tf.reset(); |
| etoms | 0:ff5c694af047 | 157 | } |
| etoms | 0:ff5c694af047 | 158 | |
| etoms | 0:ff5c694af047 | 159 | //traffic_default('G','R',47); |
| etoms | 0:ff5c694af047 | 160 | //traffic_default('Y','R',3); |
| etoms | 0:ff5c694af047 | 161 | //traffic_default('R','G',38); |
| etoms | 0:ff5c694af047 | 162 | //traffic_default('R','Y',3); |
| etoms | 0:ff5c694af047 | 163 | //traffic_default('R','R',70); |
| etoms | 0:ff5c694af047 | 164 | //traffic_default('R','R',3); |
| etoms | 0:ff5c694af047 | 165 | } |
| etoms | 0:ff5c694af047 | 166 | |
| etoms | 0:ff5c694af047 | 167 | void flip1() { |
| etoms | 0:ff5c694af047 | 168 | led1 = !led1; |
| etoms | 0:ff5c694af047 | 169 | timer_tf.stop(); |
| etoms | 0:ff5c694af047 | 170 | //pc.printf("%f\n",timer_tf.read()); |
| etoms | 0:ff5c694af047 | 171 | //wait(10); |
| etoms | 0:ff5c694af047 | 172 | //pc.printf("%f\n",timer_tf.read()); |
| etoms | 0:ff5c694af047 | 173 | //imer_tf.start(); |
| etoms | 0:ff5c694af047 | 174 | //wait(5); |
| etoms | 0:ff5c694af047 | 175 | //pc.printf("%f\n",timer_tf.read()); |
| etoms | 0:ff5c694af047 | 176 | /* |
| etoms | 0:ff5c694af047 | 177 | if(timer_tf.read() <= 47){ |
| etoms | 0:ff5c694af047 | 178 | wait(15); |
| etoms | 0:ff5c694af047 | 179 | } |
| etoms | 0:ff5c694af047 | 180 | */ |
| etoms | 0:ff5c694af047 | 181 | if(timer_tf.read() >= 0/*48*/ && timer_tf.read() <= 51){ |
| etoms | 0:ff5c694af047 | 182 | wait(15); |
| etoms | 0:ff5c694af047 | 183 | } |
| etoms | 0:ff5c694af047 | 184 | if(timer_tf.read() >= 52 && timer_tf.read() <= 90){ |
| etoms | 0:ff5c694af047 | 185 | //traffic_timer('R','G'); |
| etoms | 0:ff5c694af047 | 186 | traffic_default('R','Y',3); |
| etoms | 0:ff5c694af047 | 187 | traffic_default('G','R',15); |
| etoms | 0:ff5c694af047 | 188 | traffic_default('Y','R',3); |
| etoms | 0:ff5c694af047 | 189 | } |
| etoms | 0:ff5c694af047 | 190 | if(timer_tf.read() >= 91 && timer_tf.read() <= 94){ |
| etoms | 0:ff5c694af047 | 191 | //traffic_timer('R','Y','R'); |
| etoms | 0:ff5c694af047 | 192 | wait(3); |
| etoms | 0:ff5c694af047 | 193 | traffic_default('G','R',15); |
| etoms | 0:ff5c694af047 | 194 | traffic_default('Y','R',3); |
| etoms | 0:ff5c694af047 | 195 | } |
| etoms | 0:ff5c694af047 | 196 | |
| etoms | 0:ff5c694af047 | 197 | timer_tf.start(); |
| etoms | 0:ff5c694af047 | 198 | led1 = !led1; |
| etoms | 0:ff5c694af047 | 199 | } |
| etoms | 0:ff5c694af047 | 200 | |
| etoms | 0:ff5c694af047 | 201 | void flip2() { |
| etoms | 0:ff5c694af047 | 202 | led2 = !led2; |
| etoms | 0:ff5c694af047 | 203 | timer_tf.stop(); |
| etoms | 0:ff5c694af047 | 204 | if(timer_tf.read() >= 0 && timer_tf.read() <= 47){ |
| etoms | 0:ff5c694af047 | 205 | //traffic_timer('G','R','R'); |
| etoms | 0:ff5c694af047 | 206 | traffic_default('Y','R',3); |
| etoms | 0:ff5c694af047 | 207 | traffic_default('R','G',15); |
| etoms | 0:ff5c694af047 | 208 | traffic_default('R','Y',3); |
| etoms | 0:ff5c694af047 | 209 | } |
| etoms | 0:ff5c694af047 | 210 | if(timer_tf.read() >= 48 && timer_tf.read() <= 51){ |
| etoms | 0:ff5c694af047 | 211 | //traffic_timer('Y','R','R'); |
| etoms | 0:ff5c694af047 | 212 | wait(3); |
| etoms | 0:ff5c694af047 | 213 | traffic_default('R','G',15); |
| etoms | 0:ff5c694af047 | 214 | traffic_default('R','Y',3); |
| etoms | 0:ff5c694af047 | 215 | } |
| etoms | 0:ff5c694af047 | 216 | /* |
| etoms | 0:ff5c694af047 | 217 | if(timer_tf.read() >= 52 && timer_tf.read() <= 90){ |
| etoms | 0:ff5c694af047 | 218 | //traffic_timer('R','G','R'); |
| etoms | 0:ff5c694af047 | 219 | wait(15); |
| etoms | 0:ff5c694af047 | 220 | } |
| etoms | 0:ff5c694af047 | 221 | */ |
| etoms | 0:ff5c694af047 | 222 | if(timer_tf.read() >= 52/*91*/ && timer_tf.read() <= 94){ |
| etoms | 0:ff5c694af047 | 223 | //traffic_timer('R','Y'); |
| etoms | 0:ff5c694af047 | 224 | wait(15); |
| etoms | 0:ff5c694af047 | 225 | } |
| etoms | 0:ff5c694af047 | 226 | timer_tf.start(); |
| etoms | 0:ff5c694af047 | 227 | led2 = !led2; |
| etoms | 0:ff5c694af047 | 228 | } |
| etoms | 0:ff5c694af047 | 229 | |
| etoms | 0:ff5c694af047 | 230 | |
| etoms | 0:ff5c694af047 | 231 | int main() { |
| etoms | 0:ff5c694af047 | 232 | |
| etoms | 0:ff5c694af047 | 233 | timer_tf.start(); |
| etoms | 0:ff5c694af047 | 234 | /* |
| etoms | 0:ff5c694af047 | 235 | if(button1 == 1){ |
| etoms | 0:ff5c694af047 | 236 | flip1(); |
| etoms | 0:ff5c694af047 | 237 | }else if(button2 == 1){ |
| etoms | 0:ff5c694af047 | 238 | flip2(); |
| etoms | 0:ff5c694af047 | 239 | }else if(button3 == 1){ |
| etoms | 0:ff5c694af047 | 240 | flip3(); |
| etoms | 0:ff5c694af047 | 241 | }else{} |
| etoms | 0:ff5c694af047 | 242 | */ |
| etoms | 0:ff5c694af047 | 243 | button1.mode(PullUp); |
| etoms | 0:ff5c694af047 | 244 | button1.fall(&flip1); |
| etoms | 0:ff5c694af047 | 245 | button2.mode(PullUp); |
| etoms | 0:ff5c694af047 | 246 | button2.fall(&flip2); |
| etoms | 0:ff5c694af047 | 247 | button3.mode(PullUp); |
| etoms | 0:ff5c694af047 | 248 | button3.fall(&flip1); |
| etoms | 0:ff5c694af047 | 249 | button4.mode(PullUp); |
| etoms | 0:ff5c694af047 | 250 | button4.fall(&flip2); |
| etoms | 0:ff5c694af047 | 251 | while(1){ |
| etoms | 0:ff5c694af047 | 252 | normal_traffic(); |
| etoms | 0:ff5c694af047 | 253 | |
| etoms | 0:ff5c694af047 | 254 | } |
| etoms | 0:ff5c694af047 | 255 | } |