Jeff Nguyen / Mbed 2 deprecated Traffic_Light

Dependencies:   mbed

main.cpp

Committer:
JN482
Date:
2018-11-20
Revision:
3:3e1dfbbc8227
Parent:
2:37d5e9d92b66

File content as of revision 3:3e1dfbbc8227:

/*
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
        reset();
        ResetSeg();
        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();//This sequence is the same as reset()*/
    } else if(unsafe == false) { //if in safemode run this sequence.
        //  Sequence          TRAFFIC 1, TRAFFIC 2
        reset();
        ResetSeg();
        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();nine();wait(1);ResetSeg();eight();wait(1);ResetSeg();seven();wait(1);ResetSeg();six();wait(1);ResetSeg();five();wait(1);
            ResetSeg();four();wait(1);ResetSeg();three();wait(1);ResetSeg();two();wait(1);ResetSeg();one();wait(1);Onboard = 0; ;ResetSeg();
            pedest = false;// The boolean variable is reset back to false
        }
        //----------------------------------------------
        GLED = 0; //     RED OFF     RED OFF
        ALED = 1; //     AMBER ON    AMBER ON
        wait(2);  //     GREEN OFF   GREEN OFF
        //----------------------------------------------
        RRLED = 1;
        RLED = 0; //     RED OFF     RED ON
        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 // This fall procedure in is the button interrupt class
    Switch.fall(&safe); 
    button.fall(&pedestrians); //& denotes the that this function is an Interrupt Service Routine
    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
}//End main function