Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By vtraveller
#30960 I'm not sure on the circuit, but the posted code on my NodeMCU 0.9 wasn't quite right. As far as I can tell pin16 is multiplexed for both the button and the LED.

If the button is active then the LED is off until the button is pressed.
If the LED is active the button is disabled and the LED is on.

There's clearly one more combination, as at boot up the button doesn't illuminate the LED. I've not however figured out that combination but I'm thinking an internal pull-up/drain setting on the GPIO. Maybe there is a relationship with pin #1; regardless, I could not get the LED to flash if the button was active.

I therefore went for this combination:

Code: Select allconst int buttonPin = 16;

void button(bool enabled)
{
  pinMode(buttonPin, OUTPUT);
  digitalWrite(buttonPin, enabled ? HIGH : LOW);
  if (enabled) pinMode(buttonPin, INPUT);
}

void flash()
{
  for (int i = 0; i < 5; i++)
  {
    button(false);  // Toggle button makes LED go on/off
    delay(100);
    button(true);   // Toggle button makes LED go on/off
    delay(100);
  }
}

void setup()
{
  button(true);
  flash();
}

bool wait_for_release = false;

void loop()
{
  if (digitalRead(buttonPin) == HIGH)
  {
    wait_for_release = true;
  }
  else if (wait_for_release)
  {
    wait_for_release = false;

    flash();
  }
}