Charles Tritt / Mbed 2 deprecated TimeoutEx

Dependencies:   mbed

Fork of TW_Ex_9_1 by Charles Tritt

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Project: TimeoutEX
00003     File: main.cpp
00004     
00005     Button interrupt sets global config flag. Timeout clears it. Them main 
00006     program flashes red or green LED based on config status.
00007     
00008     Created by Dr. C. S. Tritt
00009     Last revised: 10/9/17 (v. 1.1)
00010 */
00011 #include "mbed.h"
00012 
00013 void buttonISR(); // Button ISR declaration.
00014 void configOff(); // Configuration mode off declaration.
00015  
00016 InterruptIn myButton(USER_BUTTON); // Button is normally high. Goes low w/press.
00017 Timeout configTime; // TimeOut constructor takes no arguments.
00018 
00019 DigitalOut redLED(D2); // Red and green LED junctions.
00020 DigitalOut grnLED(D3);
00021 
00022 bool config = false; // Configuration mode flag.
00023 
00024 int main() {
00025     redLED = 0; // Turn red & green off at start.
00026     grnLED = 0; 
00027     
00028     myButton.fall(&buttonISR); // Register ISR routine.
00029  
00030     while(true) { // Main loop.
00031         if (config) {
00032             redLED = !redLED; // Toggle red junction.
00033         } else {
00034             grnLED = !grnLED; // Toggle green junction.
00035         }
00036         wait(0.5); // Pause half a second.
00037     }
00038 }
00039 
00040 void buttonISR() { // Sets config status when button falls.
00041     config = true; // Set config status.
00042     grnLED = 0; // Force green junction off.
00043     redLED = 1; // Turn red junction on.
00044     configTime.attach(&configOff, 5.0f); // Register callback. 5 sec. delay.
00045 }
00046 
00047 void configOff() { // Clears config flag after timeout.
00048     config = false; // Clear config flag.
00049     redLED = 0; // Force red junction off.
00050     grnLED = 1; // Turn green junction on.
00051 }