UoD_ME21001_Group_1_12 / Mbed 2 deprecated 00xtrafficlightmulti

Dependencies:   mbed

main.cpp

Committer:
jfair
Date:
2019-11-19
Revision:
0:b8a0da291a83

File content as of revision 0:b8a0da291a83:

#include "mbed.h"
DigitalOut ga(p23); //defines the green light for first seqeunce 
DigitalOut ya(p22); //defines the yellow light for first sequence 
DigitalOut ra(p21); //defines the red light for first sequence
DigitalOut gb(p27); //defines green light for second sequence
DigitalOut yb(p25); //defines yellow light for second sequence
DigitalOut rb(p24); //defines red light for second sequence
DigitalIn buttona(p18); //defines the switch as a digital input
DigitalIn buttonb(p17);
DigitalOut led(LED1);

bool crossa;
bool crossb;

void flaga() {
    if(buttona.read()) {
        crossa = 0;
    }
}
void flagb() {
    if(buttonb.read()) {
        crossb = 0;
    }
}
void crossingfull() {
    ra = 1;//simultainiously turns on red lights to allow crossing 
    rb = 1;
    led = 1;
    crossa = 1;//sets value of crossing request to false to prevent looping
    crossb = 1; 
    wait (1);//remove later for testing
    led = 0;
    }   
void crossinga() {
    ra = 1;//turns on red lights to allow crossing 
    led = 1;
    crossa = 1;//sets value of crossing request to false to prevent looping 
    wait (1);//remove later for testing
    led = 0;
    }
void crossingb() {
    rb = 1;
    led = 1;
    crossb = 1;//sets value of crossing request to false to prevent looping 
    wait (1);//remove later for testing
    led = 0;
    }      
//no wait should be required here as the normal lights function begins with a red light, this also minimises disruption

void lightsa() { //creates the function for the first lights sequence 
         for (int i=0; i<25; i++) {
         ra = 1;
         flaga ();
         wait (0.1);
         }
         for (int i=0; i<5; i++) {
         ya = 1;
         flaga();
         wait (0.1);
         }
         for (int i=0; i<25; i++) {
         ra = 0; //turns off red light
         ya = 0; //turns off yellow light at same time
         ga = 1; //turns on green light at the same time
         flaga();
         wait (0.1);
         }
         for (int i=0; i<5; i++) {
         ga = 0; // turns off green 
         ya = 1; // turns on yellow light at same time
         flaga(); //chechs for switch press
         wait (0.1);//waits 0.5seconds before repeating
         }
         for (int i=0; i<1; i++) { 
         ya = 0; //turns off yellow light
         ra = 1; //turns on red light
         flaga();
         wait (0.1);
         }
}
void lightsb() { //creates the function for the second lights sequence 
         for (int i=0; i<25; i++) {
         rb = 1;
         flagb ();
         wait (0.1);
         }
         for (int i=0; i<5; i++) {
         yb = 1;
         flagb();
         wait (0.1);
         }
         for (int i=0; i<25; i++) {
         rb = 0; //turns off red light
         yb = 0; //turns off yellow light at same time
         gb = 1; //turns on green light at the same time
         flagb();
         wait (0.1);
         }
         for (int i=0; i<5; i++) {
         gb = 0; // turns off green 
         yb = 1; // turns on yellow light at same time
         flagb(); //chechs for switch press
         wait (0.1);//waits 0.5seconds before repeating
         }
         for (int i=0; i<1; i++) { 
         yb = 0; //turns off yellow light
         rb = 1; //turns on red light
         flagb();
         wait (0.1);
         }
}  
int main() {
    crossa = 1;
    crossb = 1;
    while(1) { //repeats indefinitely
    if (crossa == 1 && crossb == 1) {
        rb = 1; 
        lightsa(); //calls the function for the first lights sequence
        lightsb();
        }
    else if (crossa == 0 && crossb == 1) {
        crossinga ();
        }
    else if (crossa == 1 && crossb == 0) {
        crossingb ();
        }
    else if (crossa == 0 && crossb == 0) {
        crossingfull ();
        }
    }
}