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@7:667534ecd34e, 2020-11-05 (annotated)
- Committer:
- harerimana
- Date:
- Thu Nov 05 14:14:43 2020 +0000
- Revision:
- 7:667534ecd34e
- Parent:
- 6:14626bbcb236
- Child:
- 8:3dd7a4231f6e
traffic tasks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harerimana | 7:667534ecd34e | 1 | #include "mbed.h" // Include the library of Mbed |
harerimana | 7:667534ecd34e | 2 | #include "C12832.h" // Include the library of the specific LCD |
harerimana | 7:667534ecd34e | 3 | |
harerimana | 7:667534ecd34e | 4 | DigitalOut yellowled(p5, 1); // Initializing the yellow light ON (same as yellowled = 1) |
harerimana | 7:667534ecd34e | 5 | DigitalOut redled(p7, 0); // Initializing the Red light to OFF (same as redled = 0) |
harerimana | 7:667534ecd34e | 6 | InterruptIn pedestrianbutton(p6); |
harerimana | 7:667534ecd34e | 7 | |
harerimana | 7:667534ecd34e | 8 | C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); // create an instance of LCD |
harerimana | 7:667534ecd34e | 9 | |
harerimana | 7:667534ecd34e | 10 | int state = 0; // State variable indicating that YELLOW LIGHT IS ON |
harerimana | 7:667534ecd34e | 11 | |
harerimana | 7:667534ecd34e | 12 | char pedestrianAdvert[] = "2 COFFEES FOR THE PRICE OF 1. ONLY AT CAMELIA"; |
harerimana | 7:667534ecd34e | 13 | char carDriverAdvert[] = "RECEIVE 5%% REDUCTION ON YOUR FULL TANK ...."; |
harerimana | 7:667534ecd34e | 14 | |
harerimana | 7:667534ecd34e | 15 | #define YELLOWBLINKINGDURATION 10 |
harerimana | 7:667534ecd34e | 16 | #define REDLIGHTONDURATION 15 |
harerimana | 7:667534ecd34e | 17 | |
harerimana | 7:667534ecd34e | 18 | |
harerimana | 7:667534ecd34e | 19 | /** |
harerimana | 7:667534ecd34e | 20 | * Blinking a yellow LED. |
harerimana | 7:667534ecd34e | 21 | * |
harerimana | 7:667534ecd34e | 22 | * @param duration length of time to blink . |
harerimana | 7:667534ecd34e | 23 | */ |
harerimana | 7:667534ecd34e | 24 | static void yellowblinking(int duration){ |
harerimana | 7:667534ecd34e | 25 | int counter=0; |
harerimana | 7:667534ecd34e | 26 | while(counter < duration) |
harerimana | 0:392dea3408f2 | 27 | { |
harerimana | 7:667534ecd34e | 28 | counter+=1; |
harerimana | 7:667534ecd34e | 29 | yellowled = !yellowled; |
harerimana | 7:667534ecd34e | 30 | wait(1); |
harerimana | 7:667534ecd34e | 31 | } |
harerimana | 7:667534ecd34e | 32 | yellowled = 0; |
harerimana | 7:667534ecd34e | 33 | } |
harerimana | 7:667534ecd34e | 34 | |
harerimana | 7:667534ecd34e | 35 | /** |
harerimana | 7:667534ecd34e | 36 | * Crossing pedestrian. |
harerimana | 7:667534ecd34e | 37 | * |
harerimana | 7:667534ecd34e | 38 | * @param duration length of time for pedestrian to cross. |
harerimana | 7:667534ecd34e | 39 | */ |
harerimana | 7:667534ecd34e | 40 | static void pedestriancrossing(int duration){ |
harerimana | 7:667534ecd34e | 41 | redled = 1; |
harerimana | 7:667534ecd34e | 42 | int counter=0; |
harerimana | 7:667534ecd34e | 43 | while(counter < duration) |
harerimana | 7:667534ecd34e | 44 | { |
harerimana | 7:667534ecd34e | 45 | counter+=1; |
harerimana | 7:667534ecd34e | 46 | wait(1); |
harerimana | 7:667534ecd34e | 47 | } |
harerimana | 7:667534ecd34e | 48 | redled = 0; |
harerimana | 7:667534ecd34e | 49 | yellowled = 1; |
harerimana | 7:667534ecd34e | 50 | } |
harerimana | 7:667534ecd34e | 51 | |
harerimana | 7:667534ecd34e | 52 | /** |
harerimana | 7:667534ecd34e | 53 | * Display content on LCD. |
harerimana | 7:667534ecd34e | 54 | * |
harerimana | 7:667534ecd34e | 55 | * @param advert The advertising text to be displayed. |
harerimana | 7:667534ecd34e | 56 | */ |
harerimana | 7:667534ecd34e | 57 | static void display(char *advert) { |
harerimana | 7:667534ecd34e | 58 | lcd.cls(); // Clear LCD |
harerimana | 7:667534ecd34e | 59 | lcd.locate(10, 5); // get cursor to position x=3px and y=5px |
harerimana | 7:667534ecd34e | 60 | lcd.printf(advert); // Write text into LCD buffer |
harerimana | 7:667534ecd34e | 61 | lcd.copy_to_lcd(); |
harerimana | 5:e1afaceb1196 | 62 | } |
harerimana | 5:e1afaceb1196 | 63 | |
harerimana | 5:e1afaceb1196 | 64 | |
harerimana | 7:667534ecd34e | 65 | /** |
harerimana | 7:667534ecd34e | 66 | * Callback (Interrupt Service Routine) when a bouton is clicked. |
harerimana | 7:667534ecd34e | 67 | * |
harerimana | 7:667534ecd34e | 68 | */ |
harerimana | 7:667534ecd34e | 69 | void pedestrian_isr() { |
harerimana | 7:667534ecd34e | 70 | wait(1); |
harerimana | 7:667534ecd34e | 71 | if (state == 0) // YELLOW IS ON AND RED IS OFF |
harerimana | 7:667534ecd34e | 72 | { |
harerimana | 7:667534ecd34e | 73 | state = 1; // YELLOW IS BLINKING AND RED IS OFF |
harerimana | 7:667534ecd34e | 74 | display(pedestrianAdvert); |
harerimana | 7:667534ecd34e | 75 | yellowblinking(YELLOWBLINKINGDURATION); |
harerimana | 7:667534ecd34e | 76 | wait(1); |
harerimana | 7:667534ecd34e | 77 | state = 2; // YELLOW IS OFF AND RED IS ON |
harerimana | 7:667534ecd34e | 78 | display(carDriverAdvert); |
harerimana | 7:667534ecd34e | 79 | pedestriancrossing(REDLIGHTONDURATION); |
harerimana | 7:667534ecd34e | 80 | state = 0; |
harerimana | 7:667534ecd34e | 81 | lcd.cls(); |
harerimana | 7:667534ecd34e | 82 | } |
harerimana | 7:667534ecd34e | 83 | if(state == 2) // RED |
harerimana | 7:667534ecd34e | 84 | { |
harerimana | 7:667534ecd34e | 85 | // extend the duration of RED by 5 seconds |
harerimana | 7:667534ecd34e | 86 | } |
harerimana | 7:667534ecd34e | 87 | } |
harerimana | 7:667534ecd34e | 88 | |
harerimana | 7:667534ecd34e | 89 | int main() { |
harerimana | 7:667534ecd34e | 90 | pedestrianbutton.rise(&pedestrian_isr); // Registering an ISR for button click |
harerimana | 7:667534ecd34e | 91 | while (1) { // DO FOREVER |
harerimana | 7:667534ecd34e | 92 | wait(1); |
harerimana | 7:667534ecd34e | 93 | } |
harerimana | 7:667534ecd34e | 94 | } |
harerimana | 7:667534ecd34e | 95 | |
harerimana | 7:667534ecd34e | 96 | |
harerimana | 7:667534ecd34e | 97 |