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@0:47a0c4ac5a0f, 2019-10-29 (annotated)
- Committer:
- jfair
- Date:
- Tue Oct 29 16:45:51 2019 +0000
- Revision:
- 0:47a0c4ac5a0f
- Child:
- 1:7b7576c83317
a
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 | |
jfair | 0:47a0c4ac5a0f | 12 | void crossing() { |
jfair | 0:47a0c4ac5a0f | 13 | ra = 1;//simultainiously turns on red lights to allow crossing |
jfair | 0:47a0c4ac5a0f | 14 | rb = 1; |
jfair | 0:47a0c4ac5a0f | 15 | ga = 1;//remove later for testing |
jfair | 0:47a0c4ac5a0f | 16 | wait (1);//r |
jfair | 0:47a0c4ac5a0f | 17 | ga = 0;//r |
jfair | 0:47a0c4ac5a0f | 18 | wait (1);//r |
jfair | 0:47a0c4ac5a0f | 19 | ga = 1;//r |
jfair | 0:47a0c4ac5a0f | 20 | wait (1);//r |
jfair | 0:47a0c4ac5a0f | 21 | ga = 1;//r |
jfair | 0:47a0c4ac5a0f | 22 | cross = 1;//sets value of crossing request to false to prevent looping |
jfair | 0:47a0c4ac5a0f | 23 | wait (1);//remove later for testing |
jfair | 0:47a0c4ac5a0f | 24 | } |
jfair | 0:47a0c4ac5a0f | 25 | //no wait should be required here as the normal lights function begins with a red light, this also minimises disruption |
jfair | 0:47a0c4ac5a0f | 26 | |
jfair | 0:47a0c4ac5a0f | 27 | void lightsa() { //creates the function for the first lights sequence |
jfair | 0:47a0c4ac5a0f | 28 | ra = 1; //turns on red light at same time |
jfair | 0:47a0c4ac5a0f | 29 | wait(3); //waits ten seconds |
jfair | 0:47a0c4ac5a0f | 30 | ya = 1; //turns the yellow light on |
jfair | 0:47a0c4ac5a0f | 31 | wait(1); //waits two seconds |
jfair | 0:47a0c4ac5a0f | 32 | ra = 0; //turns off red light |
jfair | 0:47a0c4ac5a0f | 33 | ya = 0; //turns off yellow light at same time |
jfair | 0:47a0c4ac5a0f | 34 | ga = 1; //turns on green light at the same time |
jfair | 0:47a0c4ac5a0f | 35 | wait (3); //waits fourteen seconds |
jfair | 0:47a0c4ac5a0f | 36 | ga = 0; // turns off green |
jfair | 0:47a0c4ac5a0f | 37 | ya = 1; // turns on yellow light at same time |
jfair | 0:47a0c4ac5a0f | 38 | wait (1); //waits three seconds before repeating |
jfair | 0:47a0c4ac5a0f | 39 | ya = 0; //turns off yellow light |
jfair | 0:47a0c4ac5a0f | 40 | } |
jfair | 0:47a0c4ac5a0f | 41 | void lightsb() { //creates the function for the second lights sequence |
jfair | 0:47a0c4ac5a0f | 42 | rb = 1; //turns on red light at same time |
jfair | 0:47a0c4ac5a0f | 43 | wait(10); //waits ten seconds |
jfair | 0:47a0c4ac5a0f | 44 | yb = 1; //turns the yellow light on |
jfair | 0:47a0c4ac5a0f | 45 | wait(2); //waits two seconds |
jfair | 0:47a0c4ac5a0f | 46 | rb = 0; //turns off red light |
jfair | 0:47a0c4ac5a0f | 47 | yb = 0; //turns off yellow light at same time |
jfair | 0:47a0c4ac5a0f | 48 | gb = 1; //turns on green light at the same time |
jfair | 0:47a0c4ac5a0f | 49 | wait (14); //waits fourteen seconds |
jfair | 0:47a0c4ac5a0f | 50 | gb = 0; // turns off green |
jfair | 0:47a0c4ac5a0f | 51 | yb = 1; // turns on yellow light at same time |
jfair | 0:47a0c4ac5a0f | 52 | wait (3); //waits three |
jfair | 0:47a0c4ac5a0f | 53 | yb = 0; //turns off yellow light |
jfair | 0:47a0c4ac5a0f | 54 | } |
jfair | 0:47a0c4ac5a0f | 55 | |
jfair | 0:47a0c4ac5a0f | 56 | int main() { |
jfair | 0:47a0c4ac5a0f | 57 | while(1) { //repeats indefinitely |
jfair | 0:47a0c4ac5a0f | 58 | if (button.read()) { |
jfair | 0:47a0c4ac5a0f | 59 | cross = 0; |
jfair | 0:47a0c4ac5a0f | 60 | } |
jfair | 0:47a0c4ac5a0f | 61 | if (cross == 1) { |
jfair | 0:47a0c4ac5a0f | 62 | rb = 1; //keeps red light from second sequence on during sequence one |
jfair | 0:47a0c4ac5a0f | 63 | lightsa(); //calls the function for the first lights sequence |
jfair | 0:47a0c4ac5a0f | 64 | ra = 1; //keeps red light for sequence one on during sequence two |
jfair | 0:47a0c4ac5a0f | 65 | } |
jfair | 0:47a0c4ac5a0f | 66 | else { |
jfair | 0:47a0c4ac5a0f | 67 | crossing (); |
jfair | 0:47a0c4ac5a0f | 68 | } |
jfair | 0:47a0c4ac5a0f | 69 | } |
jfair | 0:47a0c4ac5a0f | 70 | } |