I am making a Raspberry Pi librarian which can keep track of available books and who has taken out a book. Users scan NFC cards to begin the process of borrowing a book.

The main program is an infinite loop and goes like this:

while True:
    # wait for card to be scanned
    # identify the user from a database
    # ask if borrowing or returning
    # ask which book user wants to borrow/return
    # update the database

I want to be able to include a physical cancel button so that the user can cancel at any time in the borrowing/returning process and the main loop will restart and wait for a card again.

I've used this guide from Raspi.TV and successfully set up an interrupt button.

How would I code this into my program?

1 Answer 1


You need to check the state of the GPIO at regular points in your main loop and if necessary issue the continue command to restart the loop.


if read(MY_INTERRUPT_GPIO) == 1: # high means restart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.