UoD_ME21001_Group_1_07 / Mbed 2 deprecated Project_Test_9

Dependencies:   mbed

main.cpp

Committer:
cgoodger
Date:
2019-11-22
Revision:
1:fb4ff3d2b5d0
Parent:
0:67f1965f904d

File content as of revision 1:fb4ff3d2b5d0:

//Amalgamation of Project_Test_1 and Project_Test_2_switch codes to check viability of a simple 1/2 switch light interface.
//Expansion of Project_Test_1 protocol to include three more pins and two more traffic light circuits.
//Project_Test_8_Switchtest2 amalgamated to provide switch command to light circuit 
//the code checks if the value of X has changed from 0 to 1 at the end of the code turning all lights to red for 10 seconds before-
//returning the value of X to 0 to continue the normal lights sequence. X remains 0 unless button is pressed. 
//Normal light sequence being that the lights cycle through a red, yellow, green loop at prespecified time intervals. 
//Lights act in opposite pairs to reduce pin number and to cimpify code. 


#include "mbed.h"                           //load mbed library 
DigitalOut red1(p21);                       //Assign digital output value of red1 to mbed pin p21
DigitalOut yellow1(p22);                    //Assign digital output value of yellow1 to mbed pin p22
DigitalOut green1(p23);                     //Assign digital output value of green1 to mbed pin p23
DigitalOut red2(p24);                       //Assign digital output value of red2 to mbed pin p24
DigitalOut yellow2(p25);                    //Assign digital output value of yellow2 to mbed pin p25
DigitalOut green2(p27);                     //Assign digital output value of green2 to mbed pin p26
DigitalIn switchInput(p15);                 //Assign digital input value to p15 pin 

            
int main() {         
    int x = 0;
    while(1) {                              //Repeat indefinitely
                                            
        if(switchInput.read() == 0) {       //Read the input state only once in the while loop
            red1 = 1;                       //Turn on red1
            red2 = 1;                       //Turn on red2
            wait(3);                        //Wait 3 seconds
            if(switchInput.read() == 1){    //Check condition of varibale X
                x = 1;
                }
            yellow1 = 1;                    //Turn on yellow1
            wait(1);                        //Wait 1 second
            if(switchInput.read() == 1){    //Check condition of variable X
                x = 1;
                }
            red1 = 0;                       //Turn off red1
            yellow1 = 0;                    //Turn off yellow1
            green1 = 1;                     //Turn on green1        
            wait(3);                        //Wait 3 seconds
            if(switchInput.read() == 1){    //Check condition of variable X
                x = 1;
                }
            green1 = 0;                     //Turn off green1
            yellow1 = 1;                    //Turn on yellow1
            wait(1);                        //Wait 1 second
            if(switchInput.read() == 1){    //Check condition of varible X
                x = 1;
                }
            yellow1 = 0;                    //Turn off yellow1
            red1 = 1;                       //Turn on red1
            wait(3);                        //Wait 3 seconds
            if(switchInput.read() == 1){    //Check condition of variable X
                x = 1;
                }
            yellow2 = 1;                    //Turn on yellow2
            wait(1);                        //Wait 1 second
            if(switchInput.read() == 1){    //Check condition of variable X
                x = 1;
                }
            red2 = 0;                       //Turn off red2
            yellow2=0;                      //Turn off yellow2
            green2=1;                       //Turn on green2
            wait(3);                        //Wait 3 seconds
            if(switchInput.read() == 1){    //Check condition of variable X
                x = 1;
                }
            green2 = 0;                     //Turn off green2
            yellow2=1;                      //Turn on yellow2
            wait(1);                        //Wait 1 second
            if(switchInput.read() == 1){    //Check condition of variable X
                x = 1;
                }
            yellow2=0;                      //Turn off yellow2
            if(x == 1) {                    //Check condition of varibale X
             red1 = 1;                      //Turn on red1 
             red2 = 1;                      //Turn on red2
             yellow1 = 0;                   //Turn off yellow1
             yellow2 = 0;                   //Turn off yellow2
             green1= 0;                     //Turn off green1
             green2 = 0;                    //Turn off green2
             wait(10);                      //Wait 10 seconds with red1&2 in on condition 
             x = 0;                         //Return conditoion of variable X to 0 
             }
             }
    }
}                                           //End of statement