Wemos D1 Interrupt trouble

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
patvdleer
Posts: 46
Joined: Mon Jun 13, 2016 11:52 am
Location: Maastricht, NL, Europe
Contact:

Wemos D1 Interrupt trouble

Post by patvdleer » Thu May 18, 2017 8:27 pm

I'm using a Wemos D1 mini v2 and I'm trying to hook up a 4x4 keypad via a MM74C922N chip which sets a pin to low when a button is pressed. I figured I would use an interrupt to set a bool to True but the only thing that happens is it turns on the LED on the ESP chip... I'm new to the interrupt way of working so I don't know if this behaviour is normal but nevertheless it isn't working as expected.

TL;DR;
On button press -> 74C922 sets pin low -> led turns on...

Code: Select all

from machine import I2C, Pin
i2c = I2C(sda=Pin(4), scl=Pin(5))

pinA = Pin(14, Pin.IN)
pinB = Pin(12, Pin.IN)
pinC = Pin(12, Pin.IN)
pinD = Pin(2, Pin.IN)
p0 = Pin(0, Pin.IN)

pins = [pinA, pinB, pinC, pinD]

button_pressed = False;
def callback(p):
	print('pin change', p)
	button_pressed = True;

p0.irq(trigger=Pin.IRQ_FALLING, handler=callback)

while True:
	if(button_pressed):
		output = 0;
		for pin in pins:			
			output = output | pin.value()
			output <<= 1
			print(output)
			button_pressed = False;
NodeMCU v0.9 / V1 / V2 / V3
WeMos D1 Mini
WeMos Lolin32 v1.0.0
WeMos Lolin D32 Pro V2

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Wemos D1 Interrupt trouble

Post by dhylands » Thu May 18, 2017 10:58 pm

In your callback routine you're not declaring button_pressed as a global, so the statement button_pressed = True inside the callback function is setting a local variable named button_pressed to True and not your global variable.

User avatar
patvdleer
Posts: 46
Joined: Mon Jun 13, 2016 11:52 am
Location: Maastricht, NL, Europe
Contact:

Re: Wemos D1 Interrupt trouble

Post by patvdleer » Fri May 19, 2017 10:09 am

awesome! thanks that did indeed fix it although I still don't understand why this toggles the blue led on the chip itself.
NodeMCU v0.9 / V1 / V2 / V3
WeMos D1 Mini
WeMos Lolin32 v1.0.0
WeMos Lolin D32 Pro V2

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Wemos D1 Interrupt trouble

Post by deshipu » Mon May 29, 2017 5:36 am

That LED is connected to GPIO2 and active-low. So it will come on when you pull GPIO2 low.

Post Reply