Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:b18f320ec036, 2015-12-03 (annotated)
- Committer:
- WardieB
- Date:
- Thu Dec 03 10:31:56 2015 +0000
- Revision:
- 0:b18f320ec036
UAE HND;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| WardieB | 0:b18f320ec036 | 1 | // Trafic Light Program |
| WardieB | 0:b18f320ec036 | 2 | // |
| WardieB | 0:b18f320ec036 | 3 | // HND Electronics |
| WardieB | 0:b18f320ec036 | 4 | // |
| WardieB | 0:b18f320ec036 | 5 | // Coleg Gwent |
| WardieB | 0:b18f320ec036 | 6 | |
| WardieB | 0:b18f320ec036 | 7 | #include "mbed.h" |
| WardieB | 0:b18f320ec036 | 8 | |
| WardieB | 0:b18f320ec036 | 9 | |
| WardieB | 0:b18f320ec036 | 10 | //Traffic Lights |
| WardieB | 0:b18f320ec036 | 11 | // R,Y,G R,Y,G |
| WardieB | 0:b18f320ec036 | 12 | BusOut leds(p5,p6,p7,p8,p9,p10); |
| WardieB | 0:b18f320ec036 | 13 | |
| WardieB | 0:b18f320ec036 | 14 | // 7 seg Display |
| WardieB | 0:b18f320ec036 | 15 | BusOut disp1(p11,p12,p13,p14,p15,p16,p17); |
| WardieB | 0:b18f320ec036 | 16 | // A B C D E F G |
| WardieB | 0:b18f320ec036 | 17 | |
| WardieB | 0:b18f320ec036 | 18 | // 7 seg Dispay |
| WardieB | 0:b18f320ec036 | 19 | BusOut disp2(p21,p22,p23,p24,p25,p26,p27); |
| WardieB | 0:b18f320ec036 | 20 | // A B C D E F G |
| WardieB | 0:b18f320ec036 | 21 | |
| WardieB | 0:b18f320ec036 | 22 | |
| WardieB | 0:b18f320ec036 | 23 | //Test Leds - for debugging |
| WardieB | 0:b18f320ec036 | 24 | DigitalOut myLED1(LED1); |
| WardieB | 0:b18f320ec036 | 25 | DigitalOut myLED2(LED2); |
| WardieB | 0:b18f320ec036 | 26 | |
| WardieB | 0:b18f320ec036 | 27 | |
| WardieB | 0:b18f320ec036 | 28 | |
| WardieB | 0:b18f320ec036 | 29 | int valdisp =0; |
| WardieB | 0:b18f320ec036 | 30 | |
| WardieB | 0:b18f320ec036 | 31 | //using MAN74A seven seg display |
| WardieB | 0:b18f320ec036 | 32 | //set up each number in an array |
| WardieB | 0:b18f320ec036 | 33 | int numb[10][7]={{1,1,1,1,1,1,0}, |
| WardieB | 0:b18f320ec036 | 34 | {0,1,1,0,0,0,0}, |
| WardieB | 0:b18f320ec036 | 35 | {1,1,0,1,1,0,1}, |
| WardieB | 0:b18f320ec036 | 36 | {1,1,1,1,0,0,1}, |
| WardieB | 0:b18f320ec036 | 37 | {0,1,1,0,0,1,1}, |
| WardieB | 0:b18f320ec036 | 38 | {1,0,1,1,0,1,1}, |
| WardieB | 0:b18f320ec036 | 39 | {1,0,1,1,1,1,1}, |
| WardieB | 0:b18f320ec036 | 40 | {1,1,1,0,0,0,0}, |
| WardieB | 0:b18f320ec036 | 41 | {1,1,1,1,1,1,1}, |
| WardieB | 0:b18f320ec036 | 42 | {1,1,1,1,0,1,1}}; |
| WardieB | 0:b18f320ec036 | 43 | |
| WardieB | 0:b18f320ec036 | 44 | |
| WardieB | 0:b18f320ec036 | 45 | //Traffic light sequence |
| WardieB | 0:b18f320ec036 | 46 | int nLights[] = {0x09,0x19,0x21,0x11,0x09,0x0B,0x0C,0x0A}; |
| WardieB | 0:b18f320ec036 | 47 | |
| WardieB | 0:b18f320ec036 | 48 | |
| WardieB | 0:b18f320ec036 | 49 | // Switch inputs |
| WardieB | 0:b18f320ec036 | 50 | DigitalIn autoMode(p28); |
| WardieB | 0:b18f320ec036 | 51 | DigitalIn dir(p29); |
| WardieB | 0:b18f320ec036 | 52 | |
| WardieB | 0:b18f320ec036 | 53 | |
| WardieB | 0:b18f320ec036 | 54 | // Analog input to set time |
| WardieB | 0:b18f320ec036 | 55 | AnalogIn timeIn(p19); |
| WardieB | 0:b18f320ec036 | 56 | |
| WardieB | 0:b18f320ec036 | 57 | int state; |
| WardieB | 0:b18f320ec036 | 58 | |
| WardieB | 0:b18f320ec036 | 59 | int waitTime = 0; |
| WardieB | 0:b18f320ec036 | 60 | |
| WardieB | 0:b18f320ec036 | 61 | |
| WardieB | 0:b18f320ec036 | 62 | int main() { |
| WardieB | 0:b18f320ec036 | 63 | while(1) { |
| WardieB | 0:b18f320ec036 | 64 | |
| WardieB | 0:b18f320ec036 | 65 | while (autoMode) { |
| WardieB | 0:b18f320ec036 | 66 | // if in AUTO mode |
| WardieB | 0:b18f320ec036 | 67 | // In AUTO mode sequence through the lights |
| WardieB | 0:b18f320ec036 | 68 | leds = nLights[state]; |
| WardieB | 0:b18f320ec036 | 69 | waitTime = timeIn*99; |
| WardieB | 0:b18f320ec036 | 70 | |
| WardieB | 0:b18f320ec036 | 71 | // display number UNITS |
| WardieB | 0:b18f320ec036 | 72 | for (int i = 0 ;i < 7; i++) |
| WardieB | 0:b18f320ec036 | 73 | { |
| WardieB | 0:b18f320ec036 | 74 | disp2[i]=(numb[waitTime%10][i]); |
| WardieB | 0:b18f320ec036 | 75 | } |
| WardieB | 0:b18f320ec036 | 76 | // display number TENS |
| WardieB | 0:b18f320ec036 | 77 | for (int i = 0 ;i < 7; i++) |
| WardieB | 0:b18f320ec036 | 78 | { |
| WardieB | 0:b18f320ec036 | 79 | disp1[i]=(numb[waitTime/10][i]); |
| WardieB | 0:b18f320ec036 | 80 | } |
| WardieB | 0:b18f320ec036 | 81 | if (state%2 == 0) wait(waitTime/10); //Only long Waits on Even states |
| WardieB | 0:b18f320ec036 | 82 | wait(1); |
| WardieB | 0:b18f320ec036 | 83 | state++; |
| WardieB | 0:b18f320ec036 | 84 | if (state==8) state=0; |
| WardieB | 0:b18f320ec036 | 85 | } |
| WardieB | 0:b18f320ec036 | 86 | |
| WardieB | 0:b18f320ec036 | 87 | // While Auto loop over now in manual |
| WardieB | 0:b18f320ec036 | 88 | { |
| WardieB | 0:b18f320ec036 | 89 | myLED1=autoMode; |
| WardieB | 0:b18f320ec036 | 90 | myLED2=dir; |
| WardieB | 0:b18f320ec036 | 91 | |
| WardieB | 0:b18f320ec036 | 92 | if (!dir){ |
| WardieB | 0:b18f320ec036 | 93 | // Direction 1 |
| WardieB | 0:b18f320ec036 | 94 | wait(1); |
| WardieB | 0:b18f320ec036 | 95 | leds = nLights[1]; //RED, RED AMBER |
| WardieB | 0:b18f320ec036 | 96 | wait(1); |
| WardieB | 0:b18f320ec036 | 97 | leds = nLights[2]; //RED, GREEN |
| WardieB | 0:b18f320ec036 | 98 | // Stay on Green until direction changed or Automode selected |
| WardieB | 0:b18f320ec036 | 99 | while (!dir && !autoMode){} |
| WardieB | 0:b18f320ec036 | 100 | leds = nLights[3]; //RED, RED AMBER |
| WardieB | 0:b18f320ec036 | 101 | wait(1); |
| WardieB | 0:b18f320ec036 | 102 | // set the state to 4 for the AUTO routine |
| WardieB | 0:b18f320ec036 | 103 | state=4; |
| WardieB | 0:b18f320ec036 | 104 | } |
| WardieB | 0:b18f320ec036 | 105 | else |
| WardieB | 0:b18f320ec036 | 106 | { |
| WardieB | 0:b18f320ec036 | 107 | // Direction 2 |
| WardieB | 0:b18f320ec036 | 108 | wait(1); |
| WardieB | 0:b18f320ec036 | 109 | leds = nLights[5]; //RED AMBER, RED |
| WardieB | 0:b18f320ec036 | 110 | wait(1); |
| WardieB | 0:b18f320ec036 | 111 | leds = nLights[6]; //GREEN, GREEN |
| WardieB | 0:b18f320ec036 | 112 | |
| WardieB | 0:b18f320ec036 | 113 | // Stay on Green until direction changed or Automode selected |
| WardieB | 0:b18f320ec036 | 114 | while (dir && !autoMode){} |
| WardieB | 0:b18f320ec036 | 115 | leds = nLights[7]; //RED AMBER, RED |
| WardieB | 0:b18f320ec036 | 116 | wait(1); |
| WardieB | 0:b18f320ec036 | 117 | // set the state to 0 for the AUTO routine |
| WardieB | 0:b18f320ec036 | 118 | state=0; |
| WardieB | 0:b18f320ec036 | 119 | } |
| WardieB | 0:b18f320ec036 | 120 | |
| WardieB | 0:b18f320ec036 | 121 | waitTime = timeIn*99; |
| WardieB | 0:b18f320ec036 | 122 | |
| WardieB | 0:b18f320ec036 | 123 | // display number UNITS |
| WardieB | 0:b18f320ec036 | 124 | for (int i = 0 ;i < 7; i++) |
| WardieB | 0:b18f320ec036 | 125 | { |
| WardieB | 0:b18f320ec036 | 126 | disp2[i]=(numb[waitTime%10][i]); |
| WardieB | 0:b18f320ec036 | 127 | } |
| WardieB | 0:b18f320ec036 | 128 | // display number TENS |
| WardieB | 0:b18f320ec036 | 129 | for (int i = 0 ;i < 7; i++) |
| WardieB | 0:b18f320ec036 | 130 | { |
| WardieB | 0:b18f320ec036 | 131 | disp1[i]=(numb[waitTime/10][i]); |
| WardieB | 0:b18f320ec036 | 132 | } |
| WardieB | 0:b18f320ec036 | 133 | leds = nLights[0]; //Always go to Red Red when not in loop |
| WardieB | 0:b18f320ec036 | 134 | wait(timeIn *9); // wait on Red for road distance wait |
| WardieB | 0:b18f320ec036 | 135 | } |
| WardieB | 0:b18f320ec036 | 136 | |
| WardieB | 0:b18f320ec036 | 137 | } |
| WardieB | 0:b18f320ec036 | 138 | } |