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
00001 #include "mbed.h" 00002 00003 // Use buttons to select alarm time. Cycle through hours in an incrementing 00004 // fashion using button1, hit select and increment through minutes. Hit 00005 // select one more time to begin the alarm timer. 00006 // 00007 // The Time LEDs will blink in time with the button inputs to show the 00008 // currently selected alarm time. Once select is hit a second time to begin 00009 // the timer, the LEDs will blink out the configured delay in hours and 00010 // minutes before going into a low power sleep mode. 00011 // 00012 // Once the alarm fires, hitting the select button will turn the alarm off 00013 // until the next time it fires. 00014 //__________________________________________________________________________ 00015 // You may also use the RTC (hardware or software through the Time API) to 00016 // set a real world time and set an alarm for a specific timestamp rather 00017 // than on a delay. This would require manually setting the time on each 00018 // reset however, or an internet connection to automatically collect the 00019 // time. See Example <TODO>[HERE] for how to do that. 00020 00021 // Time constants in seconds 00022 #define HOUR 60 * 60 00023 #define MINUTE 60 00024 00025 // Globals 00026 DigitalOut alarm_out(D2, 0); 00027 DigitalOut alarm_led(LED_RED, 1); 00028 DigitalOut hour_led(LED_GREEN, 1); 00029 DigitalOut min_led(LED_BLUE, 1); 00030 00031 InterruptIn inc_time(BUTTON1); 00032 InterruptIn select(BUTTON2); 00033 00034 LowPowerTicker alarm_event; 00035 00036 volatile uint64_t delay = 0; 00037 volatile uint8_t hour_count = 0; 00038 volatile uint8_t min_count = 0; 00039 volatile uint8_t select_state = 0; 00040 00041 // Timer Callbacks 00042 void inc_select(void) { 00043 if (select_state < 2) { 00044 select_state++; 00045 } else { 00046 // Use select button to disable alarm 00047 alarm_out = 0; 00048 alarm_led = 1; 00049 } 00050 } 00051 00052 void set_time_leds(void) { 00053 if (select_state == 0) { 00054 hour_led = !hour_led; 00055 } else { 00056 min_led = !min_led; 00057 } 00058 } 00059 00060 void inc_delay(void) { 00061 if (select_state == 0) { 00062 delay += HOUR; 00063 hour_count++; 00064 hour_led = !hour_led; 00065 } else { 00066 delay += MINUTE; 00067 min_count++; 00068 min_led = !min_led; 00069 } 00070 } 00071 00072 void trigger_alarm_out(void) { 00073 alarm_out = 1; 00074 alarm_led = 0; 00075 } 00076 00077 // Main thread 00078 int main() { 00079 // Configure interrupt-in pins (button controls) 00080 select.rise(inc_select); 00081 inc_time.fall(set_time_leds); 00082 inc_time.rise(inc_delay); 00083 00084 // Sleep while waiting for user input to set the desired delay 00085 while (select_state < 2) { wait_ms(10); } 00086 00087 // Once the delay has been input, blink back the configured hours and 00088 // minutes selected 00089 for (uint8_t i = 0; i < hour_count * 2; i++) { 00090 hour_led = !hour_led; 00091 wait(0.25f); 00092 } 00093 00094 for (uint8_t i = 0; i < min_count * 2; i++) { 00095 min_led = !min_led; 00096 wait(0.25f); 00097 } 00098 00099 // Attach the low power ticker with the configured alarm delay 00100 alarm_event.attach(&trigger_alarm_out, delay); 00101 00102 // Sleep in the main thread 00103 while (1) { wait(100000); } 00104 }
Generated on Mon Jul 18 2022 01:08:31 by
1.7.2