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@3:3e1dfbbc8227, 2018-11-20 (annotated)
- Committer:
- JN482
- Date:
- Tue Nov 20 12:59:19 2018 +0000
- Revision:
- 3:3e1dfbbc8227
- Parent:
- 2:37d5e9d92b66
Finalised Traffic-lights;
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 | #include <mbed.h> // importing the mbed library to allow to use input and output fuctions |
JN482 | 2:37d5e9d92b66 | 6 | #include <SevenSegment.h> |
JN482 | 0:41f066ce55cf | 7 | DigitalOut Onboard(LED1);//declaring the onboard led as Onboard |
JN482 | 0:41f066ce55cf | 8 | DigitalOut RLED(D2);//RLED refers to the first traffic light Red |
JN482 | 0:41f066ce55cf | 9 | DigitalOut ALED(D3);//ALED refers to the first traffic light Amber |
JN482 | 0:41f066ce55cf | 10 | DigitalOut GLED(D4);//GLED refers to the first traffic light Green |
JN482 | 0:41f066ce55cf | 11 | DigitalOut RRLED(D5);//RRLED refers to the second traffic light Red |
JN482 | 0:41f066ce55cf | 12 | DigitalOut AALED(D6);//AALED refers to the second traffic light Amber |
JN482 | 0:41f066ce55cf | 13 | DigitalOut GGLED(D7);//GGLED refers to the second traffic light Green |
JN482 | 0:41f066ce55cf | 14 | InterruptIn button (USER_BUTTON); //I will be using the onboard button as a pedestrian button |
JN482 | 0:41f066ce55cf | 15 | InterruptIn Switch(D15);//I will be using an external wire to act as a button. |
JN482 | 2:37d5e9d92b66 | 16 | //DigitalOut Buzzer(D10);//Buzzer |
JN482 | 2:37d5e9d92b66 | 17 | bool pedest = false, unsafe = false; //setting up 2 true or false variables and a integer counter |
JN482 | 2:37d5e9d92b66 | 18 | int counter = 0; |
JN482 | 0:41f066ce55cf | 19 | void pedestrians() |
JN482 | 2:37d5e9d92b66 | 20 | //Declaring a procedure for changing a variable for pedestrians |
JN482 | 0:41f066ce55cf | 21 | { |
JN482 | 0:41f066ce55cf | 22 | pedest = true; |
JN482 | 0:41f066ce55cf | 23 | Onboard = 1; |
JN482 | 0:41f066ce55cf | 24 | } |
JN482 | 0:41f066ce55cf | 25 | void reset() |
JN482 | 2:37d5e9d92b66 | 26 | { |
JN482 | 2:37d5e9d92b66 | 27 | //start reset procedure. |
JN482 | 2:37d5e9d92b66 | 28 | //Declaring a procedure named reset, 0 arguments are passed. |
JN482 | 2:37d5e9d92b66 | 29 | RLED = 0; |
JN482 | 2:37d5e9d92b66 | 30 | ALED = 0; |
JN482 | 2:37d5e9d92b66 | 31 | GLED = 0; |
JN482 | 2:37d5e9d92b66 | 32 | RRLED = 0; |
JN482 | 2:37d5e9d92b66 | 33 | AALED = 0; |
JN482 | 2:37d5e9d92b66 | 34 | GGLED = 0; |
JN482 | 2:37d5e9d92b66 | 35 | Onboard = 0; |
JN482 | 2:37d5e9d92b66 | 36 | ResetSeg(); // calls the resetSeg procedure in the SevenSegment.h |
JN482 | 2:37d5e9d92b66 | 37 | //All Lights are switched off. (Procedure Ends return to main) |
JN482 | 0:41f066ce55cf | 38 | }//End reset procedure. |
JN482 | 1:992086cb4c27 | 39 | void safe() |
JN482 | 2:37d5e9d92b66 | 40 | { |
JN482 | 2:37d5e9d92b66 | 41 | if (unsafe == false) { |
JN482 | 1:992086cb4c27 | 42 | unsafe = true; |
JN482 | 1:992086cb4c27 | 43 | reset(); |
JN482 | 2:37d5e9d92b66 | 44 | } else if(unsafe == true) { |
JN482 | 1:992086cb4c27 | 45 | unsafe = false; |
JN482 | 1:992086cb4c27 | 46 | reset(); |
JN482 | 1:992086cb4c27 | 47 | } |
JN482 | 1:992086cb4c27 | 48 | } |
JN482 | 0:41f066ce55cf | 49 | void sequence(int unsafe) |
JN482 | 0:41f066ce55cf | 50 | // Declaring a procedure for sequence. 2 Arguments are recieved - unsafe and pedestrians |
JN482 | 3:3e1dfbbc8227 | 51 | { |
JN482 | 2:37d5e9d92b66 | 52 | //Start sequence procedure. |
JN482 | 2:37d5e9d92b66 | 53 | if(unsafe == true) { |
JN482 | 2:37d5e9d92b66 | 54 | // Sequence TRAFFIC 1, TRAFFIC 2 |
JN482 | 3:3e1dfbbc8227 | 55 | reset(); |
JN482 | 3:3e1dfbbc8227 | 56 | ResetSeg(); |
JN482 | 2:37d5e9d92b66 | 57 | RLED = 1; // RED ON RED OFF |
JN482 | 2:37d5e9d92b66 | 58 | GGLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 59 | wait(5); // GREEN OFF GREEN ON |
JN482 | 2:37d5e9d92b66 | 60 | //---------------------------------------------- |
JN482 | 2:37d5e9d92b66 | 61 | GGLED = 0; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 62 | ALED = 1; // AMBER OFF AMBER ON |
JN482 | 0:41f066ce55cf | 63 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 2:37d5e9d92b66 | 64 | wait(2); |
JN482 | 2:37d5e9d92b66 | 65 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 66 | RLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 67 | RRLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 68 | ALED = 0; // GREEN ON GREEN OFF |
JN482 | 0:41f066ce55cf | 69 | AALED = 0; |
JN482 | 0:41f066ce55cf | 70 | GLED = 1; |
JN482 | 2:37d5e9d92b66 | 71 | wait(5); |
JN482 | 2:37d5e9d92b66 | 72 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 73 | GLED = 0; // RED OFF RED OFF |
JN482 | 0:41f066ce55cf | 74 | ALED = 1; // AMBER ON AMBER ON |
JN482 | 0:41f066ce55cf | 75 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 2:37d5e9d92b66 | 76 | wait(2); |
JN482 | 2:37d5e9d92b66 | 77 | //---------------------------------------------- |
JN482 | 3:3e1dfbbc8227 | 78 | reset();//This sequence is the same as reset()*/ |
JN482 | 2:37d5e9d92b66 | 79 | } else if(unsafe == false) { //if in safemode run this sequence. |
JN482 | 2:37d5e9d92b66 | 80 | // Sequence TRAFFIC 1, TRAFFIC 2 |
JN482 | 3:3e1dfbbc8227 | 81 | reset(); |
JN482 | 3:3e1dfbbc8227 | 82 | ResetSeg(); |
JN482 | 2:37d5e9d92b66 | 83 | RLED = 1; // RED ON RED OFF |
JN482 | 2:37d5e9d92b66 | 84 | GGLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 85 | wait(5); // GREEN OFF GREEN ON |
JN482 | 2:37d5e9d92b66 | 86 | //---------------------------------------------- |
JN482 | 2:37d5e9d92b66 | 87 | GGLED = 0; // RED ON RED OFF |
JN482 | 0:41f066ce55cf | 88 | // AMBER OFF AMBER ON |
JN482 | 0:41f066ce55cf | 89 | AALED = 1; // GREEN OFF GREEN OFF |
JN482 | 2:37d5e9d92b66 | 90 | wait(2); |
JN482 | 2:37d5e9d92b66 | 91 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 92 | RLED = 1; // RED ON RED ON |
JN482 | 0:41f066ce55cf | 93 | RRLED = 1; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 94 | ALED = 0; // GREEN OFF GREEN OFF |
JN482 | 0:41f066ce55cf | 95 | AALED = 0; |
JN482 | 0:41f066ce55cf | 96 | wait(2); |
JN482 | 2:37d5e9d92b66 | 97 | if (pedest == true) { |
JN482 | 3:3e1dfbbc8227 | 98 | //Buzzer = 1; // Turns on buzzer, however due to the fact that the Nucleo F411RE only has 15 DigitalOut of which 13 is usable as D0 and D1 are reseved pins. |
JN482 | 3:3e1dfbbc8227 | 99 | ResetSeg();nine();wait(1);ResetSeg();eight();wait(1);ResetSeg();seven();wait(1);ResetSeg();six();wait(1);ResetSeg();five();wait(1); |
JN482 | 3:3e1dfbbc8227 | 100 | ResetSeg();four();wait(1);ResetSeg();three();wait(1);ResetSeg();two();wait(1);ResetSeg();one();wait(1);Onboard = 0; ;ResetSeg(); |
JN482 | 2:37d5e9d92b66 | 101 | pedest = false;// The boolean variable is reset back to false |
JN482 | 2:37d5e9d92b66 | 102 | } |
JN482 | 2:37d5e9d92b66 | 103 | //---------------------------------------------- |
JN482 | 1:992086cb4c27 | 104 | GLED = 0; // RED OFF RED OFF |
JN482 | 1:992086cb4c27 | 105 | ALED = 1; // AMBER ON AMBER ON |
JN482 | 1:992086cb4c27 | 106 | wait(2); // GREEN OFF GREEN OFF |
JN482 | 2:37d5e9d92b66 | 107 | //---------------------------------------------- |
JN482 | 3:3e1dfbbc8227 | 108 | RRLED = 1; |
JN482 | 3:3e1dfbbc8227 | 109 | RLED = 0; // RED OFF RED ON |
JN482 | 0:41f066ce55cf | 110 | ALED = 0; // AMBER OFF AMBER OFF |
JN482 | 0:41f066ce55cf | 111 | GLED = 1; // GREEN ON GREEN OFF |
JN482 | 0:41f066ce55cf | 112 | wait(5); |
JN482 | 2:37d5e9d92b66 | 113 | //---------------------------------------------- |
JN482 | 0:41f066ce55cf | 114 | GLED = 0; |
JN482 | 0:41f066ce55cf | 115 | ALED = 1; |
JN482 | 0:41f066ce55cf | 116 | wait(2); |
JN482 | 0:41f066ce55cf | 117 | ALED = 0; |
JN482 | 0:41f066ce55cf | 118 | RLED = 1; |
JN482 | 0:41f066ce55cf | 119 | wait(2); |
JN482 | 0:41f066ce55cf | 120 | AALED = 1; |
JN482 | 0:41f066ce55cf | 121 | wait(2); |
JN482 | 2:37d5e9d92b66 | 122 | reset();//Reset the LEDs |
JN482 | 0:41f066ce55cf | 123 | } |
JN482 | 0:41f066ce55cf | 124 | }//End sequence procedure. |
JN482 | 0:41f066ce55cf | 125 | int main() |
JN482 | 3:3e1dfbbc8227 | 126 | {//start main function. |
JN482 | 0:41f066ce55cf | 127 | //Main procedure, I will create a loop and initalise my variables |
JN482 | 2:37d5e9d92b66 | 128 | /*To create my sequence I need my lights to start on the same lights everytime. |
JN482 | 0:41f066ce55cf | 129 | Therefore I will create a reset which will set all of the lights off. |
JN482 | 0:41f066ce55cf | 130 | I will do this by creating a separate procedure for my main procedure to point to.*/ |
JN482 | 3:3e1dfbbc8227 | 131 | reset();//Main points to reset procedure // This fall procedure in is the button interrupt class |
JN482 | 3:3e1dfbbc8227 | 132 | Switch.fall(&safe); |
JN482 | 3:3e1dfbbc8227 | 133 | button.fall(&pedestrians); //& denotes the that this function is an Interrupt Service Routine |
JN482 | 2:37d5e9d92b66 | 134 | while(1) { |
JN482 | 2:37d5e9d92b66 | 135 | //Start of while |
JN482 | 2:37d5e9d92b66 | 136 | /*infinite loop, the program will never be able to exit this loop because the condition is always TRUE. |
JN482 | 0:41f066ce55cf | 137 | Now that I have set up my infinite loop I can now start the sequence procedure.*/ |
JN482 | 0:41f066ce55cf | 138 | sequence(unsafe);// I am calling the sequence procedure with the arguments unsafe and pedestrians. |
JN482 | 2:37d5e9d92b66 | 139 | }//End of while |
JN482 | 0:41f066ce55cf | 140 | }//End main function |