UoD_ME21001_Group_1_02 / Mbed 2 deprecated TrafficlightsV2

Dependencies:   mbed

Revision:
0:ff5c694af047
Child:
1:848c19e20191
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 31 15:32:14 2019 +0000
@@ -0,0 +1,255 @@
+#include "mbed.h"
+ 
+ 
+#define MAX_LEN 64
+//tester
+//DigitalOut led1(LED1);
+//InterruptIn button(p28);
+ 
+//Timer class
+Timer timer_tf;
+ 
+//print for debug
+Serial pc(USBTX, USBRX);
+ 
+ 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+ 
+//interrupt  only on p13,14,27,28
+InterruptIn button1(p13);
+InterruptIn button2(p14);
+InterruptIn button3(p27);
+InterruptIn button4(p28);
+ 
+//Traffic light 1+2
+DigitalOut t1red(p5);
+DigitalOut t1yellow(p6);
+DigitalOut t1green(p7);
+//Traffic lights 2+4
+DigitalOut t2red(p11);
+DigitalOut t2yellow(p12);
+DigitalOut t2green(p30);
+
+ 
+//switch
+//DigitalIn button1(p26);
+//DigitalIn button2(p27);
+//DigitalIn button3(p28);
+ 
+ 
+ 
+ 
+void t1light(char a){
+        t1red = 0;
+        t1yellow = 0;
+        t1green = 0;    
+    if (a == 'R'){
+        t1red = 1;
+        t1yellow = 0;
+        t1green = 0;
+    }else if(a == 'F'){
+        t1red = 1;
+        t1yellow = 1;
+        t1green = 0;
+    }else if(a == 'Y'){
+        t1red = 0;
+        t1yellow = 1;
+        t1green = 0;
+    }else if(a == 'G'){
+        t1red = 0;
+        t1yellow = 0;                                                 
+        t1green = 1;
+    }else{
+        t1red = 0;
+        t1yellow = 0;
+        t1green = 0;    
+    } 
+}
+ 
+void t2light(char a){
+        t2red = 0;
+        t2yellow = 0;
+        t2green = 0;    
+    if (a == 'R'){
+        t2red = 1;
+        t2yellow = 0;
+        t2green = 0;
+    }else if(a == 'F'){
+        t2red = 1;
+        t2yellow = 1;
+        t2green = 0;
+    }else if(a == 'Y'){
+        t2red = 0;
+        t2yellow = 1;
+        t2green = 0;
+    }else if(a == 'G'){
+        t2red = 0;
+        t2yellow = 0;
+        t2green = 1;
+    }else{
+        t2red = 0;
+        t2yellow = 0;
+        t2green = 0;    
+    } 
+}
+ 
+
+ 
+void traffic (char t1, char t2, int time_end){
+    if(timer_tf.read() <= time_end){
+    t1light(t1);
+    t2light(t2);
+    timer_tf.reset();
+    }
+    //wait(time);
+    }
+ 
+void traffic_timer (char t1, char t2){
+    t1light(t1);
+    t2light(t2);
+    }
+ 
+void traffic_default (char t1, char t2, int time){
+    t1light(t1);
+    t2light(t2);
+    wait(time);
+    }
+ 
+ 
+   
+void normal_traffic(){
+///*
+    if(timer_tf.read() >= 0 && timer_tf.read() <=10){
+    traffic_timer('G','R');
+    }
+    if(timer_tf.read() >= 11 && timer_tf.read() <= 16){
+    traffic_timer('Y','R');
+    }
+    if(timer_tf.read() >= 17 && timer_tf.read() <= 23){
+    traffic_timer('R','G');
+    }
+    if(timer_tf.read() >= 24 && timer_tf.read() <= 30){
+    traffic_timer('R','Y');
+    }
+    if(timer_tf.read() >= 31 && timer_tf.read() <= 37){
+    traffic_timer('F','Y');
+    }
+    
+/*
+    if(timer_tf.read() >= 0 && timer_tf.read() <= 5){
+    traffic_timer('G','R');
+    }
+    if(timer_tf.read() >= 6 && timer_tf.read() <= 10){
+    traffic_timer('Y','R');
+    }
+    if(timer_tf.read() >= 11 && timer_tf.read() <= 15){
+    traffic_timer('R','G');
+    }
+    if(timer_tf.read() >= 16 && timer_tf.read() <= 20){
+    traffic_timer('R','Y',;
+    }
+
+*/         
+    if(timer_tf.read() >= 170){
+    timer_tf.reset();
+    }
+ 
+    //traffic_default('G','R',47);
+    //traffic_default('Y','R',3);
+    //traffic_default('R','G',38);
+    //traffic_default('R','Y',3);
+    //traffic_default('R','R',70);
+    //traffic_default('R','R',3);
+}
+ 
+void flip1() {
+    led1 = !led1;
+    timer_tf.stop();
+    //pc.printf("%f\n",timer_tf.read());
+    //wait(10);
+    //pc.printf("%f\n",timer_tf.read());
+    //imer_tf.start();
+    //wait(5);
+    //pc.printf("%f\n",timer_tf.read());
+/*
+    if(timer_tf.read() <= 47){
+    wait(15);
+    }
+*/
+    if(timer_tf.read() >= 0/*48*/ && timer_tf.read() <= 51){
+    wait(15);
+    }
+    if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
+    //traffic_timer('R','G');
+    traffic_default('R','Y',3);
+    traffic_default('G','R',15);
+    traffic_default('Y','R',3);
+    }
+    if(timer_tf.read() >= 91 && timer_tf.read() <= 94){
+    //traffic_timer('R','Y','R');
+    wait(3);
+    traffic_default('G','R',15);
+    traffic_default('Y','R',3);
+    }
+    
+    timer_tf.start();
+    led1 = !led1;
+}
+ 
+void flip2() {
+    led2 = !led2;
+    timer_tf.stop();
+    if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
+    //traffic_timer('G','R','R');
+    traffic_default('Y','R',3);
+    traffic_default('R','G',15);
+    traffic_default('R','Y',3);
+    }
+    if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
+    //traffic_timer('Y','R','R');
+    wait(3);
+    traffic_default('R','G',15);
+    traffic_default('R','Y',3);
+    }
+/*
+    if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
+    //traffic_timer('R','G','R');
+    wait(15);
+    }
+*/
+    if(timer_tf.read() >= 52/*91*/ && timer_tf.read() <= 94){
+    //traffic_timer('R','Y');
+    wait(15);
+    }   
+    timer_tf.start();
+    led2 = !led2;
+}
+ 
+ 
+int main() {
+ 
+    timer_tf.start();
+/*    
+    if(button1 == 1){
+    flip1();
+    }else if(button2 == 1){
+    flip2();
+    }else if(button3 == 1){
+    flip3();
+    }else{}
+*/  
+    button1.mode(PullUp);    
+    button1.fall(&flip1);
+    button2.mode(PullUp);
+    button2.fall(&flip2);
+    button3.mode(PullUp);
+    button3.fall(&flip1);
+    button4.mode(PullUp);
+    button4.fall(&flip2);
+    while(1){
+    normal_traffic();
+    
+    }
+}