Polling to switch ON/OFF LED depending on Button Status

Dependencies:   mbed

Fork of Amaldi_3_Exercise_LED-Button_Polling by Amaldi

LED-Button-Polling.cpp

Committer:
pinofal
Date:
2018-07-01
Revision:
2:8cd95bea99dc
Parent:
1:832dfb4ba1af

File content as of revision 2:8cd95bea99dc:

// Tested: NUCLE-L476RG

#include "mbed.h"

// crea oggetti Button, LED e serialPC  
DigitalIn myButton(USER_BUTTON);
DigitalOut myLed(LED1);
Serial pc(USBTX, USBRX); 


/********/
/* MAIN */
/********/
int main() 
{
    // imposta velocità della comunicazione con il PC
    pc.baud(921600);
    
    // messaggio di benvenuto
    pc.printf("\r\nHallo Amaldi Students - Exercise 3 \r\n");
  
    //imposta il funzionamento del pulsante come "PullDown": Aperto = '0'. L'altra modalità di funzinamento è PullUp
    myButton.mode(PullDown);
  
    // POLLING: replica sul LED myLED lo stato del pulsante myButton
    while(true) 
    {
        if (myButton == 1) 
        { 
            // Button is pressed
            myLed = 1; // Accendi LED
        }
        else 
        {
            // Button i released
            myLed = 0; // Spegni LED
        }
    }
}