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.
User avatar
patvdleer
Posts: 36
Joined: Mon Jun 13, 2016 11:52 am
Location: Maastricht, NL
Contact:

Wemos D1 Interrupt trouble

Postby 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
WeMos D1 Mini

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

Re: Wemos D1 Interrupt trouble

Postby 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: 36
Joined: Mon Jun 13, 2016 11:52 am
Location: Maastricht, NL
Contact:

Re: Wemos D1 Interrupt trouble

Postby 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
WeMos D1 Mini


Return to “ESP8266 boards”

Who is online

Users browsing this forum: No registered users and 1 guest