Kevin Mueller / Mbed 2 deprecated Traffic_Light_Project

Dependencies:   mbed

main.cpp

Committer:
kmueller
Date:
2019-12-04
Revision:
0:4bacc097881b

File content as of revision 0:4bacc097881b:

/**************************************************************
/ Traffic Light Project Group n-11
/
/ The following code describes a traffic light sequence for both safe and unsafe conditions 
/**************************************************************/

#include "mbed.h"

// E/W Road
DigitalOut red(p21);             //Outputs to a Red Light LED for EW Road
DigitalOut green(p23);           //Outputs to a Green Light LED for EW Road
DigitalOut yellow(p22);          //Outputs to a Yellow Light LED for EW Road
 
// N/S Road 
DigitalOut red2(p27);            //Outputs to a Red Light LED for NS Road
DigitalOut green2(p28);          //Outputs to a Green Light LED for NS Road
DigitalOut yellow2(p29);         //Outputs to a Yellow Light LED for NS Road
 
// Buttons
// EW Buttons                
DigitalIn button1 (p9);          //Assigns an EW button as an input
DigitalIn button2 (p10);          //Assigns an EW button as an input
// NS Buttons
DigitalIn button3 (p15);          //Assigns an NS button as an input
DigitalIn button4 (p16);          //Assigns an NS button as an input
 
float i;                         //float variable i
int j;                           //stating variable j
int k;                           //stating variable k
int x;                           //stating variable x
 
int main() {
    
    while(1) {                                         //repeat loop indefinitely
        k=1;                                           //variable k = 1
        x=0;                                           //variable x = 0
        if(k==1){                                      //if k equals 1
        for(i=0;  i<3; i+=0.1){                      //increment i by 0.1 till 100
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed
                j = 1;                                 //set j = 1
            }
            else {                                     //otherwise
                red = green2 = 1;                      //EW light is red and therefore NS road is green
                yellow = green = yellow2 = red2 = 0;   //EW yellow & green lights are off therefore NS yellow and red also are off
                }
                wait(0.1);                             //wait 0.1 seconds
            }
        for(i=0; i<30; i+=1) {                          //increment i by 1 until 3
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed
                j = 1;                                 //set j = 1
            }
            else{                                      //otherwise
            yellow = red2 = green2= green = 0;         //EW yellow & green lights are off therefore NS green and red also are off
            yellow2= red = 1;                          //NS yellow and EW red are on 
            wait(0.1);                                 //wait 0.5secs
}
            yellow = red2 = green2= green= yellow2 = 0;//EW yellow & green lights are off therefore NS green and red and yellow also are off
            red = 1;                                   //EW red is on 
            wait(0.1);                                 //wait 0.5secs
            }
        }
        
        for(i=0; i<3; i+=0.1){                        //increment i by 0.1 till 30
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed 
                j = 1;                                 //set j = 1
            }  
            
            else{                                      //otherwise
                red = red2 = 1;                        //EW read and NS turned on
                yellow = yellow2 = green = green2 = 0; //EW and NS green and yellow turned off
                }
                wait(0.1);                             //wait 0.1 secs
            }
            
        for(i=0; i<30; i+=1){                           //increment i by 1 till 3
            
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed 
                j = 1;                                 //set j equal 1
                }
                
            else{                                      //otherwise
                red = yellow = red2 = 1;               //EW red & yellow and NS red turn on 
                green = green2 = yellow2 = 0;          //EW green & NS green and yellow turn off
                wait(0.1);                             //wait 0.5secs
                } 
                red = red2 = 1;                        //EW red & NS red turn on
                 yellow = yellow2 = green = green2 = 0; //EW yellow & green and NS yellow and green turn off 
                wait(0.1);                             //wait 0.5secs
                }
            
            k=0;                                       //set variable k = 0
            x=1;                                       //set varialbe x = 1
        
        
        if(x==1){                                      //if x = 1
                                                            // End of Sequence one
            
        for(i=0; i<10; i+=0.1){                       //increment i by 0.1 till 100
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed
                j = 1;                                 //set j equal to 1
                }
            
            else{                                      //otherwise
                green = red2 = 1;                      //EW green and NS red turn on 
                red = yellow = yellow2 = green2 = 0;   //EW red & yellow and NS yellow and green turn off
                } 
                wait(0.1);                             //wait 0.1secs
            }
        for(i=0; i<30; i+=1){                           //increment i by 1 till 3
                
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed
                j = 1;                                 //set j equal to 1
                }
                
            else{                                      //otherwise
                yellow = red2 = 1;                     //EW yellow and NS red turn on
                red = yellow2 = green = green2 = 0;    //EW red and green & NS yellow and green turn off
                }
            
            } 
            yellow = 1;
            wait(3);
            yellow = 0;   
                
        for(i=0; i<3; i+=0.1){                        //increment i by 0.1 till 30
            
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed
                j = 1;                                 //set j equal to 1
                }
                
            else{                                      //otherwise
                red = red2 = 1;                        //EW and NS red turn on 
                
                yellow = yellow2 = green = green2 = 0; //EW yellow & green & NS yellow and green turn off
                }
                wait(0.1);                             //wait 0.1secs
            }
        
        for(i=0; i<3; i+=0.1){                        //increment i by 0.1 till 30
            
            if (button1.read()==0 or button2.read()==0){     //if EW buttons are pressed
                j = 1;                                 //set j equal to 1
                }
            
            else{                                      //otherwise
                red = red2 = yellow2 = 1;              //EW red and NS red and yellow turn on 
                yellow = green = green2 = 0;           //EW yellow and green and NS green turn off
                }
                wait(0.1);                             // wait 0.1secs
            }                                              // Sequence 2 done
        }
        x=0;                                           //set x to 0
        k=1;                                           //set k to 1
         
        if(j==1){                                      //if j equals to 1
             
            red = 1;                                   //EW red turn on 
            }
        }      
}