UoD_ME21001_Group_2_03 / Mbed 2 deprecated Finalcode2

Dependencies:   mbed

Revision:
0:6cba4a800507
diff -r 000000000000 -r 6cba4a800507 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 29 18:07:12 2019 +0000
@@ -0,0 +1,127 @@
+/************************************************************
+*                   University of Dundee                    *
+*   ME21001 - Software Applications for Engineering Design  *
+*               Traffic Intersection Project                *
+*                       Group 3                             *
+*                Alvaro Almeida Arencibia                   *
+*                     Adams Boyd                            *
+*                      Qiushuhao                            *
+*                                                           *
+*   This progam will control 4 traffic lights               *
+*   with the intervention of a train                        *
+*                                                           *
+*   For this program we will have 7 leds as outputs and 3   *
+*   inputs; 2 Ldrs and 1 switch                             *
+************************************************************/ 
+
+
+
+#include "mbed.h"                   // We include the library called "mbed.h"
+
+DigitalOut redled1 (p23);           // pin 23 is declared as an output with the name of redled1 (this led wiill be for the cars)
+DigitalOut redledp (p21);           // pin 21 is declared as an output with the name of redledp (this led will be for the pedestrians)
+DigitalOut greenled1 (p24);         // pin 24 is declared as an output with the name of greenled1 (this led will be for the cars)
+DigitalOut greenledp (p27);         // pin 27 is declared as an output with the name of greenledp (this led will be for the pedestrians)
+DigitalOut yellowled1 (p22);        // pin 22 is declared as an output with the name of yellowled1 (this led will be for the cars)
+DigitalIn switchstate (p10);        // pin 10 is declared as an input with the name of switchstate (this switch will be used by the pedestrians)
+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)
+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)
+DigitalOut myspeaker (LED1);        // LED1 is declared as an output with the name of myspeaker as this led will simulate an alarm
+DigitalOut LED(LED2);               // LED2 is declared as an output with the name of LED2
+
+int alarm=0;                        // A variable named as "alarm", with an initial value of 0, is defined
+int state=1;                        // A variable named as "state", with an initial value of 1, is defined
+
+void train () {                     // A new function named as "train" is declared
+ greenled1=0;                       // greenled1 is turned off
+ yellowled1=1;                      // yellowled1 is turned on
+ wait(5);                           // wait for 5 seconds 
+ redled1=1;                         // redled1 is turned on
+ yellowled1=0;                      // yellowled1 is turned off
+ wait(1);                           // wait for 1 second
+ redledp=0;                         // redledp is turned off
+ greenledp=1;                       // greenledp is turned on 
+ wait(3);                           // wait for 3 seconds
+ if(LDR1.read()) {                  // An "if" function is defined, this will make an action if LDR1 detects a lack of light 
+     if(LDR1.read()) {              // An "if" function is defined, this will make an action if LDR1 detects a lack of light 
+         alarm= alarm+1;            // This comand will increase the value of the variable "alarm" by 1
+         wait(2);                   // wait for 2 seconds
+     }
+     if(LDR1.read()) {              // An "if" function is defined, this will make an action if LDR1 detects a lack of light 
+         alarm= alarm+1;            // This comand will increase the value of the variable "alarm" by 1
+         wait(2);                   // wait for 2 seconds
+     }
+     if(LDR1.read()) {              // An "if" function is defined, this will make an action if LDR1 detects a lack of light 
+         alarm= alarm+1;            // This comand will increase the value of the variable "alarm" by 1
+         wait(2);                   // wait for 2 seconds
+     }
+     if(alarm==3) {                 // An "if" function is defined, this will make an action if the value of the variable "alarm" is equal to 3
+         myspeaker= 1;              // myspeaker1 is turned on
+         greenled1=0;               // greenled1 is turned off
+         redled1=1;                 // redled1 is turned on
+         redledp=1;                 // redledp is turned on
+         greenledp=0;               // greenledp is turned off
+         wait(50);                  // wait for 50 seconds 
+     }
+     }
+}
+
+
+void pedestrian() {                 // A new funtion named as "pedestrian" is declared
+ if (LDR2.read()) {                 // An "if" function is defined, this will make an action if LDR2 detects a lack of light 
+     train();                       // This will do the function "train"
+ }
+ greenled1=0;                       // greenled1 is turned off
+ LED=0;                             // LED is turned off
+ yellowled1=1;                      // yellowled1 is turned on
+ wait(5);                           // Wait for 5 seconds
+ redled1=1;                         // redled1 is turned on
+ wait(2);                           // Wait for 2 seconds
+ yellowled1=0;                      // yellowled1 is turned off
+ wait(1);                           // Wait for 1 second
+ redledp=0;                         // redledp is turned off
+ greenledp=1;                       // greenledp is turned on
+ wait(5);                           // Wait for 5 seconds
+ greenledp=0;                       // greenledp is turned off
+ redledp=1;                         // redledp is turned on
+ wait(1);                           // Wait for 1 second
+ yellowled1=1;                      // yellowled1 is turned on
+ wait(1);                           // wait for 1 second
+ yellowled1=0;                      // yellowled1 is turned off
+ redled1=0;                         // redled1 is turned off
+ greenled1=1;                       // greenled1 is turned on
+ LED=1;                             // LED is turned on
+ wait(1);                           // wait for 1 second
+ state= state+1;                    // This comand will increase the value of the variable "state" by 1 
+}
+
+
+
+int main() {
+    while(1) {                      // Command that creates an infinite loop
+     greenled1=1;                   // greenled1 is turned on
+     redledp=1;                     // redledp is turned on
+     if(switchstate.read()) {       // An "if" function is defined, this will make an action if switch detects it is being pressed 
+        state=state+1;              // This comand will increase the value of the variable "state" by 1 
+     }
+     if (state>1){                  // An "if" function is defined, this will make an action if the variable "state" is higher than 1
+         state=0;                   // This comand will define the value of the variable "state" to 0       
+     }
+     if(state==0) {                 // An "if" function is defined, this will make an action if the value of the variable "state" is 0
+        pedestrian ();              // The function "pedestrian" will be done
+     }
+     if(LDR2.read()) {              // An "if" function is defined, this will make an action if LDR2 detects a lack of light 
+        train();                    // The function "train" will be done
+        wait(3);                    // Wait for 3 seconds
+        redledp=1;                  // redledp is turned on
+        greenledp=0;                // greenledp is turned off
+        wait(1);                    // Wait for 1 second
+        yellowled1=1;               // yellowled1 is turned on
+        wait(1);                    // Wait for 1 second
+        redled1=0;                  // redled1 is turned off
+        yellowled1=0;               // yellowled1 is turned off
+        greenled1=1;                // greenled1 is turned on
+        wait(5);                    // Wait for 5 seconds 
+     } 
+}
+}
\ No newline at end of file