Jeff Nguyen / Mbed 2 deprecated Traffic_Light

Dependencies:   mbed

main.cpp

Committer:
JN482
Date:
2018-11-11
Revision:
2:37d5e9d92b66
Parent:
1:992086cb4c27
Child:
3:3e1dfbbc8227

File content as of revision 2:37d5e9d92b66:

/*
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 
#include <SevenSegment.h>
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

bool pedest = false, unsafe = false; //setting up 2 true or false variables and a integer counter
int counter = 0;

void pedestrians()
//Declaring a procedure for changing a variable for pedestrians
{
    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;
    Onboard = 0;
    ResetSeg(); // calls the resetSeg procedure in the SevenSegment.h
    //All Lights are switched off. (Procedure Ends return to main)
}//End reset procedure.

void safe()
{
    if (unsafe == false) {
        unsafe = true;
        reset();
    } else if(unsafe == true) {
        unsafe = false;
        reset();
    }
}

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);
        //----------------------------------------------
        reset();          /*RRLED = 0; //    RED OFF     RED OFF
                          ALED = 0;   //     AMBER OFF   AMBER OFF
                          AALED = 0;  //     GREEN OFF   GREEN OFF
                            This sequence is the same as reset()*/
    } else if(unsafe == false) { //if in safemode run this sequence.
        //  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) {
            Buzzer = 1; // Turns on buzzer, however due to the fact that the Nucleo F411RE only has 15 DigitalOut of which 13 is usable as D0 and D1 are reseved pins.
            ResetSeg(); //Calls the ResetSeg procedure to turn OFF all of the segments
            nine(); //Call the procedure to display number 9
            wait(1);
            ResetSeg();
            eight();//Call the procedure to display number 8
            wait(1);
            ResetSeg();
            seven();//Call the procedure to display number 7
            wait(1);
            ResetSeg();
            six();//Call the procedure to display number 6
            wait(1);
            ResetSeg();
            five();//Call the procedure to display number 5
            wait(1);
            ResetSeg();
            four();//Call the procedure to display number 4
            wait(1);
            ResetSeg();
            three();//Call the procedure to display number 3
            wait(1);
            ResetSeg();
            two();//Call the procedure to display number 2
            wait(1);
            ResetSeg();
            one();//Call the procedure to display number 
            wait(1);
            ResetSeg();// The final Reset turns OFF all the segments 
            pedest = false;// The boolean variable is reset back to false
            Onboard = 0; // The Onboard LED is turned OFF
            // condition of counter is while the counter is less than or equal to 20
        }
        //----------------------------------------------
        GLED = 0; //     RED OFF     RED OFF
        ALED = 1; //     AMBER ON    AMBER ON
        wait(2);  //     GREEN OFF   GREEN OFF

        //----------------------------------------------
        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();//Reset the LEDs
    }
}//End sequence procedure.

int main()
{
    //start main function.
    //Main procedure, I will create a loop and initalise my variables
    /*To create my sequence I need 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
    //Switch.fall(&safe);
    button.fall(&pedestrians);
    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