So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By katappa
#74999 Hi,

I am new to ESP8266 Node MCU.
I am trying to write simple GPIO Interrupt code.

I want to trigger interrupt when pin D1 is driven low, and change toggle on-board led. However, my ISR isn't getting triggered. Can someone please point out my mistake?

Code: Select all
#include <string.h>

#define BLUE_LED_PIN    16
#define GPIO_D1         1

unsigned char state = 0;
unsigned short int interrupt_count = 0;


void callback()
{
  if (state == 0)
  {
      digitalWrite(BLUE_LED_PIN, LOW);
      Serial.print("LED ON ");
      state = 1;
  }
  else
  {
      digitalWrite(BLUE_LED_PIN, HIGH);
      Serial.print("LED OFF ");
      state = 0;
  }
  interrupt_count++;
  Serial.print(interrupt_count);
  Serial.print(" ");

}

void setup()
{
  pinMode(BLUE_LED_PIN, OUTPUT);
  Serial.begin(115200);
  pinMode(GPIO_D1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(GPIO_D1), callback, LOW);
//  interrupts();
  Serial.print("Counter=");
  Serial.print(interrupt_count);
  Serial.print("\n");
  Serial.print("waiting 4 interrupt ");
}


void loop()
{
//  Serial.print(digitalRead(GPIO_D1));
//  Serial.print("  ");
}