MTbutton: Example 1
Event management for Uno, Nano, Mega
Light a LED on a press button
The program will turn on the LED_BUILTIN led when pressing a button wired between A0 and GND.
Complete program recommended
This program is completely under interruption, and releases loop which can be used to do something else.
// The program will turn on the LED_BUILTIN led when pressing a button.
#include <MTobjects.h> // V1.0.6 See http://arduino.dansetrad.fr/en/MTobjects
const uint8_t PIN_BUTTON = A0; // Button wired between A0 and GND
void light(void) // Called when you just press the button
{
digitalWrite(LED_BUILTIN, HIGH); // Light the LED
}
void turnOff(void) // Called when you have just released the button
{
digitalWrite(LED_BUILTIN, LOW); // Turn off the LED
}
MTbutton Bouton(PIN_BUTTON, light, turnOff); // Implementation of the button
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // LED initialization
}
void loop(){}
Complete program possible
It is a more traditional method but you should not block loop otherwise the button has no more effect.
// The program will turn on the LED_BUILTIN led when pressing a button.
#include <MTobjects.h> // V1.0.6 See http://arduino.dansetrad.fr/en/MTobjects
const uint8_t PIN_BUTTON = A0; // Button wired between A0 and GND
MTbutton Bouton(PIN_BUTTON); // Implementation of the button
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // LED initialization
}
void loop()
{
digitalWrite(LED_BUILTIN, Bouton.getSelect()); // Copies the state of the button to the LED
// digitalWrite(LED_BUILTIN, Bouton.getSelect()? HIGH : LOW) ; // More correct!
}