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.
Dependencies: mbed
main.cpp@2:837e1af33ebd, 2019-11-05 (annotated)
- Committer:
- jfair
- Date:
- Tue Nov 05 15:14:42 2019 +0000
- Revision:
- 2:837e1af33ebd
- Parent:
- 1:7b7576c83317
ta dah
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jfair | 0:47a0c4ac5a0f | 1 | #include "mbed.h" |
| jfair | 0:47a0c4ac5a0f | 2 | DigitalOut ga(p23); //defines the green light for first seqeunce |
| jfair | 0:47a0c4ac5a0f | 3 | DigitalOut ya(p22); //defines the yellow light for first sequence |
| jfair | 0:47a0c4ac5a0f | 4 | DigitalOut ra(p21); //defines the red light for first sequence |
| jfair | 0:47a0c4ac5a0f | 5 | DigitalOut gb(p27); //defines green light for second sequence |
| jfair | 0:47a0c4ac5a0f | 6 | DigitalOut yb(p25); //defines yellow light for second sequence |
| jfair | 0:47a0c4ac5a0f | 7 | DigitalOut rb(p24); //defines red light for second sequence |
| jfair | 0:47a0c4ac5a0f | 8 | DigitalIn button(p18); //defines the switch as a digital input |
| jfair | 0:47a0c4ac5a0f | 9 | |
| jfair | 0:47a0c4ac5a0f | 10 | bool cross; |
| jfair | 0:47a0c4ac5a0f | 11 | |
| abogan | 1:7b7576c83317 | 12 | void flag1() { |
| abogan | 1:7b7576c83317 | 13 | if(button.read()) { |
| abogan | 1:7b7576c83317 | 14 | cross = 0; |
| abogan | 1:7b7576c83317 | 15 | } |
| abogan | 1:7b7576c83317 | 16 | } |
| jfair | 0:47a0c4ac5a0f | 17 | void crossing() { |
| jfair | 0:47a0c4ac5a0f | 18 | ra = 1;//simultainiously turns on red lights to allow crossing |
| jfair | 0:47a0c4ac5a0f | 19 | rb = 1; |
| jfair | 0:47a0c4ac5a0f | 20 | ga = 1;//remove later for testing |
| jfair | 0:47a0c4ac5a0f | 21 | wait (1);//r |
| jfair | 0:47a0c4ac5a0f | 22 | ga = 0;//r |
| jfair | 0:47a0c4ac5a0f | 23 | wait (1);//r |
| jfair | 0:47a0c4ac5a0f | 24 | ga = 1;//r |
| jfair | 0:47a0c4ac5a0f | 25 | wait (1);//r |
| abogan | 1:7b7576c83317 | 26 | ga = 0;//r |
| jfair | 0:47a0c4ac5a0f | 27 | cross = 1;//sets value of crossing request to false to prevent looping |
| jfair | 0:47a0c4ac5a0f | 28 | wait (1);//remove later for testing |
| jfair | 0:47a0c4ac5a0f | 29 | } |
| jfair | 0:47a0c4ac5a0f | 30 | //no wait should be required here as the normal lights function begins with a red light, this also minimises disruption |
| jfair | 0:47a0c4ac5a0f | 31 | |
| jfair | 0:47a0c4ac5a0f | 32 | void lightsa() { //creates the function for the first lights sequence |
| jfair | 2:837e1af33ebd | 33 | for (int i=0; i<25; i++) { |
| jfair | 2:837e1af33ebd | 34 | ra = 1; |
| jfair | 2:837e1af33ebd | 35 | flag1 (); |
| jfair | 2:837e1af33ebd | 36 | wait (0.1); |
| jfair | 2:837e1af33ebd | 37 | } |
| jfair | 2:837e1af33ebd | 38 | for (int i=0; i<5; i++) { |
| jfair | 2:837e1af33ebd | 39 | ya = 1; |
| abogan | 1:7b7576c83317 | 40 | flag1(); |
| jfair | 2:837e1af33ebd | 41 | wait (0.1); |
| jfair | 2:837e1af33ebd | 42 | } |
| jfair | 2:837e1af33ebd | 43 | for (int i=0; i<25; i++) { |
| jfair | 0:47a0c4ac5a0f | 44 | ra = 0; //turns off red light |
| jfair | 0:47a0c4ac5a0f | 45 | ya = 0; //turns off yellow light at same time |
| jfair | 0:47a0c4ac5a0f | 46 | ga = 1; //turns on green light at the same time |
| abogan | 1:7b7576c83317 | 47 | flag1(); |
| jfair | 2:837e1af33ebd | 48 | wait (0.1); |
| jfair | 2:837e1af33ebd | 49 | } |
| jfair | 2:837e1af33ebd | 50 | for (int i=0; i<5; i++) { |
| jfair | 0:47a0c4ac5a0f | 51 | ga = 0; // turns off green |
| jfair | 0:47a0c4ac5a0f | 52 | ya = 1; // turns on yellow light at same time |
| abogan | 1:7b7576c83317 | 53 | flag1(); //chechs for switch press |
| jfair | 2:837e1af33ebd | 54 | wait (0.1);//waits 0.5seconds before repeating |
| jfair | 2:837e1af33ebd | 55 | } |
| jfair | 2:837e1af33ebd | 56 | for (int i=0; i<1; i++) { |
| abogan | 1:7b7576c83317 | 57 | ya = 0; //turns off yellow light |
| abogan | 1:7b7576c83317 | 58 | ra = 1; //turns on red light |
| jfair | 2:837e1af33ebd | 59 | flag1(); |
| jfair | 2:837e1af33ebd | 60 | wait (0.1); |
| jfair | 2:837e1af33ebd | 61 | } |
| jfair | 2:837e1af33ebd | 62 | } |
| jfair | 0:47a0c4ac5a0f | 63 | void lightsb() { //creates the function for the second lights sequence |
| jfair | 2:837e1af33ebd | 64 | for (int i=0; i<25; i++) { |
| jfair | 2:837e1af33ebd | 65 | rb = 1; |
| jfair | 2:837e1af33ebd | 66 | flag1 (); |
| jfair | 2:837e1af33ebd | 67 | wait (0.1); |
| jfair | 2:837e1af33ebd | 68 | } |
| jfair | 2:837e1af33ebd | 69 | for (int i=0; i<5; i++) { |
| jfair | 2:837e1af33ebd | 70 | yb = 1; |
| jfair | 2:837e1af33ebd | 71 | flag1(); |
| jfair | 2:837e1af33ebd | 72 | wait (0.1); |
| jfair | 2:837e1af33ebd | 73 | } |
| jfair | 2:837e1af33ebd | 74 | for (int i=0; i<25; i++) { |
| jfair | 0:47a0c4ac5a0f | 75 | rb = 0; //turns off red light |
| jfair | 0:47a0c4ac5a0f | 76 | yb = 0; //turns off yellow light at same time |
| jfair | 0:47a0c4ac5a0f | 77 | gb = 1; //turns on green light at the same time |
| jfair | 2:837e1af33ebd | 78 | flag1(); |
| jfair | 2:837e1af33ebd | 79 | wait (0.1); |
| jfair | 2:837e1af33ebd | 80 | } |
| jfair | 2:837e1af33ebd | 81 | for (int i=0; i<5; i++) { |
| jfair | 0:47a0c4ac5a0f | 82 | gb = 0; // turns off green |
| jfair | 0:47a0c4ac5a0f | 83 | yb = 1; // turns on yellow light at same time |
| jfair | 2:837e1af33ebd | 84 | flag1(); //chechs for switch press |
| jfair | 2:837e1af33ebd | 85 | wait (0.1);//waits 0.5seconds before repeating |
| jfair | 2:837e1af33ebd | 86 | } |
| jfair | 2:837e1af33ebd | 87 | for (int i=0; i<1; i++) { |
| jfair | 0:47a0c4ac5a0f | 88 | yb = 0; //turns off yellow light |
| abogan | 1:7b7576c83317 | 89 | rb = 1; //turns on red light |
| jfair | 2:837e1af33ebd | 90 | flag1(); |
| jfair | 2:837e1af33ebd | 91 | wait (0.1); |
| jfair | 2:837e1af33ebd | 92 | } |
| jfair | 2:837e1af33ebd | 93 | } |
| jfair | 0:47a0c4ac5a0f | 94 | int main() { |
| jfair | 2:837e1af33ebd | 95 | cross = 1; |
| jfair | 0:47a0c4ac5a0f | 96 | while(1) { //repeats indefinitely |
| jfair | 2:837e1af33ebd | 97 | if (cross == 1) { |
| jfair | 2:837e1af33ebd | 98 | rb = 1; |
| jfair | 0:47a0c4ac5a0f | 99 | lightsa(); //calls the function for the first lights sequence |
| jfair | 2:837e1af33ebd | 100 | lightsb(); |
| jfair | 0:47a0c4ac5a0f | 101 | } |
| jfair | 0:47a0c4ac5a0f | 102 | else { |
| jfair | 0:47a0c4ac5a0f | 103 | crossing (); |
| jfair | 0:47a0c4ac5a0f | 104 | } |
| jfair | 0:47a0c4ac5a0f | 105 | } |
| jfair | 0:47a0c4ac5a0f | 106 | } |