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@1:992086cb4c27, 2018-10-20 (annotated)
- Committer:
- JN482
- Date:
- Sat Oct 20 21:58:52 2018 +0000
- Revision:
- 1:992086cb4c27
- Parent:
- 0:41f066ce55cf
- Child:
- 2:37d5e9d92b66
Switch needs some tweaking but works for now; SEQ - Done; PED - Done; BUZ - Done
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 | 1:992086cb4c27 | 28 | |
JN482 | 0:41f066ce55cf | 29 | void reset() |
JN482 | 0:41f066ce55cf | 30 | {//start reset procedure. |
JN482 | 0:41f066ce55cf | 31 | //Declaring a procedure named reset, 0 arguments are passed. |
JN482 | 0:41f066ce55cf | 32 | RLED = 0; |
JN482 | 0:41f066ce55cf | 33 | ALED = 0; |
JN482 | 0:41f066ce55cf | 34 | GLED = 0; |
JN482 | 0:41f066ce55cf | 35 | RRLED = 0; |
JN482 | 0:41f066ce55cf | 36 | AALED = 0; |
JN482 | 0:41f066ce55cf | 37 | GGLED = 0; |
JN482 | 0:41f066ce55cf | 38 | //All Lights are switched off. (Procedure Ends return to main) |
JN482 | 0:41f066ce55cf | 39 | }//End reset procedure. |
JN482 | 0:41f066ce55cf | 40 | |
JN482 | 1:992086cb4c27 | 41 | void safe() |
JN482 | 1:992086cb4c27 | 42 | { |
JN482 | 1:992086cb4c27 | 43 | if (unsafe == false) |
JN482 | 1:992086cb4c27 | 44 | { |
JN482 | 1:992086cb4c27 | 45 | unsafe = true; |
JN482 | 1:992086cb4c27 | 46 | reset(); |
JN482 | 1:992086cb4c27 | 47 | } |
JN482 | 1:992086cb4c27 | 48 | else if(unsafe == true) |
JN482 | 1:992086cb4c27 | 49 | { |
JN482 | 1:992086cb4c27 | 50 | unsafe = false; |
JN482 | 1:992086cb4c27 | 51 | reset(); |
JN482 | 1:992086cb4c27 | 52 | } |
JN482 | 1:992086cb4c27 | 53 | } |
JN482 | 0:41f066ce55cf | 54 | |
JN482 | 0:41f066ce55cf | 55 | void sequence(int unsafe) |
JN482 | 0:41f066ce55cf | 56 | // Declaring a procedure for sequence. 2 Arguments are recieved - unsafe and pedestrians |
JN482 | 0:41f066ce55cf | 57 | {//Start sequence procedure. |
JN482 | 0:41f066ce55cf | 58 | if(unsafe == true) |
JN482 | 1:992086cb4c27 | 59 | { |
JN482 | 0:41f066ce55cf | 60 | // Sequence TRAFFIC 1, TRAFFIC 2 |
JN482 | 0:41f066ce55cf | 61 | RLED = 1; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 62 | GGLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 63 | wait(5); // GREEN OFF GREEN ON |
JN482 | 0:41f066ce55cf | 64 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 65 | GGLED = 0; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 66 | ALED = 1; // AMBER OFF AMBER ON |
JN482 | 0:41f066ce55cf | 67 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 68 | wait(2); |
JN482 | 0:41f066ce55cf | 69 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 70 | RLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 71 | RRLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 72 | ALED = 0; // GREEN ON GREEN OFF |
JN482 | 0:41f066ce55cf | 73 | AALED = 0; |
JN482 | 0:41f066ce55cf | 74 | GLED = 1; |
JN482 | 0:41f066ce55cf | 75 | wait(5); |
JN482 | 0:41f066ce55cf | 76 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 77 | GLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 78 | ALED = 1; // AMBER ON AMBER ON |
JN482 | 0:41f066ce55cf | 79 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 80 | wait(2); |
JN482 | 0:41f066ce55cf | 81 | //---------------------------------------------- |
JN482 | 1:992086cb4c27 | 82 | reset(); /*RRLED = 0; // RED OFF RED OFF |
JN482 | 1:992086cb4c27 | 83 | ALED = 0; // AMBER OFF AMBER OFF |
JN482 | 1:992086cb4c27 | 84 | AALED = 0; // GREEN OFF GREEN OFF |
JN482 | 1:992086cb4c27 | 85 | This sequence is the same as reset()*/ |
JN482 | 0:41f066ce55cf | 86 | } |
JN482 | 0:41f066ce55cf | 87 | else if(unsafe == false) |
JN482 | 0:41f066ce55cf | 88 | { |
JN482 | 0:41f066ce55cf | 89 | // Sequence TRAFFIC 1, TRAFFIC 2 |
JN482 | 0:41f066ce55cf | 90 | RLED = 1; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 91 | GGLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 92 | wait(5); // GREEN OFF GREEN ON |
JN482 | 0:41f066ce55cf | 93 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 94 | GGLED = 0; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 95 | // AMBER OFF AMBER ON |
JN482 | 0:41f066ce55cf | 96 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 97 | wait(2); |
JN482 | 0:41f066ce55cf | 98 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 99 | RLED = 1; // RED ON RED ON |
JN482 | 0:41f066ce55cf | 100 | RRLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 101 | ALED = 0; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 102 | AALED = 0; |
JN482 | 0:41f066ce55cf | 103 | wait(2); |
JN482 | 0:41f066ce55cf | 104 | if (pedest == true) |
JN482 | 0:41f066ce55cf | 105 | { |
JN482 | 0:41f066ce55cf | 106 | do // A do while loop, do the following until the condition counter has been met |
JN482 | 0:41f066ce55cf | 107 | { |
JN482 | 0:41f066ce55cf | 108 | GGLED = 1; |
JN482 | 0:41f066ce55cf | 109 | Buzzer = 1; |
JN482 | 0:41f066ce55cf | 110 | GLED = 1; |
JN482 | 0:41f066ce55cf | 111 | wait(0.2); |
JN482 | 0:41f066ce55cf | 112 | GGLED = 0; |
JN482 | 0:41f066ce55cf | 113 | GLED = 0; |
JN482 | 0:41f066ce55cf | 114 | Buzzer = 0; |
JN482 | 0:41f066ce55cf | 115 | wait(0.2); |
JN482 | 0:41f066ce55cf | 116 | counter ++; |
JN482 | 0:41f066ce55cf | 117 | pedest = false; |
JN482 | 0:41f066ce55cf | 118 | Onboard = 0; |
JN482 | 0:41f066ce55cf | 119 | } |
JN482 | 0:41f066ce55cf | 120 | while(counter <= 20);// condition of counter is while the counter is less than or equal to 10 |
JN482 | 0:41f066ce55cf | 121 | } |
JN482 | 0:41f066ce55cf | 122 | //---------------------------------------------- |
JN482 | 1:992086cb4c27 | 123 | GLED = 0; // RED OFF RED OFF |
JN482 | 1:992086cb4c27 | 124 | ALED = 1; // AMBER ON AMBER ON |
JN482 | 1:992086cb4c27 | 125 | wait(2); // GREEN OFF GREEN OFF |
JN482 | 1:992086cb4c27 | 126 | |
JN482 | 0:41f066ce55cf | 127 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 128 | RRLED = 1; // RED OFF RED ON |
JN482 | 0:41f066ce55cf | 129 | RLED = 0; |
JN482 | 0:41f066ce55cf | 130 | ALED = 0; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 131 | GLED = 1; // GREEN ON GREEN OFF |
JN482 | 0:41f066ce55cf | 132 | wait(5); |
JN482 | 0:41f066ce55cf | 133 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 134 | GLED = 0; |
JN482 | 0:41f066ce55cf | 135 | ALED = 1; |
JN482 | 0:41f066ce55cf | 136 | wait(2); |
JN482 | 0:41f066ce55cf | 137 | ALED = 0; |
JN482 | 0:41f066ce55cf | 138 | RLED = 1; |
JN482 | 0:41f066ce55cf | 139 | wait(2); |
JN482 | 0:41f066ce55cf | 140 | AALED = 1; |
JN482 | 0:41f066ce55cf | 141 | wait(2); |
JN482 | 0:41f066ce55cf | 142 | reset(); |
JN482 | 0:41f066ce55cf | 143 | } |
JN482 | 0:41f066ce55cf | 144 | }//End sequence procedure. |
JN482 | 0:41f066ce55cf | 145 | |
JN482 | 0:41f066ce55cf | 146 | int main() |
JN482 | 0:41f066ce55cf | 147 | {//start main function. |
JN482 | 0:41f066ce55cf | 148 | //Main procedure, I will create a loop and initalise my variables |
JN482 | 0:41f066ce55cf | 149 | /*To create my sequence Ivneed my lights to start on the same lights everytime. |
JN482 | 0:41f066ce55cf | 150 | Therefore I will create a reset which will set all of the lights off. |
JN482 | 0:41f066ce55cf | 151 | I will do this by creating a separate procedure for my main procedure to point to.*/ |
JN482 | 0:41f066ce55cf | 152 | |
JN482 | 0:41f066ce55cf | 153 | reset();//Main points to reset procedure |
JN482 | 1:992086cb4c27 | 154 | //button.fall(&pedestrians); // This fall procedure in is the button interrupt class |
JN482 | 1:992086cb4c27 | 155 | Switch.fall(&safe); |
JN482 | 1:992086cb4c27 | 156 | button.fall(&pedestrians); |
JN482 | 0:41f066ce55cf | 157 | while(1) |
JN482 | 0:41f066ce55cf | 158 | {//Start of while |
JN482 | 0:41f066ce55cf | 159 | |
JN482 | 0:41f066ce55cf | 160 | /*infinite loop, the program will never be able to exit this loop because the condition is always TRUE. |
JN482 | 0:41f066ce55cf | 161 | Now that I have set up my infinite loop I can now start the sequence procedure.*/ |
JN482 | 0:41f066ce55cf | 162 | sequence(unsafe);// I am calling the sequence procedure with the arguments unsafe and pedestrians. |
JN482 | 0:41f066ce55cf | 163 | |
JN482 | 0:41f066ce55cf | 164 | }//End of while |
JN482 | 0:41f066ce55cf | 165 | return(0); // The complier warns me that this statement is unreachable, I know. Due the infinite loop above. |
JN482 | 0:41f066ce55cf | 166 | }//End main function |