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.
Fork of TimeoutEx by
Diff: main.cpp
- Revision:
- 4:587825079f11
- Parent:
- 3:439f58e558e5
- Child:
- 5:307135bd050f
diff -r 439f58e558e5 -r 587825079f11 main.cpp --- a/main.cpp Mon Oct 09 15:51:08 2017 +0000 +++ b/main.cpp Wed Oct 11 01:59:34 2017 +0000 @@ -1,51 +1,81 @@ /* - Project: TimeoutEX + Project: TrafficEx File: main.cpp - Button interrupt sets global config flag. Timeout clears it. Them main - program flashes red or green LED based on config status. + Control a hypothetical traffic light using Timeouts. Display a Violation + message if light is run (using polling in main). + + Car active high input (D6) is created by placing the photocell on the low + side of a voltage divider created using a series of 1 kOhm resistors (the + number depends on ambient conditions). Three worked well for me. + + The red, yellow and green LEDs are connected to pins D2, D3 and D4, + respectively through 330 Ohm resistors. They are active high. Created by Dr. C. S. Tritt - Last revised: 10/9/17 (v. 1.1) + Last revised: 10/10/17 (v. 1.0) */ #include "mbed.h" -void buttonISR(); // Button ISR declaration. -void configOff(); // Configuration mode off declaration. +const float grnTime = 3.0f; // Green duration. +const float ylwTime = 2.0f; // Yellow duration. +const float redTime = 4.0f; // Red duration. + +// These functions change to the indicated color and schedule the next change. +void grnChange(); +void ylwChange(); +void redChange(); -InterruptIn myButton(USER_BUTTON); // Button is normally high. Goes low w/press. -Timeout configTime; // TimeOut constructor takes no arguments. +Timeout changeLight; // Reuse for all colors. -DigitalOut redLED(D2); // Red and green LED junctions. -DigitalOut grnLED(D3); +// Red, yellow and green LEDs. +DigitalOut redLight(D2); +DigitalOut ylwLight(D3); +DigitalOut grnLight(D4); -bool config = false; // Configuration mode flag. +// Analog signal used as digital input. Threshold is about 1.6 V. +DigitalIn car(D6); int main() { - redLED = 0; // Turn red & green off at start. - grnLED = 0; + + redLight = 1; // Start with red on. Seems safest. + grnLight = 0; + ylwLight = 0; - myButton.fall(&buttonISR); // Register ISR routine. - + redChange(); // Call redChange to start cycling. + + // redChange will return here almost immediately (after scheduling + // grnChange). + while(true) { // Main loop. - if (config) { - redLED = !redLED; // Toggle red junction. + // Display RYG status, stay on line. Okay to use printf in main. + printf("RYG: %d %d %d. ", (int) redLight, (int) ylwLight, + (int) grnLight); + // Display car status, stay on line. + printf("Car: %d.", (int) car); + if ((int) car && (int) redLight) { + printf(" Violation!\n"); // Note violation. } else { - grnLED = !grnLED; // Toggle green junction. - } + printf("\n"); // Just advance a line. + } wait(0.5); // Pause half a second. } } -void buttonISR() { // Sets config status when button falls. - config = true; // Set config status. - grnLED = 0; // Force green junction off. - redLED = 1; // Turn red junction on. - configTime.attach(&configOff, 5.0f); // Register callback. 5 sec. delay. +void grnChange(){ + redLight = 0; // Turn off red. + grnLight = 1; // Turn on green. + changeLight.attach(&ylwChange, grnTime); // Schedule change to yellow. } -void configOff() { // Clears config flag after timeout. - config = false; // Clear config flag. - redLED = 0; // Force red junction off. - grnLED = 1; // Turn green junction on. +void ylwChange(){ + grnLight = 0; // Turn off grn. + ylwLight = 1; // Turn on yellow. + changeLight.attach(&redChange, ylwTime); // Schedule change to red. +} + +void redChange(){ + ylwLight = 0; // Turn off yellow. + redLight = 1; // Turn on red. + changeLight.attach(&grnChange, redTime); // Schedule change to green. } \ No newline at end of file