A sleep (or deepSleep) example with button interrupt wakeup

Dependencies:   mbed

main.cpp

Committer:
jose_23991
Date:
2014-09-08
Revision:
0:4d80b159d444

File content as of revision 0:4d80b159d444:

#include "mbed.h"
 
InterruptIn event_button(USER_BUTTON);  // Create the button event object
volatile bool go_to_sleep = false;      // Volatile variable to make the system sleep
 
void ISR_pressed()                      // ISR for the button press
{
    printf("Button pressed\n");         // Show that the button has pressed
    go_to_sleep = !go_to_sleep;         // Toogle the sleep state
    event_button.disable_irq();         // Disable the interrupt request
}
 
int main()
{
    int i = 0;
    
    event_button.mode(PullUp);          // Setup the internall pull-up resistor
    event_button.fall(&ISR_pressed);    // Set the ISR associated to event fall (push the button)
 
    while(1)
    {
        if(go_to_sleep)
        {
            printf("%d: Entering sleep (press user button to resume)\n", i);
            
            event_button.enable_irq();  // Enable the interrupt request
            //sleep();                  // Enter Low Power Mode
            deepsleep();                // Enter Low Power Mode (deep)
            wait_ms(200);               // Wait 200ms for debounce
            event_button.enable_irq();  // Enable the interrupt request
        }
        else
        {
            printf("%d: Running\n", i); // Show the counter
        }
        i++;
    }
}