IRQ: Pin Interrupt
This Python code demonstrates the pin interrupt function. The Arduino main control board is connected to a Windows or Linux computer, and a button module is connected to the D8 pin of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Pin interrupt function test
# Connection: Use a Windows or Linux computer to connect to an Arduino main control board, the main control board D8 is connected to a button module
import time
from pinpong.board import Board, Pin
Board("uno").begin() # Initialization, select board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic detection will be performed
# Board("uno", "COM36").begin() # Initialization with specified port on Windows
# Board("uno", "/dev/ttyACM0").begin() # Initialization with specified port on Linux
# Board("uno", "/dev/cu.usbmodem14101").begin() # Initialization with specified port on Mac
btn = Pin(Pin.D8, Pin.IN)
def btn_rising_handler(pin): # Interrupt event callback function
print("\n--rising---")
print("pin = ", pin)
def btn_falling_handler(pin): # Interrupt event callback function
print("\n--falling---")
print("pin = ", pin)
def btn_both_handler(pin): # Interrupt event callback function
print("\n--both---")
print("pin = ", pin)
btn.irq(trigger=Pin.IRQ_FALLING, handler=btn_falling_handler) # Set the interrupt mode to trigger on falling edge
# btn.irq(trigger=Pin.IRQ_RISING, handler=btn_rising_handler) # Set the interrupt mode to trigger on rising edge, and callback function
# btn.irq(trigger=Pin.IRQ_RISING+Pin.IRQ_FALLING, handler=btn_both_handler) # Set the interrupt mode to trigger when the level changes
while True:
time.sleep(1) # Keep the program running continuously