Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Infineon Team
Published © MIT

Using GPIO/ Timer Interrupts with Micropython on the PSoC6

Tap into the power of interrupts with PSoC 6 and MicroPython! Check out a simple 2-player reaction time game.

IntermediateProtip2 hours147
Using GPIO/ Timer Interrupts with Micropython on the PSoC6

Things used in this project

Hardware components

CY8CPROTO-062-4343W
Infineon CY8CPROTO-062-4343W
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×2
Resistor 10k ohm
Resistor 10k ohm
×2
LED (generic)
LED (generic)
×1
Through Hole Resistor, 300 ohm
Through Hole Resistor, 300 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

MicroPython
MicroPython
Thonny IDE

Story

Read more

Schematics

interrupt_game

Code

interrupt_game.py

MicroPython
from machine import Pin
import time
import urandom

# Initialize the pin for the LED (replace 'P9_4' with your actual LED pin)
led = Pin('P9_4', Pin.OUT)

# Initialize the pins for the buttons 
button1 = Pin('P9_6', Pin.IN, Pin.PULL_UP)
button2 = Pin('P9_7', Pin.IN, Pin.PULL_UP)

# Flags to indicate if the LED is on and if the game is active
led_on = False
game_active = False

# Variable to hold the start time
start_time = 0

# Callback function for button presses
def button_press_callback(pin):
    global start_time, led_on, game_active
    if game_active and led_on:
        reaction_time = time.ticks_diff(time.ticks_ms(), start_time)
        winner = "Player 1" if pin == button1 else "Player 2"
        print("{} wins with a reaction time of: {} ms".format(winner, reaction_time))
        led.value(0)
        led_on = False
        game_active = False

# Attach the interrupt to the callback function for both buttons
button1.irq(trigger=Pin.IRQ_RISING, handler=button_press_callback)
button2.irq(trigger=Pin.IRQ_RISING, handler=button_press_callback)
i = 0 
# Main program loop
while i < 5 :
    i = i + 1
    game_active = True
    # Generate a random time between 5 and 10 seconds (5000 to 10000 milliseconds)
    wait_time = urandom.getrandbits(20) % 5000 + 5000
    time.sleep_ms(wait_time)
    
    # Turn on the LED and start the game
    led.value(1)
    led_on = True
    start_time = time.ticks_ms()
    
    # Now we wait for either button press in the interrupt handler
    while game_active:
        time.sleep_ms(50)  # Small delay to prevent busy waiting

    # Wait a moment before starting the next round
    time.sleep_ms(2000)

Timer

MicroPython
from machine import Timer
import time
tim0 = Timer(0, period=1000, mode=Timer.ONE_SHOT, callback=lambda t:print("One shot timer triggered")) #Default assignment: period=9999, frequency=10000
tim1 = Timer(1, period=3000, mode=Timer.PERIODIC, callback=lambda t:print("Periodic timer triggered"))

tim0.deinit() # Deinitialise the timer
tim1.deinit() # Deinitialise the timer

Credits

Infineon Team
101 projects • 158 followers
Contact

Comments

Please log in or sign up to comment.