I'm using ezbutton to make a pushbutton act like a toggle switch, first press doing permanent on, second press doing permanent off. I'm just trying to understand the basic coding. This project is just a testbed for what I'm planning to do later - wiring vehicle toggle switches to Arduino and feeding the output signal to mosfet that will control the load state. So I'm trying to get the code as nice and tidy as I possible can. In the code below, I would like to know if ledState will be applicable when this sketch will control a mosfet instead of a test led, and I've seen comments saying "ledState = !ledState;" is not optimal - but being a beginner I'm not sure what this means, and is there a better more sound way to address it. Thanks!
#include <ezButton.h>
// ---
// Declaration
// ---
const int button1 = 9;
const int led1 = 4;
int ledState = LOW;
ezButton button(button1);
// ---
// Setup
// ---
void setup()
{
pinMode(led1, OUTPUT);
pinMode(button1, INPUT_PULLUP);
button.setDebounceTime(50);
}
// ***
// Main Loop
// ***
void loop()
{
button.loop();
if (button.isPressed()) {
ledState = !ledState;
digitalWrite(led1, ledState);
}
}