UoD_ME21001_Group_1_12 / Mbed 2 deprecated 0fullfunctionlights

Dependencies:   mbed

main.cpp

Committer:
abogan
Date:
2019-11-05
Revision:
1:7b7576c83317
Parent:
0:47a0c4ac5a0f
Child:
2:837e1af33ebd

File content as of revision 1:7b7576c83317:

#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 button(p18); //defines the switch as a digital input

bool cross;

void flag1() {
    if(button.read()) {
        cross = 0;
        wait (0.5);
    }
}
void crossing() {
    ra = 1;//simultainiously turns on red lights to allow crossing 
    rb = 1;
    ga = 1;//remove later for testing
    wait (1);//r
    ga = 0;//r
    wait (1);//r
    ga = 1;//r
    wait (1);//r
    ga = 0;//r
    cross = 1;//sets value of crossing request to false to prevent looping 
    wait (1);//remove later for testing
    }   
//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 
         ra = 1; //turns on red light at same time
         rb = 1;
         flag1();
         wait(2.5);//waits 2.5 seconds
         ya = 1; //turns the yellow light on 
         flag1();
         wait(0.5);//waits 0.5 seconds
         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
         flag1();
         wait (2.5); //waits 2.5 seconds
         ga = 0; // turns off green 
         ya = 1; // turns on yellow light at same time
         flag1(); //chechs for switch press
         wait (0.5);//waits 0.5seconds before repeating
         ya = 0; //turns off yellow light
         ra = 1; //turns on red light
    }
void lightsb() { //creates the function for the second lights sequence 
         rb = 1; //turns on red light at same time
         wait(10); //waits ten seconds
         yb = 1; //turns the yellow light on 
         wait(2); //waits two seconds
         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
         wait (14); //waits fourteen seconds
         gb = 0; // turns off green 
         yb = 1; // turns on yellow light at same time
         wait (3); //waits three  
         yb = 0; //turns off yellow light
         rb = 1; //turns on red light
    }
    
int main() {
    while(1) { //repeats indefinitely
    if (cross == 1) { 
        lightsa(); //calls the function for the first lights sequence
        }
    else {
        crossing ();
        }
    }
}