from machine import Pin, Timer import time # play Hungry Hungry Hippo # Game lasts 10 seconds set with a timer interrupt # Count button presses by two players # Counting done by edge interrupts led = Pin(17, Pin.OUT) tim = Timer() flag1 = 0 flag2 = 0 N1 = 0 N2 = 0 GameTime = 0 pin1 = Pin(15,Pin.IN,Pin.PULL_UP) pin2 = Pin(14,Pin.IN,Pin.PULL_UP) def tick(timer): global led global N global GameTime if(GameTime > 0): GameTime -= 1 led.toggle() else: led.off() def player1(pin1): global flag1 global N1 flag = 1 N1 = N1 + 1 def player2(pin2): global flag2 global N2 flag2 = 1 N2 = N2 + 1 # options are RISING, FALLING, LOW_LEVEL, HIGH_LEVEL pin1.irq(trigger=Pin.IRQ_FALLING, handler=player1) pin2.irq(trigger=Pin.IRQ_FALLING, handler=player2) # set interrupt time to 10ms tim.init(freq=100, mode=Timer.PERIODIC, callback=tick) GameTime = 1000 while (GameTime > 0): print(GameTime*0.01, N1, N2) time.sleep(0.1)