Il pulsante attiva/disattiva il LED/RELÈ senza Debounce (antirimbalzo)
// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int LED_PIN = 3; // Arduino pin connected to LED/RELÈ's pin
// variables will change:
int ledState = LOW; // the current state of LED/RELÈ
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// toggle state of LED/RELÈ
ledState = !ledState;
// control LED/RELÈ arccoding to the toggled state
digitalWrite(LED_PIN, ledState);
}
}
Il pulsante attiva/disattiva (toogle) il LED/RELÈ con Debounce (antirimbalzo)
#include <ezButton.h>
/// constants won't change
const int BUTTON_PIN = 7; // the number of the pushbutton pin
const int LED_PIN = 3; // the number of the LED/RELÈ pin
ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
// variables will change:
int ledState = LOW; // the current state of LED/RELÈ
void setup() {
Serial.begin(9600); // initialize serial
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed()) {
Serial.println("The button is pressed");
// toggle state of LED/RELÈ
ledState = !ledState;
// control LED/RELÈ arccoding to the toggleed sate
digitalWrite(LED_PIN, ledState);
}
}