Jeff Nguyen / Mbed 2 deprecated Traffic_Light

Dependencies:   mbed

main.cpp

Committer:
JN482
Date:
2018-10-20
Revision:
0:41f066ce55cf
Child:
1:992086cb4c27

File content as of revision 0:41f066ce55cf:

/*
This project includes both the safe, unsafe sequences, pedestrian crossing, and a buzzer.
The Assignment task is to create both the safe and unsafe traffic light sequence.
*/

#include <mbed.h> // importing the mbed library to allow to use input and output fuctions 

DigitalOut Onboard(LED1);//declaring the onboard led as Onboard
DigitalOut RLED(D2);//RLED refers to the first traffic light Red
DigitalOut ALED(D3);//ALED refers to the first traffic light Amber
DigitalOut GLED(D4);//GLED refers to the first traffic light Green
DigitalOut RRLED(D5);//RRLED refers to the second traffic light Red
DigitalOut AALED(D6);//AALED refers to the second traffic light Amber
DigitalOut GGLED(D7);//GGLED refers to the second traffic light Green
InterruptIn button (USER_BUTTON); //I will be using the onboard button as a pedestrian button
InterruptIn Switch(D15);//I will be using an external wire to act as a button.
DigitalOut Buzzer(D10);//Buzzer/Beeper

bool pedest = false, unsafe = false;
int counter = 0;  

void pedestrians()
//Declaring a procedure for changing a variable
{
    pedest = true;
    Onboard = 1;
}
void reset()
{//start reset procedure.
  //Declaring a procedure named reset, 0 arguments are passed.
  RLED = 0;    
  ALED = 0;
  GLED = 0; 
  RRLED = 0;
  AALED = 0;
  GGLED = 0;
  //All Lights are switched off. (Procedure Ends return to main)
}//End reset procedure.


void sequence(int unsafe)
// Declaring a procedure for sequence. 2 Arguments are recieved - unsafe and pedestrians
{//Start sequence procedure. 
    if(unsafe == true)
    {
    //  Sequence          TRAFFIC 1, TRAFFIC 2
        RLED = 1;  //     RED ON     RED OFF                
        GGLED = 1; //     AMBER OFF  AMBER OFF 
        wait(5);   //     GREEN OFF  GREEN ON
    //----------------------------------------------
        GGLED = 0; //     RED ON     RED OFF  
        ALED = 1;  //     AMBER OFF  AMBER ON
        AALED = 1; //     GREEN OFF  GREEN OFF
        wait(2);   
    //----------------------------------------------
        RLED = 0;  //     RED OFF    RED OFF
        RRLED = 1; //     AMBER OFF  AMBER OFF
        ALED = 0;  //     GREEN ON   GREEN OFF
        AALED = 0;
        GLED = 1;
        wait(5);   
    //----------------------------------------------
        GLED = 0;  //     RED OFF     RED OFF
        ALED = 1;  //     AMBER ON    AMBER ON
        AALED = 1; //     GREEN OFF   GREEN OFF
        wait(2);   
    //----------------------------------------------
        RRLED = 0; //     RED OFF     RED OFF
        ALED = 0;  //     AMBER OFF   AMBER OFF
        AALED = 0;  //     GREEN OFF   GREEN OFF     ----------> The same as reset I may replace this with a call to reset
    }
    else if(unsafe == false)
    {
    //  Sequence          TRAFFIC 1, TRAFFIC 2
        RLED = 1;  //     RED ON     RED OFF                
        GGLED = 1; //     AMBER OFF  AMBER OFF 
        wait(5);   //     GREEN OFF  GREEN ON
    //----------------------------------------------
        GGLED = 0; //     RED ON     RED OFF  
                   //     AMBER OFF  AMBER ON
        AALED = 1; //     GREEN OFF  GREEN OFF
        wait(2);   
    //----------------------------------------------
        RLED = 1;  //     RED ON    RED ON
        RRLED = 1; //     AMBER OFF  AMBER OFF
        ALED = 0;  //     GREEN OFF   GREEN OFF
        AALED = 0;
        wait(2);
        if (pedest == true)
        {
                do // A do while loop, do the following until the condition counter has been met
                {
                    GGLED = 1;
                    Buzzer = 1;
                    GLED = 1;
                    wait(0.2);
                    GGLED = 0;
                    GLED = 0;
                    Buzzer = 0;
                    wait(0.2);
                    counter ++;
                    pedest = false;
                    Onboard = 0;
                } 
                while(counter <= 20);// condition of counter is while the counter is less than or equal to 10
        }        
    //----------------------------------------------
        GLED = 0;  //     RED OFF     RED OFF
                 //     AMBER ON    AMBER ON
        ALED = 1; //     GREEN OFF   GREEN OFF
        wait(2);   
    //----------------------------------------------
        RRLED = 1; //     RED OFF     RED ON 
        RLED = 0;
        ALED = 0; //     AMBER OFF   AMBER OFF
        GLED = 1; //     GREEN ON    GREEN OFF
        wait(5);
    //----------------------------------------------
        GLED = 0;
        ALED = 1;
        wait(2);
        ALED = 0;
        RLED = 1;
        wait(2);
        AALED = 1;
        wait(2);
        reset();
    }
}//End sequence procedure.

int main()
{//start main function.
    //Main procedure, I will create a loop and initalise my variables
    bool unsafe = false; 
    /*To create my sequence Ivneed my lights to start on the same lights everytime.
    Therefore I will create a reset which will set all of the lights off.
    I will do this by creating a separate procedure for my main procedure to point to.*/
    
    reset();//Main points to reset procedure
    button.fall(&pedestrians); // This fall procedure in is the button interrupt class 
    while(1)
    {//Start of while
    
        /*infinite loop, the program will never be able to exit this loop because the condition is always TRUE. 
        Now that I have set up my infinite loop I can now start the sequence procedure.*/
        sequence(unsafe);// I am calling the sequence procedure with the arguments unsafe and pedestrians.
        
    }//End of while 
    return(0); // The complier warns me that this statement is unreachable, I know. Due the infinite loop above.  
}//End main function