simple 3 traffic light with interrupt (to change specific traffic light to green)

Dependencies:   mbed

Committer:
daklowprofile
Date:
Wed Jul 04 00:01:15 2018 +0000
Revision:
1:7bad12e1bbbc
Parent:
0:9a9e02000311
fixed timer for traffic light 3 interrupt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daklowprofile 0:9a9e02000311 1 #include "mbed.h"
daklowprofile 0:9a9e02000311 2
daklowprofile 0:9a9e02000311 3
daklowprofile 0:9a9e02000311 4 #define MAX_LEN 64
daklowprofile 0:9a9e02000311 5 //tester
daklowprofile 0:9a9e02000311 6 //DigitalOut led1(LED1);
daklowprofile 0:9a9e02000311 7 //InterruptIn button(p28);
daklowprofile 0:9a9e02000311 8
daklowprofile 0:9a9e02000311 9 //Timer class
daklowprofile 0:9a9e02000311 10 Timer timer_tf;
daklowprofile 0:9a9e02000311 11
daklowprofile 0:9a9e02000311 12 //print for debug
daklowprofile 0:9a9e02000311 13 Serial pc(USBTX, USBRX);
daklowprofile 0:9a9e02000311 14
daklowprofile 0:9a9e02000311 15
daklowprofile 0:9a9e02000311 16 DigitalOut led1(LED1);
daklowprofile 0:9a9e02000311 17 DigitalOut led2(LED2);
daklowprofile 0:9a9e02000311 18 DigitalOut led3(LED3);
daklowprofile 0:9a9e02000311 19
daklowprofile 0:9a9e02000311 20 //interrupt indicator only on p13,14,27,28
daklowprofile 0:9a9e02000311 21 InterruptIn button1(p14);
daklowprofile 0:9a9e02000311 22 InterruptIn button2(p27);
daklowprofile 0:9a9e02000311 23 InterruptIn button3(p28);
daklowprofile 0:9a9e02000311 24
daklowprofile 0:9a9e02000311 25 //T1
daklowprofile 0:9a9e02000311 26 DigitalOut t1red(p5);
daklowprofile 0:9a9e02000311 27 DigitalOut t1yellow(p6);
daklowprofile 0:9a9e02000311 28 DigitalOut t1green(p7);
daklowprofile 0:9a9e02000311 29 //T2
daklowprofile 0:9a9e02000311 30 DigitalOut t2red(p11);
daklowprofile 0:9a9e02000311 31 DigitalOut t2yellow(p12);
daklowprofile 0:9a9e02000311 32 DigitalOut t2green(p13);
daklowprofile 0:9a9e02000311 33 //T3
daklowprofile 0:9a9e02000311 34 DigitalOut t3red(p18);
daklowprofile 0:9a9e02000311 35 DigitalOut t3yellow(p19);
daklowprofile 0:9a9e02000311 36 DigitalOut t3green(p20);
daklowprofile 0:9a9e02000311 37
daklowprofile 0:9a9e02000311 38 //switch
daklowprofile 0:9a9e02000311 39 //DigitalIn button1(p26);
daklowprofile 0:9a9e02000311 40 //DigitalIn button2(p27);
daklowprofile 0:9a9e02000311 41 //DigitalIn button3(p28);
daklowprofile 0:9a9e02000311 42
daklowprofile 0:9a9e02000311 43
daklowprofile 0:9a9e02000311 44
daklowprofile 0:9a9e02000311 45
daklowprofile 0:9a9e02000311 46 void t1light(char a){
daklowprofile 0:9a9e02000311 47 t1red = 0;
daklowprofile 0:9a9e02000311 48 t1yellow = 0;
daklowprofile 0:9a9e02000311 49 t1green = 0;
daklowprofile 0:9a9e02000311 50 if (a == 'R'){
daklowprofile 0:9a9e02000311 51 t1red = 1;
daklowprofile 0:9a9e02000311 52 t1yellow = 0;
daklowprofile 0:9a9e02000311 53 t1green = 0;
daklowprofile 0:9a9e02000311 54 }else if(a == 'Y'){
daklowprofile 0:9a9e02000311 55 t1red = 0;
daklowprofile 0:9a9e02000311 56 t1yellow = 1;
daklowprofile 0:9a9e02000311 57 t1green = 0;
daklowprofile 0:9a9e02000311 58 }else if(a == 'G'){
daklowprofile 0:9a9e02000311 59 t1red = 0;
daklowprofile 0:9a9e02000311 60 t1yellow = 0;
daklowprofile 0:9a9e02000311 61 t1green = 1;
daklowprofile 0:9a9e02000311 62 }else{
daklowprofile 0:9a9e02000311 63 t1red = 0;
daklowprofile 0:9a9e02000311 64 t1yellow = 0;
daklowprofile 0:9a9e02000311 65 t1green = 0;
daklowprofile 0:9a9e02000311 66 }
daklowprofile 0:9a9e02000311 67 }
daklowprofile 0:9a9e02000311 68
daklowprofile 0:9a9e02000311 69 void t2light(char a){
daklowprofile 0:9a9e02000311 70 t2red = 0;
daklowprofile 0:9a9e02000311 71 t2yellow = 0;
daklowprofile 0:9a9e02000311 72 t2green = 0;
daklowprofile 0:9a9e02000311 73 if (a == 'R'){
daklowprofile 0:9a9e02000311 74 t2red = 1;
daklowprofile 0:9a9e02000311 75 t2yellow = 0;
daklowprofile 0:9a9e02000311 76 t2green = 0;
daklowprofile 0:9a9e02000311 77 }else if(a == 'Y'){
daklowprofile 0:9a9e02000311 78 t2red = 0;
daklowprofile 0:9a9e02000311 79 t2yellow = 1;
daklowprofile 0:9a9e02000311 80 t2green = 0;
daklowprofile 0:9a9e02000311 81 }else if(a == 'G'){
daklowprofile 0:9a9e02000311 82 t2red = 0;
daklowprofile 0:9a9e02000311 83 t2yellow = 0;
daklowprofile 0:9a9e02000311 84 t2green = 1;
daklowprofile 0:9a9e02000311 85 }else{
daklowprofile 0:9a9e02000311 86 t2red = 0;
daklowprofile 0:9a9e02000311 87 t2yellow = 0;
daklowprofile 0:9a9e02000311 88 t2green = 0;
daklowprofile 0:9a9e02000311 89 }
daklowprofile 0:9a9e02000311 90 }
daklowprofile 0:9a9e02000311 91
daklowprofile 0:9a9e02000311 92 void t3light(char a){
daklowprofile 0:9a9e02000311 93 t3red = 0;
daklowprofile 0:9a9e02000311 94 t3yellow = 0;
daklowprofile 0:9a9e02000311 95 t3green = 0;
daklowprofile 0:9a9e02000311 96 if (a == 'R'){
daklowprofile 0:9a9e02000311 97 t3red = 1;
daklowprofile 0:9a9e02000311 98 t3yellow = 0;
daklowprofile 0:9a9e02000311 99 t3green = 0;
daklowprofile 0:9a9e02000311 100 }else if(a == 'Y'){
daklowprofile 0:9a9e02000311 101 t3red = 0;
daklowprofile 0:9a9e02000311 102 t3yellow = 1;
daklowprofile 0:9a9e02000311 103 t3green = 0;
daklowprofile 0:9a9e02000311 104 }else if(a == 'G'){
daklowprofile 0:9a9e02000311 105 t3red = 0;
daklowprofile 0:9a9e02000311 106 t3yellow = 0;
daklowprofile 0:9a9e02000311 107 t3green = 1;
daklowprofile 0:9a9e02000311 108 }else{
daklowprofile 0:9a9e02000311 109 t3red = 0;
daklowprofile 0:9a9e02000311 110 t3yellow = 0;
daklowprofile 0:9a9e02000311 111 t3green = 0;
daklowprofile 0:9a9e02000311 112 }
daklowprofile 0:9a9e02000311 113 }
daklowprofile 0:9a9e02000311 114
daklowprofile 0:9a9e02000311 115 void traffic (char t1, char t2, char t3, int time_end){
daklowprofile 0:9a9e02000311 116 if(timer_tf.read() <= time_end){
daklowprofile 0:9a9e02000311 117 t1light(t1);
daklowprofile 0:9a9e02000311 118 t2light(t2);
daklowprofile 0:9a9e02000311 119 t3light(t3);
daklowprofile 0:9a9e02000311 120 timer_tf.reset();
daklowprofile 0:9a9e02000311 121 }
daklowprofile 0:9a9e02000311 122 //wait(time);
daklowprofile 0:9a9e02000311 123 }
daklowprofile 0:9a9e02000311 124
daklowprofile 0:9a9e02000311 125 void traffic_timer (char t1, char t2, char t3){
daklowprofile 0:9a9e02000311 126 t1light(t1);
daklowprofile 0:9a9e02000311 127 t2light(t2);
daklowprofile 0:9a9e02000311 128 t3light(t3);
daklowprofile 0:9a9e02000311 129 }
daklowprofile 0:9a9e02000311 130
daklowprofile 0:9a9e02000311 131 void traffic_default (char t1, char t2, char t3, int time){
daklowprofile 0:9a9e02000311 132 t1light(t1);
daklowprofile 0:9a9e02000311 133 t2light(t2);
daklowprofile 0:9a9e02000311 134 t3light(t3);
daklowprofile 0:9a9e02000311 135 wait(time);
daklowprofile 0:9a9e02000311 136 }
daklowprofile 0:9a9e02000311 137
daklowprofile 0:9a9e02000311 138
daklowprofile 0:9a9e02000311 139
daklowprofile 0:9a9e02000311 140 void normal_traffic(){
daklowprofile 0:9a9e02000311 141 ///*
daklowprofile 0:9a9e02000311 142 if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
daklowprofile 0:9a9e02000311 143 traffic_timer('G','R','R');
daklowprofile 0:9a9e02000311 144 }
daklowprofile 0:9a9e02000311 145 if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
daklowprofile 0:9a9e02000311 146 traffic_timer('Y','R','R');
daklowprofile 0:9a9e02000311 147 }
daklowprofile 0:9a9e02000311 148 if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
daklowprofile 0:9a9e02000311 149 traffic_timer('R','G','R');
daklowprofile 0:9a9e02000311 150 }
daklowprofile 0:9a9e02000311 151 if(timer_tf.read() >= 91 && timer_tf.read() <= 94){
daklowprofile 0:9a9e02000311 152 traffic_timer('R','Y','R');
daklowprofile 0:9a9e02000311 153 }
daklowprofile 0:9a9e02000311 154 if(timer_tf.read() >= 95 && timer_tf.read() <= 165){
daklowprofile 0:9a9e02000311 155 traffic_timer('R','R','G');
daklowprofile 0:9a9e02000311 156 }
daklowprofile 0:9a9e02000311 157 if(timer_tf.read() >= 166 && timer_tf.read() <= 169){
daklowprofile 0:9a9e02000311 158 traffic_timer('R','R','Y');
daklowprofile 0:9a9e02000311 159 }
daklowprofile 0:9a9e02000311 160 /*
daklowprofile 0:9a9e02000311 161 if(timer_tf.read() >= 0 && timer_tf.read() <= 5){
daklowprofile 0:9a9e02000311 162 traffic_timer('G','R','R');
daklowprofile 0:9a9e02000311 163 }
daklowprofile 0:9a9e02000311 164 if(timer_tf.read() >= 6 && timer_tf.read() <= 10){
daklowprofile 0:9a9e02000311 165 traffic_timer('Y','R','R');
daklowprofile 0:9a9e02000311 166 }
daklowprofile 0:9a9e02000311 167 if(timer_tf.read() >= 11 && timer_tf.read() <= 15){
daklowprofile 0:9a9e02000311 168 traffic_timer('R','G','R');
daklowprofile 0:9a9e02000311 169 }
daklowprofile 0:9a9e02000311 170 if(timer_tf.read() >= 16 && timer_tf.read() <= 20){
daklowprofile 0:9a9e02000311 171 traffic_timer('R','Y','R');
daklowprofile 0:9a9e02000311 172 }
daklowprofile 0:9a9e02000311 173 if(timer_tf.read() >= 21 && timer_tf.read() <= 25){
daklowprofile 0:9a9e02000311 174 traffic_timer('R','R','G');
daklowprofile 0:9a9e02000311 175 }
daklowprofile 0:9a9e02000311 176 if(timer_tf.read() >= 26 && timer_tf.read() <= 30){
daklowprofile 0:9a9e02000311 177 traffic_timer('R','R','Y');
daklowprofile 0:9a9e02000311 178 }
daklowprofile 0:9a9e02000311 179 */
daklowprofile 0:9a9e02000311 180 if(timer_tf.read() >= 170){
daklowprofile 0:9a9e02000311 181 timer_tf.reset();
daklowprofile 0:9a9e02000311 182 }
daklowprofile 0:9a9e02000311 183
daklowprofile 0:9a9e02000311 184 //traffic_default('G','R','R',47);
daklowprofile 0:9a9e02000311 185 //traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 186 //traffic_default('R','G','R',38);
daklowprofile 0:9a9e02000311 187 //traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 188 //traffic_default('R','R','G',70);
daklowprofile 0:9a9e02000311 189 //traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 190 }
daklowprofile 0:9a9e02000311 191
daklowprofile 0:9a9e02000311 192 void flip1() {
daklowprofile 0:9a9e02000311 193 led1 = !led1;
daklowprofile 0:9a9e02000311 194 timer_tf.stop();
daklowprofile 0:9a9e02000311 195 //pc.printf("%f\n",timer_tf.read());
daklowprofile 0:9a9e02000311 196 //wait(10);
daklowprofile 0:9a9e02000311 197 //pc.printf("%f\n",timer_tf.read());
daklowprofile 0:9a9e02000311 198 //imer_tf.start();
daklowprofile 0:9a9e02000311 199 //wait(5);
daklowprofile 0:9a9e02000311 200 //pc.printf("%f\n",timer_tf.read());
daklowprofile 0:9a9e02000311 201 /*
daklowprofile 0:9a9e02000311 202 if(timer_tf.read() <= 47){
daklowprofile 0:9a9e02000311 203 wait(15);
daklowprofile 0:9a9e02000311 204 }
daklowprofile 0:9a9e02000311 205 */
daklowprofile 0:9a9e02000311 206 if(timer_tf.read() >= 0/*48*/ && timer_tf.read() <= 51){
daklowprofile 0:9a9e02000311 207 wait(15);
daklowprofile 0:9a9e02000311 208 }
daklowprofile 0:9a9e02000311 209 if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
daklowprofile 0:9a9e02000311 210 //traffic_timer('R','G','R');
daklowprofile 0:9a9e02000311 211 traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 212 traffic_default('G','R','R',15);
daklowprofile 0:9a9e02000311 213 traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 214 }
daklowprofile 0:9a9e02000311 215 if(timer_tf.read() >= 91 && timer_tf.read() <= 94){
daklowprofile 0:9a9e02000311 216 //traffic_timer('R','Y','R');
daklowprofile 0:9a9e02000311 217 wait(3);
daklowprofile 0:9a9e02000311 218 traffic_default('G','R','R',15);
daklowprofile 0:9a9e02000311 219 traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 220 }
daklowprofile 0:9a9e02000311 221 if(timer_tf.read() >= 95 && timer_tf.read() <= 165){
daklowprofile 0:9a9e02000311 222 //traffic_timer('R','R','G');
daklowprofile 0:9a9e02000311 223 traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 224 traffic_default('G','R','R',15);
daklowprofile 0:9a9e02000311 225 traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 226 }
daklowprofile 0:9a9e02000311 227 if(timer_tf.read() >= 166 && timer_tf.read() <= 169){
daklowprofile 0:9a9e02000311 228 //traffic_timer('R','R','Y');
daklowprofile 0:9a9e02000311 229 wait(3);
daklowprofile 0:9a9e02000311 230 traffic_default('G','R','R',15);
daklowprofile 0:9a9e02000311 231 traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 232 }
daklowprofile 0:9a9e02000311 233 timer_tf.start();
daklowprofile 0:9a9e02000311 234 led1 = !led1;
daklowprofile 0:9a9e02000311 235 }
daklowprofile 0:9a9e02000311 236
daklowprofile 0:9a9e02000311 237 void flip2() {
daklowprofile 0:9a9e02000311 238 led2 = !led2;
daklowprofile 0:9a9e02000311 239 timer_tf.stop();
daklowprofile 0:9a9e02000311 240 if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
daklowprofile 0:9a9e02000311 241 //traffic_timer('G','R','R');
daklowprofile 0:9a9e02000311 242 traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 243 traffic_default('R','G','R',15);
daklowprofile 0:9a9e02000311 244 traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 245 }
daklowprofile 0:9a9e02000311 246 if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
daklowprofile 0:9a9e02000311 247 //traffic_timer('Y','R','R');
daklowprofile 0:9a9e02000311 248 wait(3);
daklowprofile 0:9a9e02000311 249 traffic_default('R','G','R',15);
daklowprofile 0:9a9e02000311 250 traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 251 }
daklowprofile 0:9a9e02000311 252 /*
daklowprofile 0:9a9e02000311 253 if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
daklowprofile 0:9a9e02000311 254 //traffic_timer('R','G','R');
daklowprofile 0:9a9e02000311 255 wait(15);
daklowprofile 0:9a9e02000311 256 }
daklowprofile 0:9a9e02000311 257 */
daklowprofile 0:9a9e02000311 258 if(timer_tf.read() >= 52/*91*/ && timer_tf.read() <= 94){
daklowprofile 0:9a9e02000311 259 //traffic_timer('R','Y','R');
daklowprofile 0:9a9e02000311 260 wait(15);
daklowprofile 0:9a9e02000311 261 }
daklowprofile 0:9a9e02000311 262 if(timer_tf.read() >= 95 && timer_tf.read() <= 165){
daklowprofile 0:9a9e02000311 263 //traffic_timer('R','R','G');
daklowprofile 0:9a9e02000311 264 traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 265 traffic_default('R','G','R',15);
daklowprofile 0:9a9e02000311 266 traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 267 }
daklowprofile 0:9a9e02000311 268 if(timer_tf.read() >= 166 && timer_tf.read() <= 169){
daklowprofile 0:9a9e02000311 269 //traffic_timer('R','R','Y');
daklowprofile 0:9a9e02000311 270 wait(3);
daklowprofile 0:9a9e02000311 271 traffic_default('R','G','R',15);
daklowprofile 0:9a9e02000311 272 traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 273 }
daklowprofile 0:9a9e02000311 274 timer_tf.start();
daklowprofile 0:9a9e02000311 275 led2 = !led2;
daklowprofile 0:9a9e02000311 276 }
daklowprofile 0:9a9e02000311 277
daklowprofile 0:9a9e02000311 278 void flip3() {
daklowprofile 0:9a9e02000311 279 led3 = !led3;
daklowprofile 0:9a9e02000311 280 timer_tf.stop();
daklowprofile 0:9a9e02000311 281 if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
daklowprofile 0:9a9e02000311 282 //traffic_timer('G','R','R');
daklowprofile 0:9a9e02000311 283 traffic_default('Y','R','R',3);
daklowprofile 0:9a9e02000311 284 traffic_default('R','R','G',15);
daklowprofile 0:9a9e02000311 285 traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 286 }
daklowprofile 0:9a9e02000311 287 if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
daklowprofile 0:9a9e02000311 288 //traffic_timer('Y','R','R');
daklowprofile 0:9a9e02000311 289 wait(3);
daklowprofile 0:9a9e02000311 290 traffic_default('R','R','G',15);
daklowprofile 0:9a9e02000311 291 traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 292 }
daklowprofile 0:9a9e02000311 293 if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
daklowprofile 0:9a9e02000311 294 //traffic_timer('R','G','R');
daklowprofile 0:9a9e02000311 295 traffic_default('R','Y','R',3);
daklowprofile 0:9a9e02000311 296 traffic_default('R','R','G',15);
daklowprofile 0:9a9e02000311 297 traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 298 }
daklowprofile 1:7bad12e1bbbc 299 if(timer_tf.read() >= 91 && timer_tf.read() <= 94){
daklowprofile 0:9a9e02000311 300 //traffic_timer('R','Y','R');
daklowprofile 0:9a9e02000311 301 wait(3);
daklowprofile 0:9a9e02000311 302 traffic_default('R','R','G',15);
daklowprofile 0:9a9e02000311 303 traffic_default('R','R','Y',3);
daklowprofile 0:9a9e02000311 304 }
daklowprofile 0:9a9e02000311 305 /*
daklowprofile 0:9a9e02000311 306 if(timer_tf.read() >= 95 && timer_tf.read() <= 165){
daklowprofile 0:9a9e02000311 307 //traffic_timer('R','R','G');
daklowprofile 0:9a9e02000311 308 }
daklowprofile 0:9a9e02000311 309 */
daklowprofile 0:9a9e02000311 310 if(timer_tf.read() >= 95/*166*/ && timer_tf.read() <= 169){
daklowprofile 0:9a9e02000311 311 //traffic_timer('R','R','Y');
daklowprofile 0:9a9e02000311 312 wait(15);
daklowprofile 0:9a9e02000311 313 }
daklowprofile 0:9a9e02000311 314 timer_tf.start();
daklowprofile 0:9a9e02000311 315 led3 = !led3;
daklowprofile 0:9a9e02000311 316 }
daklowprofile 0:9a9e02000311 317
daklowprofile 0:9a9e02000311 318
daklowprofile 0:9a9e02000311 319 int main() {
daklowprofile 0:9a9e02000311 320
daklowprofile 0:9a9e02000311 321 timer_tf.start();
daklowprofile 0:9a9e02000311 322 /*
daklowprofile 0:9a9e02000311 323 if(button1 == 1){
daklowprofile 0:9a9e02000311 324 flip1();
daklowprofile 0:9a9e02000311 325 }else if(button2 == 1){
daklowprofile 0:9a9e02000311 326 flip2();
daklowprofile 0:9a9e02000311 327 }else if(button3 == 1){
daklowprofile 0:9a9e02000311 328 flip3();
daklowprofile 0:9a9e02000311 329 }else{}
daklowprofile 0:9a9e02000311 330 */
daklowprofile 0:9a9e02000311 331 button1.mode(PullUp);
daklowprofile 0:9a9e02000311 332 button1.fall(&flip1);
daklowprofile 0:9a9e02000311 333 button2.mode(PullUp);
daklowprofile 0:9a9e02000311 334 button2.fall(&flip2);
daklowprofile 0:9a9e02000311 335 button3.mode(PullUp);
daklowprofile 0:9a9e02000311 336 button3.fall(&flip3);
daklowprofile 0:9a9e02000311 337 while(1){
daklowprofile 0:9a9e02000311 338 normal_traffic();
daklowprofile 0:9a9e02000311 339
daklowprofile 0:9a9e02000311 340 }
daklowprofile 0:9a9e02000311 341 }