How can I handle button inputs in a while loop and at the same time refresh i2c data in a timely manner?
A while loop that watches for changes on the button pins can be run very quickly so that it is responsive, but I also want to update the i2c data once second. If I use time.sleep(1)
at the end of the while loop, this is too slow of a reaction time for the buttons.
Code would look something like:
from machine import Pin
from time import sleep
up_button = Pin(13, Pin.IN)
while True:
logic_state = up_button.value()
if logic_state == True:
print("Button Pressed")
else:
update_ic2_data() # polling the i2c device for new data
time.sleep(1) # sleeping for 1 second which is a good speed for i2c, but not very responsive for the button inputs.