M T BULUSHI / Mbed 2 deprecated traffic_bish

Dependencies:   mbed

Revision:
1:673b7c90b411
Parent:
0:e63002f11fd1
Child:
2:470641683dfb
diff -r e63002f11fd1 -r 673b7c90b411 main.cpp
--- a/main.cpp	Thu Dec 02 13:45:34 2021 +0000
+++ b/main.cpp	Tue Dec 28 13:08:18 2021 +0000
@@ -1,12 +1,271 @@
 #include "mbed.h"
 
-DigitalOut myled(LED1);
+
+
+DigitalOut  PIN_RED(D8);
+DigitalOut  PIN_AMBER(D9);
+DigitalOut  PIN_GREEN(D10);
+DigitalIn    p1(D0);   // representing sensor for people
+DigitalIn    p2(D1);   
+DigitalIn    p3(D2);   
+DigitalIn    p4(D3);  
+DigitalIn    c1(D4); // representing sensor for cars  
+DigitalIn    c2(D5);   
+DigitalIn    c3(D6);  
+DigitalIn    c4(D7);    
+
+// lights onboard for pedestrian 
+
+DigitalOut ledR(LED_RED);
+DigitalOut ledG(LED_GREEN);
+
+// initiliasing variables
+
+int count_p ;
+int count_c;
+Timer t;
+bool pedestrian = false;
+
+
+void Count(){
+        count_p = 0;
+        count_c = 0;
+        
+        if(p1 == 1){
+            count_p = count_p + 1;
+            
+            if(t.read() == 0){
+                t.start();
+                }
+            
+            
+            
+           
+            }
+        if(p2 == 1){
+            count_p = count_p + 1;
+            
+            if(t.read() == 0){
+                t.start();
+                }
+                
+            }
+            
+        if(p3 == 1){
+            count_p = count_p + 1;
+            
+            if(t.read() == 0){
+                t.start();
+                }
+                
+            }
+        if(p4 == 1){
+            count_p = count_p + 1;
+            
+               if(t.read() == 0){
+                t.start();
+                }
+                
+            }
+            
+        if(c1 == 1){
+            count_c = count_c + 1;
+            }
+        if(c2 == 1){
+            count_c = count_c + 1;
+            }
+            
+        if(c3 == 1){
+            count_c = count_c + 1;
+            }
+        if(c4 == 1){
+            count_c = count_c + 1;
+            }
+            
+        
+        
+        
+        }
+
+void Red(){
+    
+
+    PIN_RED = 1;
+    PIN_AMBER = 0;
+    PIN_GREEN = 0;
+        
+        };
+        
+void Red_Amber(){
+    
+    PIN_RED = 1;
+    PIN_AMBER = 1;
+    PIN_GREEN = 0;
+    
+        
+        };
+        
+void Amber(){
+
+    PIN_RED = 0;
+    PIN_AMBER = 1;
+    PIN_GREEN = 0;
+        
+        }
+        
+        
+void Green(){
+    PIN_RED = 0;
+    PIN_AMBER = 0;
+    PIN_GREEN = 1;
+        }
+        
+void Stop(){
+    // when our changetraffic light function finishes at is green.. 
+    Amber();
+    wait(3);
+    // turn off amber, then turn red on for 5 seconds
+    Red();
+        
+    }
+void Start(){
+    
+    
+    }
 
-int main() {
-    while(1) {
-        myled = 1;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
+        
+    
+
+        
+void changeLights(){
+ 
+    if(pedestrian == true){
+            // red and amber on for 2 seconds ( red is already on though)
+            Red_Amber();
+            wait(2);
+    
+            // turn off red and amber, then turn on green
+            Green();
+            wait(3);
+            pedestrian = false;
+        }
+    else {
+        // green off, Amber on for 3 seconds
+    Amber();
+    wait(3);
+    
+    // turn off amber, then turn red on for 5 seconds
+    Red();
+    wait(5);
+    
+    // red and amber on for 2 seconds ( red is already on though)
+    Red_Amber();
+    wait(2);
+    
+    // turn off red and amber, then turn on green
+    Green();
+    wait(3);
+        
+        }
+    
+    
+ 
+ 
+ 
+ }
+ 
+ void P_Green(){
+                
+            ledR = 1;
+            ledG = 0;
+     
+     }
+ 
+void P_Red(){
+                
+            ledR = 0;
+            ledG = 1;
+     
+     }
+void Fault(){
+    
+    Amber();
+    P_Red();
+    
+    while(true){
+        
+        
+    wait(0.5);
+    PIN_AMBER = 0;
+    ledR = 1;
+    wait(0.5);
+    PIN_AMBER = 1;
+    ledR = 0;
+    
     }
+    
 }
+ 
+ 
+
+int main()
+{
+ 
+        try{
+            
+        ledR = 0;
+        ledG = 1;
+        Green();
+        wait(3);
+        
+        
+        
+        
+    
+       while (true) {
+        
+        pedestrian = false;
+        
+        // checks for count every 5 seconds
+        
+        wait(5);
+        Count();
+
+        if (count_p > count_c){
+                Stop();
+                P_Green();
+                wait(20);
+                P_Red();
+                t.reset();
+                //check variable for our changelight function 
+                pedestrian = true;
+
+            }
+        else if (count_p > 0 & t.read() > 60){
+                Stop();
+                P_Green();
+                wait(20);
+                P_Red();
+                t.reset();
+                //check variable for our changelight function 
+                pedestrian = true;
+                
+            }
+            
+            changeLights();
+
+            
+            
+    
+        }
+
+
+
+    }
+    }
+    
+    catch(...){
+        Fault();
+        
+        }
+