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.
Dependencies: mbed
Fork of Amaldi_4_Exercise_LED-Button_Interrupt by
Revision 2:932543952437, committed 2018-05-07
- Comitter:
- pinofal
- Date:
- Mon May 07 07:14:03 2018 +0000
- Parent:
- 1:cb693ddbc9b2
- Commit message:
- Amaldi 4 Exercise
Changed in this revision
LED-Button-Interrupt.cpp | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED-Button-Interrupt.cpp Mon May 07 07:14:03 2018 +0000 @@ -0,0 +1,53 @@ +#include "mbed.h" + +// inizializza oggetti +InterruptIn myButton(USER_BUTTON); +DigitalOut myLed(LED1); + +// dichiara variabile. Inizialmente delay lunghissimo +double dDelay = 2000; // 2 sec + + +/*************************************************************/ +/* Routine di gestione Interrupt associata al button pressed */ +/*************************************************************/ +void pressedIRQ() +{ + // assegna valore alla variabile dDelay che sarà poi usata nel Main per generare un ritardo + dDelay = 100; // 100 ms +} + +/**************************************************************/ +/* Routine di gestione Interrupt associata al button released */ +/**************************************************************/ +void releasedIRQ() +{ + // assegna valore alla variabile dDelay che sarà poi usata nel Main per generare un ritardo + dDelay = 500; // 500 ms +} + + +/********/ +/* MAIN */ +/********/ +int main() +{ + + // Associa routine di Interrup all'evento Button premuto + myButton.fall(&pressedIRQ); // Ogni volta che premo Button viene richiamata la routine pressedIRQ + // Associa routine di Interrup all'evento Button rilasciato + myButton.rise(&releasedIRQ); + + // POLLING: accende/spegne il LED con un ritardo diverso a seconda che sia premuto/rilasciato il pulsante + while (true) + { + // accende/spegne il LED + myLed = !myLed; + + // Il delay dipende dalla posizione Pressed/Released del pulsante + wait_ms(dDelay); + + // posso aumentare la precisione del ritardo impostandolo in usec + //wait_us(dDelay); + } +}
--- a/main.cpp Fri Jul 07 06:42:11 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#include "mbed.h" - -InterruptIn button(USER_BUTTON); - -DigitalOut led(LED1); - -double delay = 0.5; // 500 ms - -void pressed() -{ - delay = 0.1; // 100 ms -} - -void released() -{ - delay = 0.5; // 500 ms -} - -int main() -{ - // Assign functions to button - button.fall(&pressed); - button.rise(&released); - - while (1) { - led = !led; - wait(delay); - } -}