UoD_ME21001_Group_1_12 / Mbed 2 deprecated 0fullfunctionlights

Dependencies:   mbed

Committer:
abogan
Date:
Tue Nov 05 14:09:38 2019 +0000
Revision:
1:7b7576c83317
Parent:
0:47a0c4ac5a0f
Child:
2:837e1af33ebd
flags

Who changed what in which revision?

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