Interrupts on NodeMCU
Posted: Mon Nov 13, 2017 4:35 am
Hi Guys, I am quite new to nodeMCU and have been experimenting it with Arduino IDE. Recently I have been trying out the interrupt functions. I have written a simple code to test interrupt, when during normal operation, 3 LEDS (red, green, yellow) blink in sequence. Once interrupt is activated, another LED (blue) will blink once and return to normal operation of blinking the other LEDs.
Upon executing, the LEDS blink as per normal, when i press the switch to trigger a falling edge interrupt, the blue LED blinks and holds there while the blinking sequence continues. Upon repeated tries of pressing the switch, one time I was able to stop the sequence and enter blink the blue LED. But the LED was on for almost 8s even though i had specified 1s. I was not able to create this scenario again.
I have attached my code below,
In the hardware, I have a switch connected externally to D7 and pulled up to 3.3V.
int yellow = D2; // declare yellow ledPin as variable
int green = D3; // declare green ledPin as variable
int red = D4; // declare red ledPin as variable
int blue = D5; // declare blue ledPin as variable
void setup()
{
// put your setup code here, to run once:
pinMode(yellow,OUTPUT); //configure ledPin as OUTPUT
pinMode(green,OUTPUT); //configure ledPin as OUTPUT
pinMode(red,OUTPUT); //configure ledPin as OUTPUT
pinMode(blue,OUTPUT); //configure ledPin as OUTPUT
pinMode(SW,INPUT); //configure switch as INPUT
attachInterrupt(digitalPinToInterrupt(SW), interruptISR, FALLING);
}
void loop()
{
digitalWrite(yellow, HIGH); // turn on yellow LED
delay(1000); // delay 1s
digitalWrite(yellow, LOW); // turn off yellow LED
digitalWrite(green, HIGH); // turn on green LED
delay(1000); // delay 1s
digitalWrite(green, LOW); // turn off green LED
digitalWrite(red, HIGH); // turn on red LED
delay(1000); // delay 1s
digitalWrite(red, LOW); // turn off red LED
}
void interruptISR() {
digitalWrite(blue, HIGH); // turn on blue LED
delay(500); // delay 1s
digitalWrite(blue, LOW); // turn off blue LED
}
Upon executing, the LEDS blink as per normal, when i press the switch to trigger a falling edge interrupt, the blue LED blinks and holds there while the blinking sequence continues. Upon repeated tries of pressing the switch, one time I was able to stop the sequence and enter blink the blue LED. But the LED was on for almost 8s even though i had specified 1s. I was not able to create this scenario again.
I have attached my code below,
In the hardware, I have a switch connected externally to D7 and pulled up to 3.3V.
int yellow = D2; // declare yellow ledPin as variable
int green = D3; // declare green ledPin as variable
int red = D4; // declare red ledPin as variable
int blue = D5; // declare blue ledPin as variable
void setup()
{
// put your setup code here, to run once:
pinMode(yellow,OUTPUT); //configure ledPin as OUTPUT
pinMode(green,OUTPUT); //configure ledPin as OUTPUT
pinMode(red,OUTPUT); //configure ledPin as OUTPUT
pinMode(blue,OUTPUT); //configure ledPin as OUTPUT
pinMode(SW,INPUT); //configure switch as INPUT
attachInterrupt(digitalPinToInterrupt(SW), interruptISR, FALLING);
}
void loop()
{
digitalWrite(yellow, HIGH); // turn on yellow LED
delay(1000); // delay 1s
digitalWrite(yellow, LOW); // turn off yellow LED
digitalWrite(green, HIGH); // turn on green LED
delay(1000); // delay 1s
digitalWrite(green, LOW); // turn off green LED
digitalWrite(red, HIGH); // turn on red LED
delay(1000); // delay 1s
digitalWrite(red, LOW); // turn off red LED
}
void interruptISR() {
digitalWrite(blue, HIGH); // turn on blue LED
delay(500); // delay 1s
digitalWrite(blue, LOW); // turn off blue LED
}