UoD_ME21001_Group_2_03 / Mbed 2 deprecated Finalcode2

Dependencies:   mbed

Committer:
alvaro75
Date:
Fri Nov 29 18:07:12 2019 +0000
Revision:
0:6cba4a800507
Finalcode2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alvaro75 0:6cba4a800507 1 /************************************************************
alvaro75 0:6cba4a800507 2 * University of Dundee *
alvaro75 0:6cba4a800507 3 * ME21001 - Software Applications for Engineering Design *
alvaro75 0:6cba4a800507 4 * Traffic Intersection Project *
alvaro75 0:6cba4a800507 5 * Group 3 *
alvaro75 0:6cba4a800507 6 * Alvaro Almeida Arencibia *
alvaro75 0:6cba4a800507 7 * Adams Boyd *
alvaro75 0:6cba4a800507 8 * Qiushuhao *
alvaro75 0:6cba4a800507 9 * *
alvaro75 0:6cba4a800507 10 * This progam will control 4 traffic lights *
alvaro75 0:6cba4a800507 11 * with the intervention of a train *
alvaro75 0:6cba4a800507 12 * *
alvaro75 0:6cba4a800507 13 * For this program we will have 7 leds as outputs and 3 *
alvaro75 0:6cba4a800507 14 * inputs; 2 Ldrs and 1 switch *
alvaro75 0:6cba4a800507 15 ************************************************************/
alvaro75 0:6cba4a800507 16
alvaro75 0:6cba4a800507 17
alvaro75 0:6cba4a800507 18
alvaro75 0:6cba4a800507 19 #include "mbed.h" // We include the library called "mbed.h"
alvaro75 0:6cba4a800507 20
alvaro75 0:6cba4a800507 21 DigitalOut redled1 (p23); // pin 23 is declared as an output with the name of redled1 (this led wiill be for the cars)
alvaro75 0:6cba4a800507 22 DigitalOut redledp (p21); // pin 21 is declared as an output with the name of redledp (this led will be for the pedestrians)
alvaro75 0:6cba4a800507 23 DigitalOut greenled1 (p24); // pin 24 is declared as an output with the name of greenled1 (this led will be for the cars)
alvaro75 0:6cba4a800507 24 DigitalOut greenledp (p27); // pin 27 is declared as an output with the name of greenledp (this led will be for the pedestrians)
alvaro75 0:6cba4a800507 25 DigitalOut yellowled1 (p22); // pin 22 is declared as an output with the name of yellowled1 (this led will be for the cars)
alvaro75 0:6cba4a800507 26 DigitalIn switchstate (p10); // pin 10 is declared as an input with the name of switchstate (this switch will be used by the pedestrians)
alvaro75 0:6cba4a800507 27 DigitalIn LDR1 (p17); // pin 17 is declared as an input with the name of LDR1 (this LDR will be in charge to see if there are cars in the middle of the junctions)
alvaro75 0:6cba4a800507 28 DigitalIn LDR2 (p18); // pin 18 is declared as an input with the name of LDR2 (this LDR will observe if there are is any train coming)
alvaro75 0:6cba4a800507 29 DigitalOut myspeaker (LED1); // LED1 is declared as an output with the name of myspeaker as this led will simulate an alarm
alvaro75 0:6cba4a800507 30 DigitalOut LED(LED2); // LED2 is declared as an output with the name of LED2
alvaro75 0:6cba4a800507 31
alvaro75 0:6cba4a800507 32 int alarm=0; // A variable named as "alarm", with an initial value of 0, is defined
alvaro75 0:6cba4a800507 33 int state=1; // A variable named as "state", with an initial value of 1, is defined
alvaro75 0:6cba4a800507 34
alvaro75 0:6cba4a800507 35 void train () { // A new function named as "train" is declared
alvaro75 0:6cba4a800507 36 greenled1=0; // greenled1 is turned off
alvaro75 0:6cba4a800507 37 yellowled1=1; // yellowled1 is turned on
alvaro75 0:6cba4a800507 38 wait(5); // wait for 5 seconds
alvaro75 0:6cba4a800507 39 redled1=1; // redled1 is turned on
alvaro75 0:6cba4a800507 40 yellowled1=0; // yellowled1 is turned off
alvaro75 0:6cba4a800507 41 wait(1); // wait for 1 second
alvaro75 0:6cba4a800507 42 redledp=0; // redledp is turned off
alvaro75 0:6cba4a800507 43 greenledp=1; // greenledp is turned on
alvaro75 0:6cba4a800507 44 wait(3); // wait for 3 seconds
alvaro75 0:6cba4a800507 45 if(LDR1.read()) { // An "if" function is defined, this will make an action if LDR1 detects a lack of light
alvaro75 0:6cba4a800507 46 if(LDR1.read()) { // An "if" function is defined, this will make an action if LDR1 detects a lack of light
alvaro75 0:6cba4a800507 47 alarm= alarm+1; // This comand will increase the value of the variable "alarm" by 1
alvaro75 0:6cba4a800507 48 wait(2); // wait for 2 seconds
alvaro75 0:6cba4a800507 49 }
alvaro75 0:6cba4a800507 50 if(LDR1.read()) { // An "if" function is defined, this will make an action if LDR1 detects a lack of light
alvaro75 0:6cba4a800507 51 alarm= alarm+1; // This comand will increase the value of the variable "alarm" by 1
alvaro75 0:6cba4a800507 52 wait(2); // wait for 2 seconds
alvaro75 0:6cba4a800507 53 }
alvaro75 0:6cba4a800507 54 if(LDR1.read()) { // An "if" function is defined, this will make an action if LDR1 detects a lack of light
alvaro75 0:6cba4a800507 55 alarm= alarm+1; // This comand will increase the value of the variable "alarm" by 1
alvaro75 0:6cba4a800507 56 wait(2); // wait for 2 seconds
alvaro75 0:6cba4a800507 57 }
alvaro75 0:6cba4a800507 58 if(alarm==3) { // An "if" function is defined, this will make an action if the value of the variable "alarm" is equal to 3
alvaro75 0:6cba4a800507 59 myspeaker= 1; // myspeaker1 is turned on
alvaro75 0:6cba4a800507 60 greenled1=0; // greenled1 is turned off
alvaro75 0:6cba4a800507 61 redled1=1; // redled1 is turned on
alvaro75 0:6cba4a800507 62 redledp=1; // redledp is turned on
alvaro75 0:6cba4a800507 63 greenledp=0; // greenledp is turned off
alvaro75 0:6cba4a800507 64 wait(50); // wait for 50 seconds
alvaro75 0:6cba4a800507 65 }
alvaro75 0:6cba4a800507 66 }
alvaro75 0:6cba4a800507 67 }
alvaro75 0:6cba4a800507 68
alvaro75 0:6cba4a800507 69
alvaro75 0:6cba4a800507 70 void pedestrian() { // A new funtion named as "pedestrian" is declared
alvaro75 0:6cba4a800507 71 if (LDR2.read()) { // An "if" function is defined, this will make an action if LDR2 detects a lack of light
alvaro75 0:6cba4a800507 72 train(); // This will do the function "train"
alvaro75 0:6cba4a800507 73 }
alvaro75 0:6cba4a800507 74 greenled1=0; // greenled1 is turned off
alvaro75 0:6cba4a800507 75 LED=0; // LED is turned off
alvaro75 0:6cba4a800507 76 yellowled1=1; // yellowled1 is turned on
alvaro75 0:6cba4a800507 77 wait(5); // Wait for 5 seconds
alvaro75 0:6cba4a800507 78 redled1=1; // redled1 is turned on
alvaro75 0:6cba4a800507 79 wait(2); // Wait for 2 seconds
alvaro75 0:6cba4a800507 80 yellowled1=0; // yellowled1 is turned off
alvaro75 0:6cba4a800507 81 wait(1); // Wait for 1 second
alvaro75 0:6cba4a800507 82 redledp=0; // redledp is turned off
alvaro75 0:6cba4a800507 83 greenledp=1; // greenledp is turned on
alvaro75 0:6cba4a800507 84 wait(5); // Wait for 5 seconds
alvaro75 0:6cba4a800507 85 greenledp=0; // greenledp is turned off
alvaro75 0:6cba4a800507 86 redledp=1; // redledp is turned on
alvaro75 0:6cba4a800507 87 wait(1); // Wait for 1 second
alvaro75 0:6cba4a800507 88 yellowled1=1; // yellowled1 is turned on
alvaro75 0:6cba4a800507 89 wait(1); // wait for 1 second
alvaro75 0:6cba4a800507 90 yellowled1=0; // yellowled1 is turned off
alvaro75 0:6cba4a800507 91 redled1=0; // redled1 is turned off
alvaro75 0:6cba4a800507 92 greenled1=1; // greenled1 is turned on
alvaro75 0:6cba4a800507 93 LED=1; // LED is turned on
alvaro75 0:6cba4a800507 94 wait(1); // wait for 1 second
alvaro75 0:6cba4a800507 95 state= state+1; // This comand will increase the value of the variable "state" by 1
alvaro75 0:6cba4a800507 96 }
alvaro75 0:6cba4a800507 97
alvaro75 0:6cba4a800507 98
alvaro75 0:6cba4a800507 99
alvaro75 0:6cba4a800507 100 int main() {
alvaro75 0:6cba4a800507 101 while(1) { // Command that creates an infinite loop
alvaro75 0:6cba4a800507 102 greenled1=1; // greenled1 is turned on
alvaro75 0:6cba4a800507 103 redledp=1; // redledp is turned on
alvaro75 0:6cba4a800507 104 if(switchstate.read()) { // An "if" function is defined, this will make an action if switch detects it is being pressed
alvaro75 0:6cba4a800507 105 state=state+1; // This comand will increase the value of the variable "state" by 1
alvaro75 0:6cba4a800507 106 }
alvaro75 0:6cba4a800507 107 if (state>1){ // An "if" function is defined, this will make an action if the variable "state" is higher than 1
alvaro75 0:6cba4a800507 108 state=0; // This comand will define the value of the variable "state" to 0
alvaro75 0:6cba4a800507 109 }
alvaro75 0:6cba4a800507 110 if(state==0) { // An "if" function is defined, this will make an action if the value of the variable "state" is 0
alvaro75 0:6cba4a800507 111 pedestrian (); // The function "pedestrian" will be done
alvaro75 0:6cba4a800507 112 }
alvaro75 0:6cba4a800507 113 if(LDR2.read()) { // An "if" function is defined, this will make an action if LDR2 detects a lack of light
alvaro75 0:6cba4a800507 114 train(); // The function "train" will be done
alvaro75 0:6cba4a800507 115 wait(3); // Wait for 3 seconds
alvaro75 0:6cba4a800507 116 redledp=1; // redledp is turned on
alvaro75 0:6cba4a800507 117 greenledp=0; // greenledp is turned off
alvaro75 0:6cba4a800507 118 wait(1); // Wait for 1 second
alvaro75 0:6cba4a800507 119 yellowled1=1; // yellowled1 is turned on
alvaro75 0:6cba4a800507 120 wait(1); // Wait for 1 second
alvaro75 0:6cba4a800507 121 redled1=0; // redled1 is turned off
alvaro75 0:6cba4a800507 122 yellowled1=0; // yellowled1 is turned off
alvaro75 0:6cba4a800507 123 greenled1=1; // greenled1 is turned on
alvaro75 0:6cba4a800507 124 wait(5); // Wait for 5 seconds
alvaro75 0:6cba4a800507 125 }
alvaro75 0:6cba4a800507 126 }
alvaro75 0:6cba4a800507 127 }