#include "mbed.h" DigitalIn sens1(p17); DigitalOut sensreceiver1(p19); DigitalOut sensled1(p19); DigitalOut traffledred1(p12); DigitalOut traffledgreen1(p14); DigitalIn sens2(p21); DigitalOut sensled2(p23); DigitalOut sensreceiver2(p22); DigitalOut traffledred2(p10); DigitalOut traffledgreen2(p16); Serial bthc05serial(p28,p27); int btstate = 0; //variable btstae to represent output of Bluetooth Timer timer1,timer2; //setting up 2 timers.. int breaknest= 0; //for break in double loop (nest) //a while in while loop class Traffic_Light{ //creating a class for traffic light 1 //Generalizing the attribiute of d traffic light system using pointers public: DigitalIn *sensor; DigitalIn *pullsensor; //To pull out of loop eg (Tl1.sensorpull = &sense2;) DigitalOut *Glight; //The traffic light can display green light//using pointers to the output pins DigitalOut *Rlight; // The traffic light can display Red lights void turn_red(); //creating a function to turn the traffic light red void turn_green(); //creating a function to turn the traffic light green void sequence(); //creating a function for when the light switching green or red int breaknest; //to break from nest }; Traffic_Light Tl1, Tl2; //creating an object for traffic light 1 & 2 int main() { bthc05serial.baud(9600);//the bit rate at which data is sent from the microncroller to d usrt char recieved_byte; //creating a char named recieved_byte which is used for the bluetooth input   bool controllable = 0; //create a true/fasle condition (bool) controllable and uncontrollabel state for BT sensled1 = 1; //turning the infrared led on sensreceiver1 = 1; //turning the infrared reciever on sensled2 = 1; //turning the infrared led on sensreceiver2 = 1; //turning the infrared reciever on //Referecing attribute of class TrafficLight to Tl1 and Tl2 Tl1.sensor = &sens1; //when traffic light 1 detects vehicle Tl1.pullsensor = &sens2; //to break out of trafficlight1 loop when t2 sense2 detects vehicle Tl1.Glight = &traffledgreen1; //turn traffic light 1 green Tl1.Rlight = &traffledred1; // turn traffic light one red Tl2.sensor = &sens2; //when traffic light 2 detetcs vehicle Tl2.pullsensor = &sens1; //to break out of traffic2 light loop when vehicle has been deteced by at sensor 1 Tl2.Glight = &traffledgreen2; Tl2.Rlight = &traffledred2; while (1){ //creating a while loop so the sensors are always updating, when the program is running //making a stand by procedure where the light are constantly switching between red and green light if ((sens1 == 1 && sens2 == 1)&& btstate == 0){ //only starts the standby procedure if both receivers are receiving the IR lights and state is 0 Tl1.turn_green(); Tl2.turn_red(); btstate++; //turn the btstate to 1.bluetooth now active and can be operated manually } if(bthc05serial.readable()){ //creating an if statement for when the bluetooth is being used recieved_byte = bthc05serial.getc(); //naming the input from bluetooth as recieved_byte if(recieved_byte == 'x'){controllable = 1;} //when a x is sent from bluetooth the system turns controllable while(controllable == 1){ //creating the while loop for the controlls of the system, while it is controllable if(bthc05serial.readable()){ //if the bluetooth reads a character ,then get the char in hex or dec recieved_byte = bthc05serial.getc(); if(recieved_byte == 's'){//if the input is an s , both lights turns red   Tl1.turn_red(); Tl2.turn_red(); } if(recieved_byte == '1'){//if input is 1, tr1 turns green and tr2 turns red Tl1.turn_green(); Tl2.turn_red(); } if(recieved_byte == '2'){//if input is 2, tr2 turns green and tr1 turns red Tl1.turn_green(); Tl2.turn_red(); } if(recieved_byte == 'X'){//if input is X, becomes uncontrollable again controllable = 0; btstate = 0; break; } } } } Tl1.sequence(); //start the traffic light sequences fucntion Tl2.sequence(); } } //creating the funciton outside the class Trafficlight void Traffic_Light::turn_red(){ //truns trafficlight Red for both traffic light and displays on BT) *Glight = 0; *Rlight = 1; if(traffledred1 == 1){ bthc05serial.printf("TRAFFIC LIGHT 1 IS RED.\n");} if(traffledred2 == 1){ bthc05serial.printf("TRAFFIC LIGHT 2 IS RED.\n");} } void Traffic_Light::turn_green(){ // green for both trafficlights) *Glight = 1; *Rlight = 0; if(traffledgreen1 == 1){ bthc05serial.printf("TRAFFIC LIGHT 1 IS GREEN<3){ timer1.stop(); timer1.reset(); break;} } breaknest = 0;//resetting the breakflag to 0 turn_green();//the tr1 turns green while (1){ if (*pullsensor == 0){ //while tr1 is green, if a car arrives to tr2 will start a timer for tr1, after 10 seconds tr1 will turn red and will break out of the loops in this sequence timer2.start(); while(1){ if (timer2.read()>10){ timer2.stop(); timer2.reset(); breaknest++; break; } if (*sensor == 1){ //if there are no cars coming from tr1 during the 10 second timer, it will not wait for the 10 second timer to finish timer2.stop(); timer2.reset(); break; } } } if (breaknest == 1 && *pullsensor == 0){break;}//if breakflag is 1 and reciever 2 detects a car, break out of the sequence if (*sensor == 1 ){//if reciever one does not detects a car wait three seconds, then turn the red lights on for three seconds then break out of this loop   timer1.start(); while(1){ if(timer1.read()>3 && timer1.read()<6){// wait for 3 seconds to detect if there is any car coming behind the last one detected. if this timer would not be there, if a car would go through the greenlight it will instantly turn red after it left if(btstate == 0){ turn_red(); btstate++; } } if(timer1.read()>6){ //wait 3 seconds before starting the sequence with the other light aswell, this is a safety feature, so the cars can pass the junction before the other light turns green timer1.stop(); timer1.reset(); breaknest++; break; } } } if (breaknest == 1 && *sensor ==1){//if breakflag is 1 and reciever 1 is not detecting a car, break from the loop btstate =0; break; } } } }