finalbuild

Dependencies:   mbed

Committer:
jfair
Date:
Tue Nov 19 15:10:27 2019 +0000
Revision:
0:b8a0da291a83
Child:
1:bd2414fc0fdc
multiple crossing sequence ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jfair 0:b8a0da291a83 1 #include "mbed.h"
jfair 0:b8a0da291a83 2 DigitalOut ga(p23); //defines the green light for first seqeunce
jfair 0:b8a0da291a83 3 DigitalOut ya(p22); //defines the yellow light for first sequence
jfair 0:b8a0da291a83 4 DigitalOut ra(p21); //defines the red light for first sequence
jfair 0:b8a0da291a83 5 DigitalOut gb(p27); //defines green light for second sequence
jfair 0:b8a0da291a83 6 DigitalOut yb(p25); //defines yellow light for second sequence
jfair 0:b8a0da291a83 7 DigitalOut rb(p24); //defines red light for second sequence
jfair 0:b8a0da291a83 8 DigitalIn buttona(p18); //defines the switch as a digital input
jfair 0:b8a0da291a83 9 DigitalIn buttonb(p17);
jfair 0:b8a0da291a83 10 DigitalOut led(LED1);
jfair 0:b8a0da291a83 11
jfair 0:b8a0da291a83 12 bool crossa;
jfair 0:b8a0da291a83 13 bool crossb;
jfair 0:b8a0da291a83 14
jfair 0:b8a0da291a83 15 void flaga() {
jfair 0:b8a0da291a83 16 if(buttona.read()) {
jfair 0:b8a0da291a83 17 crossa = 0;
jfair 0:b8a0da291a83 18 }
jfair 0:b8a0da291a83 19 }
jfair 0:b8a0da291a83 20 void flagb() {
jfair 0:b8a0da291a83 21 if(buttonb.read()) {
jfair 0:b8a0da291a83 22 crossb = 0;
jfair 0:b8a0da291a83 23 }
jfair 0:b8a0da291a83 24 }
jfair 0:b8a0da291a83 25 void crossingfull() {
jfair 0:b8a0da291a83 26 ra = 1;//simultainiously turns on red lights to allow crossing
jfair 0:b8a0da291a83 27 rb = 1;
jfair 0:b8a0da291a83 28 led = 1;
jfair 0:b8a0da291a83 29 crossa = 1;//sets value of crossing request to false to prevent looping
jfair 0:b8a0da291a83 30 crossb = 1;
jfair 0:b8a0da291a83 31 wait (1);//remove later for testing
jfair 0:b8a0da291a83 32 led = 0;
jfair 0:b8a0da291a83 33 }
jfair 0:b8a0da291a83 34 void crossinga() {
jfair 0:b8a0da291a83 35 ra = 1;//turns on red lights to allow crossing
jfair 0:b8a0da291a83 36 led = 1;
jfair 0:b8a0da291a83 37 crossa = 1;//sets value of crossing request to false to prevent looping
jfair 0:b8a0da291a83 38 wait (1);//remove later for testing
jfair 0:b8a0da291a83 39 led = 0;
jfair 0:b8a0da291a83 40 }
jfair 0:b8a0da291a83 41 void crossingb() {
jfair 0:b8a0da291a83 42 rb = 1;
jfair 0:b8a0da291a83 43 led = 1;
jfair 0:b8a0da291a83 44 crossb = 1;//sets value of crossing request to false to prevent looping
jfair 0:b8a0da291a83 45 wait (1);//remove later for testing
jfair 0:b8a0da291a83 46 led = 0;
jfair 0:b8a0da291a83 47 }
jfair 0:b8a0da291a83 48 //no wait should be required here as the normal lights function begins with a red light, this also minimises disruption
jfair 0:b8a0da291a83 49
jfair 0:b8a0da291a83 50 void lightsa() { //creates the function for the first lights sequence
jfair 0:b8a0da291a83 51 for (int i=0; i<25; i++) {
jfair 0:b8a0da291a83 52 ra = 1;
jfair 0:b8a0da291a83 53 flaga ();
jfair 0:b8a0da291a83 54 wait (0.1);
jfair 0:b8a0da291a83 55 }
jfair 0:b8a0da291a83 56 for (int i=0; i<5; i++) {
jfair 0:b8a0da291a83 57 ya = 1;
jfair 0:b8a0da291a83 58 flaga();
jfair 0:b8a0da291a83 59 wait (0.1);
jfair 0:b8a0da291a83 60 }
jfair 0:b8a0da291a83 61 for (int i=0; i<25; i++) {
jfair 0:b8a0da291a83 62 ra = 0; //turns off red light
jfair 0:b8a0da291a83 63 ya = 0; //turns off yellow light at same time
jfair 0:b8a0da291a83 64 ga = 1; //turns on green light at the same time
jfair 0:b8a0da291a83 65 flaga();
jfair 0:b8a0da291a83 66 wait (0.1);
jfair 0:b8a0da291a83 67 }
jfair 0:b8a0da291a83 68 for (int i=0; i<5; i++) {
jfair 0:b8a0da291a83 69 ga = 0; // turns off green
jfair 0:b8a0da291a83 70 ya = 1; // turns on yellow light at same time
jfair 0:b8a0da291a83 71 flaga(); //chechs for switch press
jfair 0:b8a0da291a83 72 wait (0.1);//waits 0.5seconds before repeating
jfair 0:b8a0da291a83 73 }
jfair 0:b8a0da291a83 74 for (int i=0; i<1; i++) {
jfair 0:b8a0da291a83 75 ya = 0; //turns off yellow light
jfair 0:b8a0da291a83 76 ra = 1; //turns on red light
jfair 0:b8a0da291a83 77 flaga();
jfair 0:b8a0da291a83 78 wait (0.1);
jfair 0:b8a0da291a83 79 }
jfair 0:b8a0da291a83 80 }
jfair 0:b8a0da291a83 81 void lightsb() { //creates the function for the second lights sequence
jfair 0:b8a0da291a83 82 for (int i=0; i<25; i++) {
jfair 0:b8a0da291a83 83 rb = 1;
jfair 0:b8a0da291a83 84 flagb ();
jfair 0:b8a0da291a83 85 wait (0.1);
jfair 0:b8a0da291a83 86 }
jfair 0:b8a0da291a83 87 for (int i=0; i<5; i++) {
jfair 0:b8a0da291a83 88 yb = 1;
jfair 0:b8a0da291a83 89 flagb();
jfair 0:b8a0da291a83 90 wait (0.1);
jfair 0:b8a0da291a83 91 }
jfair 0:b8a0da291a83 92 for (int i=0; i<25; i++) {
jfair 0:b8a0da291a83 93 rb = 0; //turns off red light
jfair 0:b8a0da291a83 94 yb = 0; //turns off yellow light at same time
jfair 0:b8a0da291a83 95 gb = 1; //turns on green light at the same time
jfair 0:b8a0da291a83 96 flagb();
jfair 0:b8a0da291a83 97 wait (0.1);
jfair 0:b8a0da291a83 98 }
jfair 0:b8a0da291a83 99 for (int i=0; i<5; i++) {
jfair 0:b8a0da291a83 100 gb = 0; // turns off green
jfair 0:b8a0da291a83 101 yb = 1; // turns on yellow light at same time
jfair 0:b8a0da291a83 102 flagb(); //chechs for switch press
jfair 0:b8a0da291a83 103 wait (0.1);//waits 0.5seconds before repeating
jfair 0:b8a0da291a83 104 }
jfair 0:b8a0da291a83 105 for (int i=0; i<1; i++) {
jfair 0:b8a0da291a83 106 yb = 0; //turns off yellow light
jfair 0:b8a0da291a83 107 rb = 1; //turns on red light
jfair 0:b8a0da291a83 108 flagb();
jfair 0:b8a0da291a83 109 wait (0.1);
jfair 0:b8a0da291a83 110 }
jfair 0:b8a0da291a83 111 }
jfair 0:b8a0da291a83 112 int main() {
jfair 0:b8a0da291a83 113 crossa = 1;
jfair 0:b8a0da291a83 114 crossb = 1;
jfair 0:b8a0da291a83 115 while(1) { //repeats indefinitely
jfair 0:b8a0da291a83 116 if (crossa == 1 && crossb == 1) {
jfair 0:b8a0da291a83 117 rb = 1;
jfair 0:b8a0da291a83 118 lightsa(); //calls the function for the first lights sequence
jfair 0:b8a0da291a83 119 lightsb();
jfair 0:b8a0da291a83 120 }
jfair 0:b8a0da291a83 121 else if (crossa == 0 && crossb == 1) {
jfair 0:b8a0da291a83 122 crossinga ();
jfair 0:b8a0da291a83 123 }
jfair 0:b8a0da291a83 124 else if (crossa == 1 && crossb == 0) {
jfair 0:b8a0da291a83 125 crossingb ();
jfair 0:b8a0da291a83 126 }
jfair 0:b8a0da291a83 127 else if (crossa == 0 && crossb == 0) {
jfair 0:b8a0da291a83 128 crossingfull ();
jfair 0:b8a0da291a83 129 }
jfair 0:b8a0da291a83 130 }
jfair 0:b8a0da291a83 131 }