Legal Removal Request This form is for reporting content posted on the AB Electronics UK forums that you believe violates your personal legal rights or applicable local laws for your country. Post: Hello The problem you are having is due to the GPIO event listener and your test function running within the same thread. The time.sleep() call is pausing the thread for 2 seconds during which the event listener can not respond to any button presses. What you will need to do is split your code into two threads with the gpio event listener in one thread and your other program code in a second thread. I have modified your program in the code below to show how you can use threads to make your program more responsive. [pre][code]import time from threading import Thread from threading import Event import RPi.GPIO as GPIO from IOPi import IOPi bus1 = None def button_pressed(interrupt_pin): global bus1 """ this function will be called when GPIO 23 falls low """ # read the interrupt capture for port 0 and store it in variable intval intval = bus1.read_interrupt_capture(0) # compare the value of intval with the IO Pi port 0 # using read_port(). wait until the port changes which will indicate # the button has been released. # without this while loop the function will keep repeating. while (intval == bus1.read_port(0)): time.sleep(0.2) # loop through each bit in the intval variable and check if the bit is 1 # which will indicate a button has been pressed for num in range(0, 8): if (intval & (1 << num)): # print("Pin " + str(num + 1) + " pressed") if str(num + 1) == '1': return state0() elif str(num + 1) == '2': return state1() elif str(num + 1) == '3': return state2() elif str(num + 1) == '4': return state3() def state0(): print("Pyano: Main Menu") bus1.write_pin(9, 1) bus1.write_pin(11, 0) bus1.write_pin(13, 0) bus1.write_pin(15, 0) def state1(): print("Mode 1: midi_player") bus1.write_pin(9, 0) bus1.write_pin(11, 1) bus1.write_pin(13, 0) bus1.write_pin(15, 0) def state2(): print("Mode 2: midi_maker") bus1.write_pin(9, 0) bus1.write_pin(11, 0) bus1.write_pin(13, 1) bus1.write_pin(15, 0) def state3(): print("Mode 3: midi_live") bus1.write_pin(9, 0) bus1.write_pin(11, 0) bus1.write_pin(13, 0) bus1.write_pin(15, 1) def test(): i = 0 while i < 100: print(i) e = Event() e.wait(timeout=2) i = i + 1 def gpio_thread(): # set the Raspberry Pi GPIO mode to be BCM GPIO.setmode(GPIO.BCM) # Set up GPIO 23 as an input. The pull-up resistor is disabled as the # level shifter will act as a pull-up. GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # when a falling edge is detected on GPIO 23 the function # button_pressed will be run GPIO.add_event_detect(23, GPIO.FALLING, callback=button_pressed, bouncetime=300) def main(): """ Main program function """ global bus1 # Create an instance of the IOPi class called bus1 and # set the I2C address to be 0x20 or Bus 1. bus1 = IOPi(0x20) # State LED indicator setup bus1.set_port_direction(1, 0x00) bus1.write_port(1, 0x00) # Set port 0 on the bus1 to be inputs with internal pull-ups enabled. bus1.set_port_pullups(0, 0xFF) bus1.set_port_direction(0, 0xFF) # Inverting the port will allow a button connected to ground to # register as 1 or on. bus1.invert_port(0, 0xFF) # Set the interrupt polarity to be active low so Int A and IntB go low # when an interrupt is triggered and mirroring disabled, so # Int A is mapped to port 0 and Int B is mapped to port 1 bus1.set_interrupt_polarity(0) bus1.mirror_interrupts(0) # Set the interrupts default value to 0 so it will trigger when any of # the pins on the port 0 change to 1 bus1.set_interrupt_defaults(0, 0x00) # Set the interrupt type to be 0xFF so an interrupt is # fired when the pin matches the default value bus1.set_interrupt_type(0, 0xFF) # Enable interrupts for all pins on port 0 bus1.set_interrupt_on_port(0, 0xFF) # reset the interrups on the IO Pi bus1 bus1.reset_interrupts() #make sure all LEDs are turned off bus1.write_port(1, 0x00) thread1 = Thread(target=gpio_thread()) thread1.start() thread2 = Thread(target=test()) thread2.start() input("Press enter to exit program \n") if __name__ == "__main__": main()[/code][/pre] Select the country where you are claiming legal rights. Albania Algeria American Samoa Andorra Angola Anguilla Antarctica Antigua and Barbuda Argentina Aruba Australia Austria Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bermuda Bhutan Bolivia Bosnia And Herzegovina Botswana Bouvet Island Brazil British Indian Ocean Territory British Virgin Islands Brunei Bulgaria Burkina Faso Burundi Cambodia Cameroon Canada Canary Islands Cape Verde Cayman Islands Central African Republic Chad Channel Islands Chile Christmas Island Cocos (Keeling) Islands Colombia Comoros Congo Cook Islands Costa Rica Croatia Cuba Cyprus Czech Republic Denmark Djibouti Dominica Dominican Republic East Timor Ecuador Egypt El Salvador Equatorial Guinea Estonia Ethiopia Falkland Islands (Malvinas) Faroe Islands Federated States of Micronesia Fiji Finland France French Guiana French Polynesia French Southern Territories Gabon Gambia Georgia Germany Ghana Gibraltar Greece Greenland Grenada Guadeloupe Guam Guatemala Guyana Haiti Heard Island and McDonald Islands Honduras Hong Kong Hungary Iceland India Indonesia Ireland Israel Italy Jamaica Japan Jordan Kazakhstan Kenya Kiribati Kuwait Kyrgyzstan Laos Latvia Lesotho Liechtenstein Lithuania Luxembourg Macau Macedonia Madagascar Malawi Malaysia Maldives Mali Malta Marshall Islands Martinique Mauritania Mauritius Mayotte Mexico Micronesia, Federated States Of Moldova, Republic Of Monaco Mongolia Montenegro Montserrat Morocco Mozambique Myanmar Namibia Nauru Nepal Netherlands Netherlands Antilles New Caledonia New Zealand Nicaragua Niue Norfolk Island Northern Mariana Islands Norway Oman Palau Panama Papua New Guinea Paraguay Peru Philippines Pitcairn Poland Portugal Puerto Rico Qatar Reunion Romania Russia Rwanda Samoa San Marino Sao Tome and Principe Saudi Arabia Serbia Seychelles Singapore Slovakia Slovenia Solomon Islands South Africa South Georgia and the South Sandwich Islands South Korea Spain Sri Lanka St. Helena St. Kitts and Nevis St. Lucia St. Pierre and Miquelon St. Vincent and the Grenadines Suriname Svalbard and Jan Mayen Islands Swaziland Sweden Switzerland Syria Taiwan Tajikistan Tanzania Thailand Togo Tokelau Tonga Trinidad and Tobago Tunisia Turkey Turkmenistan Turks and Caicos Islands Tuvalu U.S. Virgin Islands Uganda Ukraine United Arab Emirates United Kingdom United States United States Minor Outlying Islands Uruguay Uzbekistan Vanuatu Vatican City Venezuela Vietnam Wallis and Futuna Islands Western Sahara Yemen Yugoslavia Zambia What legal issue or problem do you wish to report? Please select Privacy / Erasure under GDPR Defamation Intellectual Property Hate Speech Other Please enter the following information so we can process your report. Contact Name: Contact Email: Details of complaint: Submit Complaint