#include "mbed.h" //Include the mbed header file which contains the prototypes of the libraries used here (DigitalOut, Timer) as well as various syntax definitions. void output(void); //Function prototype - Must have either whole function or function //prototype declared above main function to avoid errors. DigitalOut R(P1_23); //Create a DigitalOut instance for the Red light. DigitalOut A(P1_19); //Create a DigitalOut instance for the Amber light. DigitalOut G(P1_31); //Create a DigitalOut instance for the Green light. DigitalOut MUX(P1_25); //Create a DigitalOut instance for the multiplexing pin. Timer timer; //Create an ininstance of Timer to be able to use time functions. //Set up integers for the a and b lights, set the time step to 1 and the swap variable which swaps the sequence to the opposite set of lights to 0. int aR, aA, aG, bR, bA, bG, timeStep = 1, swap = 0; int main() { timer.start(); //Start the timer. while(1) { //Start infinite loop for the rest of the program. //If the timer is between 0 and 4 timeSteps, hold red on one side and green on the other. //This same sequence will be used for both sides by swapping which way round the sequence is output. if (timer.read() >= timeStep * 0 && timer.read() < timeStep * 4) { //Set a lights. aR = 1; aA = 0; aG = 0; //Set b lights bR = 0; bA = 0; bG = 1; } //If the timer is between 4 and 5 timeSteps, move green to amber and hold red on the other side. if (timer.read() >= timeStep * 4 && timer.read() < timeStep * 5) { //Set a lights. aR = 1; aA = 0; aG = 0; //Set b lights bR = 0; bA = 1; bG = 0; } //If the timer is between 5 and 6 timeSteps, move amber to red and hold red on the other side. if (timer.read() >= timeStep * 5 && timer.read() < timeStep * 6) { //Set a lights. aR = 1; aA = 0; aG = 0; //Set b lights bR = 1; bA = 0; bG = 0; } //If the timer is between 6 and 7 timeSteps, hold red and move to red and amber on the other side. if (timer.read() >= timeStep * 6 && timer.read() < timeStep * 7) { //Set a lights. aR = 1; aA = 1; aG = 0; //Set b lights bR = 1; bA = 0; bG = 0; } //If the timer is between 7 and 8 timeSteps, restart the process with the sides swapped over as the process from this point is just a repeat of what has already happened. if (timer.read() >= timeStep * 7 && timer.read() < timeStep * 8) { swap = !swap; //Invert the swap variable to swap over the light sets a and b. timer.reset(); //Reset the timer so that the sequence will start from the beginning. } output(); //Call the output function every loop to keep the seven segment displays multiplexed. } } //output function multiplexes the seven segment displays and reverses the lights a and b when necessary. void output (void) { R = A = G = 1; //Set all LED's to off so that the image of one does not transfer to the other when the MUX pin is changed. MUX = !MUX; //Invert the MUX pin to swap which display is used. if (swap) { //If swap is 1 then put a to the left side and b to the right side. switch (MUX) { //Switch on the value of MUX case 0: //If MUX = 0 then output a to the left display. R = !aR; A = !aA; G = !aG; break; case 1: //If MUX = 1 then output b to the right display. R = !bR; A = !bA; G = !bG; break; default: //If MUX does not equal 0 or 1 then put all LED's on. R = A = G = 0; break; } } else { //If swap is not 1 then put a to the right side and b to the left side. switch (MUX) { //Switch on the value of MUX case 0: //If MUX = 0 then output b to the left display. R = !bR; A = !bA; G = !bG; break; case 1: //If MUX = 1 then output a to the right display. R = !aR; A = !aA; G = !aG; break; default: //If MUX does not equal 0 or 1 then put all LED's on. R = A = G = 0; break; } } }