Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:41f066ce55cf, 2018-10-20 (annotated)
- Committer:
- JN482
- Date:
- Sat Oct 20 20:24:53 2018 +0000
- Revision:
- 0:41f066ce55cf
- Child:
- 1:992086cb4c27
Traffic light with safe and unsafe sequences. Pedestrian system also working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JN482 | 0:41f066ce55cf | 1 | /* |
JN482 | 0:41f066ce55cf | 2 | This project includes both the safe, unsafe sequences, pedestrian crossing, and a buzzer. |
JN482 | 0:41f066ce55cf | 3 | The Assignment task is to create both the safe and unsafe traffic light sequence. |
JN482 | 0:41f066ce55cf | 4 | */ |
JN482 | 0:41f066ce55cf | 5 | |
JN482 | 0:41f066ce55cf | 6 | #include <mbed.h> // importing the mbed library to allow to use input and output fuctions |
JN482 | 0:41f066ce55cf | 7 | |
JN482 | 0:41f066ce55cf | 8 | DigitalOut Onboard(LED1);//declaring the onboard led as Onboard |
JN482 | 0:41f066ce55cf | 9 | DigitalOut RLED(D2);//RLED refers to the first traffic light Red |
JN482 | 0:41f066ce55cf | 10 | DigitalOut ALED(D3);//ALED refers to the first traffic light Amber |
JN482 | 0:41f066ce55cf | 11 | DigitalOut GLED(D4);//GLED refers to the first traffic light Green |
JN482 | 0:41f066ce55cf | 12 | DigitalOut RRLED(D5);//RRLED refers to the second traffic light Red |
JN482 | 0:41f066ce55cf | 13 | DigitalOut AALED(D6);//AALED refers to the second traffic light Amber |
JN482 | 0:41f066ce55cf | 14 | DigitalOut GGLED(D7);//GGLED refers to the second traffic light Green |
JN482 | 0:41f066ce55cf | 15 | InterruptIn button (USER_BUTTON); //I will be using the onboard button as a pedestrian button |
JN482 | 0:41f066ce55cf | 16 | InterruptIn Switch(D15);//I will be using an external wire to act as a button. |
JN482 | 0:41f066ce55cf | 17 | DigitalOut Buzzer(D10);//Buzzer/Beeper |
JN482 | 0:41f066ce55cf | 18 | |
JN482 | 0:41f066ce55cf | 19 | bool pedest = false, unsafe = false; |
JN482 | 0:41f066ce55cf | 20 | int counter = 0; |
JN482 | 0:41f066ce55cf | 21 | |
JN482 | 0:41f066ce55cf | 22 | void pedestrians() |
JN482 | 0:41f066ce55cf | 23 | //Declaring a procedure for changing a variable |
JN482 | 0:41f066ce55cf | 24 | { |
JN482 | 0:41f066ce55cf | 25 | pedest = true; |
JN482 | 0:41f066ce55cf | 26 | Onboard = 1; |
JN482 | 0:41f066ce55cf | 27 | } |
JN482 | 0:41f066ce55cf | 28 | void reset() |
JN482 | 0:41f066ce55cf | 29 | {//start reset procedure. |
JN482 | 0:41f066ce55cf | 30 | //Declaring a procedure named reset, 0 arguments are passed. |
JN482 | 0:41f066ce55cf | 31 | RLED = 0; |
JN482 | 0:41f066ce55cf | 32 | ALED = 0; |
JN482 | 0:41f066ce55cf | 33 | GLED = 0; |
JN482 | 0:41f066ce55cf | 34 | RRLED = 0; |
JN482 | 0:41f066ce55cf | 35 | AALED = 0; |
JN482 | 0:41f066ce55cf | 36 | GGLED = 0; |
JN482 | 0:41f066ce55cf | 37 | //All Lights are switched off. (Procedure Ends return to main) |
JN482 | 0:41f066ce55cf | 38 | }//End reset procedure. |
JN482 | 0:41f066ce55cf | 39 | |
JN482 | 0:41f066ce55cf | 40 | |
JN482 | 0:41f066ce55cf | 41 | void sequence(int unsafe) |
JN482 | 0:41f066ce55cf | 42 | // Declaring a procedure for sequence. 2 Arguments are recieved - unsafe and pedestrians |
JN482 | 0:41f066ce55cf | 43 | {//Start sequence procedure. |
JN482 | 0:41f066ce55cf | 44 | if(unsafe == true) |
JN482 | 0:41f066ce55cf | 45 | { |
JN482 | 0:41f066ce55cf | 46 | // Sequence TRAFFIC 1, TRAFFIC 2 |
JN482 | 0:41f066ce55cf | 47 | RLED = 1; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 48 | GGLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 49 | wait(5); // GREEN OFF GREEN ON |
JN482 | 0:41f066ce55cf | 50 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 51 | GGLED = 0; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 52 | ALED = 1; // AMBER OFF AMBER ON |
JN482 | 0:41f066ce55cf | 53 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 54 | wait(2); |
JN482 | 0:41f066ce55cf | 55 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 56 | RLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 57 | RRLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 58 | ALED = 0; // GREEN ON GREEN OFF |
JN482 | 0:41f066ce55cf | 59 | AALED = 0; |
JN482 | 0:41f066ce55cf | 60 | GLED = 1; |
JN482 | 0:41f066ce55cf | 61 | wait(5); |
JN482 | 0:41f066ce55cf | 62 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 63 | GLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 64 | ALED = 1; // AMBER ON AMBER ON |
JN482 | 0:41f066ce55cf | 65 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 66 | wait(2); |
JN482 | 0:41f066ce55cf | 67 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 68 | RRLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 69 | ALED = 0; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 70 | AALED = 0; // GREEN OFF GREEN OFF ----------> The same as reset I may replace this with a call to reset |
JN482 | 0:41f066ce55cf | 71 | } |
JN482 | 0:41f066ce55cf | 72 | else if(unsafe == false) |
JN482 | 0:41f066ce55cf | 73 | { |
JN482 | 0:41f066ce55cf | 74 | // Sequence TRAFFIC 1, TRAFFIC 2 |
JN482 | 0:41f066ce55cf | 75 | RLED = 1; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 76 | GGLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 77 | wait(5); // GREEN OFF GREEN ON |
JN482 | 0:41f066ce55cf | 78 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 79 | GGLED = 0; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 80 | // AMBER OFF AMBER ON |
JN482 | 0:41f066ce55cf | 81 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 82 | wait(2); |
JN482 | 0:41f066ce55cf | 83 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 84 | RLED = 1; // RED ON RED ON |
JN482 | 0:41f066ce55cf | 85 | RRLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 86 | ALED = 0; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 87 | AALED = 0; |
JN482 | 0:41f066ce55cf | 88 | wait(2); |
JN482 | 0:41f066ce55cf | 89 | if (pedest == true) |
JN482 | 0:41f066ce55cf | 90 | { |
JN482 | 0:41f066ce55cf | 91 | do // A do while loop, do the following until the condition counter has been met |
JN482 | 0:41f066ce55cf | 92 | { |
JN482 | 0:41f066ce55cf | 93 | GGLED = 1; |
JN482 | 0:41f066ce55cf | 94 | Buzzer = 1; |
JN482 | 0:41f066ce55cf | 95 | GLED = 1; |
JN482 | 0:41f066ce55cf | 96 | wait(0.2); |
JN482 | 0:41f066ce55cf | 97 | GGLED = 0; |
JN482 | 0:41f066ce55cf | 98 | GLED = 0; |
JN482 | 0:41f066ce55cf | 99 | Buzzer = 0; |
JN482 | 0:41f066ce55cf | 100 | wait(0.2); |
JN482 | 0:41f066ce55cf | 101 | counter ++; |
JN482 | 0:41f066ce55cf | 102 | pedest = false; |
JN482 | 0:41f066ce55cf | 103 | Onboard = 0; |
JN482 | 0:41f066ce55cf | 104 | } |
JN482 | 0:41f066ce55cf | 105 | while(counter <= 20);// condition of counter is while the counter is less than or equal to 10 |
JN482 | 0:41f066ce55cf | 106 | } |
JN482 | 0:41f066ce55cf | 107 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 108 | GLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 109 | // AMBER ON AMBER ON |
JN482 | 0:41f066ce55cf | 110 | ALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 111 | wait(2); |
JN482 | 0:41f066ce55cf | 112 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 113 | RRLED = 1; // RED OFF RED ON |
JN482 | 0:41f066ce55cf | 114 | RLED = 0; |
JN482 | 0:41f066ce55cf | 115 | ALED = 0; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 116 | GLED = 1; // GREEN ON GREEN OFF |
JN482 | 0:41f066ce55cf | 117 | wait(5); |
JN482 | 0:41f066ce55cf | 118 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 119 | GLED = 0; |
JN482 | 0:41f066ce55cf | 120 | ALED = 1; |
JN482 | 0:41f066ce55cf | 121 | wait(2); |
JN482 | 0:41f066ce55cf | 122 | ALED = 0; |
JN482 | 0:41f066ce55cf | 123 | RLED = 1; |
JN482 | 0:41f066ce55cf | 124 | wait(2); |
JN482 | 0:41f066ce55cf | 125 | AALED = 1; |
JN482 | 0:41f066ce55cf | 126 | wait(2); |
JN482 | 0:41f066ce55cf | 127 | reset(); |
JN482 | 0:41f066ce55cf | 128 | } |
JN482 | 0:41f066ce55cf | 129 | }//End sequence procedure. |
JN482 | 0:41f066ce55cf | 130 | |
JN482 | 0:41f066ce55cf | 131 | int main() |
JN482 | 0:41f066ce55cf | 132 | {//start main function. |
JN482 | 0:41f066ce55cf | 133 | //Main procedure, I will create a loop and initalise my variables |
JN482 | 0:41f066ce55cf | 134 | bool unsafe = false; |
JN482 | 0:41f066ce55cf | 135 | /*To create my sequence Ivneed my lights to start on the same lights everytime. |
JN482 | 0:41f066ce55cf | 136 | Therefore I will create a reset which will set all of the lights off. |
JN482 | 0:41f066ce55cf | 137 | I will do this by creating a separate procedure for my main procedure to point to.*/ |
JN482 | 0:41f066ce55cf | 138 | |
JN482 | 0:41f066ce55cf | 139 | reset();//Main points to reset procedure |
JN482 | 0:41f066ce55cf | 140 | button.fall(&pedestrians); // This fall procedure in is the button interrupt class |
JN482 | 0:41f066ce55cf | 141 | while(1) |
JN482 | 0:41f066ce55cf | 142 | {//Start of while |
JN482 | 0:41f066ce55cf | 143 | |
JN482 | 0:41f066ce55cf | 144 | /*infinite loop, the program will never be able to exit this loop because the condition is always TRUE. |
JN482 | 0:41f066ce55cf | 145 | Now that I have set up my infinite loop I can now start the sequence procedure.*/ |
JN482 | 0:41f066ce55cf | 146 | sequence(unsafe);// I am calling the sequence procedure with the arguments unsafe and pedestrians. |
JN482 | 0:41f066ce55cf | 147 | |
JN482 | 0:41f066ce55cf | 148 | }//End of while |
JN482 | 0:41f066ce55cf | 149 | return(0); // The complier warns me that this statement is unreachable, I know. Due the infinite loop above. |
JN482 | 0:41f066ce55cf | 150 | }//End main function |