0

I have a Python 3.6 application that waits indefinitely for the user to push a button.

while(1)
    print('You have 10 seocnds to push the button and start the connection wizard')
    If if GPIO.input(button1)==0: 
        subprocess.call(["wifi", "--clear=ssid"])

However, I only want to allow 10 seconds to push the button before moving on, how?

I have tried looping (for loop) but this just iterates and did not work.

MarkK
  • 968
  • 2
  • 14
  • 30
  • Possible duplicate of [Measuring elapsed time with the Time module](https://stackoverflow.com/questions/3620943/measuring-elapsed-time-with-the-time-module) – zrh Dec 12 '17 at 21:13
  • Possible duplicate of [How can I make a time delay in Python?](https://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python) – Aaron Lael Dec 12 '17 at 21:15

1 Answers1

2

You can use the time module to record the time before entering the loop, then calculate the time elapsed during each iteration. Add the time elapsed to the while condition.

import time

time_elapsed = 0
start_time = time.time()
while(time_elapsed < 10)
    print('You have 10 seocnds to push the button and start the connection wizard')
    If if GPIO.input(button1)==0: 
        subprocess.call(["wifi", "--clear=ssid"])
    time_elapsed = time.time() - start_time
Apollo2020
  • 509
  • 2
  • 8