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