Continue running a loop until a button is pressed using gpiozero - button press not registeredC program a button to perform a task once when pressed (latch)Python variable not recognized in if-statementAre tkinter buttons able to run if statement once pressed?Trigger action on button press on raspberryRaspberry Pi Python Press button and run some codeMaking Tkinter wait untill button is pressedBug in python while loop… Need Python advicestatement still running even though while loop is evaluated to false?Python | How can i run a loop for x seconds and then break out of it without sleep?tkinter wait for button press with .invoke()

The grades of the students in a class

If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?

Gold Battle KoTH

Is this popular optical illusion made of a grey-scale image with coloured lines?

What Marvel character has this 'W' symbol?

Why are prop blades not shaped like household fan blades?

How did Biff return to 2015 from 1955 without a lightning strike?

What is the name of this type of figure?

Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) ...1748 Euler

Applications of pure mathematics in operations research

Why is the "I" in "Indigenous crisis" capitalized?

Can the additional attack from a Samurai's Rapid Strike have advantage?

Reasons for using monsters as bioweapons

Why are sugars in whole fruits not digested the same way sugars in juice are?

Should I put my name first or last in the team members list?

How to structure presentation to avoid getting questions that will be answered later in the presentation?

Can machine learning learn a function like finding maximum from a list?

Conflict between senior and junior members

How do I respond appropriately to an overseas company that obtained a visa for me without hiring me?

How to get Planck length in meters to 6 decimal places

Russian pronunciation of /etc (a directory)

UX writing: When to use "we"?

May a hotel provide accommodation for fewer people than booked?

How can a class have multiple methods without breaking the single responsibility principle



Continue running a loop until a button is pressed using gpiozero - button press not registered


C program a button to perform a task once when pressed (latch)Python variable not recognized in if-statementAre tkinter buttons able to run if statement once pressed?Trigger action on button press on raspberryRaspberry Pi Python Press button and run some codeMaking Tkinter wait untill button is pressedBug in python while loop… Need Python advicestatement still running even though while loop is evaluated to false?Python | How can i run a loop for x seconds and then break out of it without sleep?tkinter wait for button press with .invoke()






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have some python code on a raspberry pi that I want to run and carry on a loop until a button is pressed.



The button.wait_for_press() is not suitable because that pauses the program until it runs, but I have tried it out to see if the hardware is working and it does.



def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True

def main():

"""
Main program loop
"""

#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))

#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)

#Wait for button press
i = 0
blink_speed = 10

button.when_pressed = shutterPressed

while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


When I run this code, the button press is not registered at all.



What have I got wrong?



EDIT: so I added a print statement to the shutterPressed() function and confirmed that it is running when the button is pressed.



In also added a statement to print the value of shutterHasBeenPressed just before the if statement. This never changed from false.



However, if I removed the line changing the variable to false at the beginning of the loop, then code worked, so it is obviously something to do with when various bits get run. Maybe the while loop starts again after the shutterPressed() function runs?



Either way, i have fixed it by moving the reassignment of the variable to after the if statement.










share|improve this question


























  • Why are you using when_pressed and setting a global variable? Just use one of the other methods of detecting the button press. I'd suggest button.wait_for_press(). Try that on its own to make sure your button is working.

    – ben_nuttall
    Mar 27 at 10:28











  • So I have tried the button.waitforpress() method to check the button as mentioned in the original question. The issue with it is that it pauses the program rather than allowing the code to continue until the button is pressed.

    – James
    Mar 27 at 12:03


















0















I have some python code on a raspberry pi that I want to run and carry on a loop until a button is pressed.



The button.wait_for_press() is not suitable because that pauses the program until it runs, but I have tried it out to see if the hardware is working and it does.



def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True

def main():

"""
Main program loop
"""

#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))

#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)

#Wait for button press
i = 0
blink_speed = 10

button.when_pressed = shutterPressed

while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


When I run this code, the button press is not registered at all.



What have I got wrong?



EDIT: so I added a print statement to the shutterPressed() function and confirmed that it is running when the button is pressed.



In also added a statement to print the value of shutterHasBeenPressed just before the if statement. This never changed from false.



However, if I removed the line changing the variable to false at the beginning of the loop, then code worked, so it is obviously something to do with when various bits get run. Maybe the while loop starts again after the shutterPressed() function runs?



Either way, i have fixed it by moving the reassignment of the variable to after the if statement.










share|improve this question


























  • Why are you using when_pressed and setting a global variable? Just use one of the other methods of detecting the button press. I'd suggest button.wait_for_press(). Try that on its own to make sure your button is working.

    – ben_nuttall
    Mar 27 at 10:28











  • So I have tried the button.waitforpress() method to check the button as mentioned in the original question. The issue with it is that it pauses the program rather than allowing the code to continue until the button is pressed.

    – James
    Mar 27 at 12:03














0












0








0








I have some python code on a raspberry pi that I want to run and carry on a loop until a button is pressed.



The button.wait_for_press() is not suitable because that pauses the program until it runs, but I have tried it out to see if the hardware is working and it does.



def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True

def main():

"""
Main program loop
"""

#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))

#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)

#Wait for button press
i = 0
blink_speed = 10

button.when_pressed = shutterPressed

while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


When I run this code, the button press is not registered at all.



What have I got wrong?



EDIT: so I added a print statement to the shutterPressed() function and confirmed that it is running when the button is pressed.



In also added a statement to print the value of shutterHasBeenPressed just before the if statement. This never changed from false.



However, if I removed the line changing the variable to false at the beginning of the loop, then code worked, so it is obviously something to do with when various bits get run. Maybe the while loop starts again after the shutterPressed() function runs?



Either way, i have fixed it by moving the reassignment of the variable to after the if statement.










share|improve this question
















I have some python code on a raspberry pi that I want to run and carry on a loop until a button is pressed.



The button.wait_for_press() is not suitable because that pauses the program until it runs, but I have tried it out to see if the hardware is working and it does.



def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True

def main():

"""
Main program loop
"""

#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))

#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)

#Wait for button press
i = 0
blink_speed = 10

button.when_pressed = shutterPressed

while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


When I run this code, the button press is not registered at all.



What have I got wrong?



EDIT: so I added a print statement to the shutterPressed() function and confirmed that it is running when the button is pressed.



In also added a statement to print the value of shutterHasBeenPressed just before the if statement. This never changed from false.



However, if I removed the line changing the variable to false at the beginning of the loop, then code worked, so it is obviously something to do with when various bits get run. Maybe the while loop starts again after the shutterPressed() function runs?



Either way, i have fixed it by moving the reassignment of the variable to after the if statement.







python raspberry-pi gpiozero






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 18:25







James

















asked Mar 26 at 23:04









JamesJames

84 bronze badges




84 bronze badges















  • Why are you using when_pressed and setting a global variable? Just use one of the other methods of detecting the button press. I'd suggest button.wait_for_press(). Try that on its own to make sure your button is working.

    – ben_nuttall
    Mar 27 at 10:28











  • So I have tried the button.waitforpress() method to check the button as mentioned in the original question. The issue with it is that it pauses the program rather than allowing the code to continue until the button is pressed.

    – James
    Mar 27 at 12:03


















  • Why are you using when_pressed and setting a global variable? Just use one of the other methods of detecting the button press. I'd suggest button.wait_for_press(). Try that on its own to make sure your button is working.

    – ben_nuttall
    Mar 27 at 10:28











  • So I have tried the button.waitforpress() method to check the button as mentioned in the original question. The issue with it is that it pauses the program rather than allowing the code to continue until the button is pressed.

    – James
    Mar 27 at 12:03

















Why are you using when_pressed and setting a global variable? Just use one of the other methods of detecting the button press. I'd suggest button.wait_for_press(). Try that on its own to make sure your button is working.

– ben_nuttall
Mar 27 at 10:28





Why are you using when_pressed and setting a global variable? Just use one of the other methods of detecting the button press. I'd suggest button.wait_for_press(). Try that on its own to make sure your button is working.

– ben_nuttall
Mar 27 at 10:28













So I have tried the button.waitforpress() method to check the button as mentioned in the original question. The issue with it is that it pauses the program rather than allowing the code to continue until the button is pressed.

– James
Mar 27 at 12:03






So I have tried the button.waitforpress() method to check the button as mentioned in the original question. The issue with it is that it pauses the program rather than allowing the code to continue until the button is pressed.

– James
Mar 27 at 12:03













1 Answer
1






active

oldest

votes


















0














Welcome to the world of concurrency (somewhat)!



Just imagine your program running: the instructions are being executed one after the other in the order in which they are written, according to the execution flow that they define with the exception of shutterPressed which is asynchronously executed (maybe).



Therefore, imagine we enter the loop and are at the first line, <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


Now, shutterHasBeenPressed has been set to False and the condition that follows is verified so that we enter the if.



the program keeps running until, unexpectedly, the button is pressed. Say, it reached <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


At this point, shutterPressed runs, sets shutterHasBeenPressed to True. Then, back in our loop, the iteration finishes, we continue at the start of the loop and ... what's there?!



 shutterHasBeenPressed = False


and the button press just went completely unnoticed!



I believe this answers your question asking what you have got wrong.






share|improve this answer

























  • Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

    – James
    Mar 27 at 20:47











  • @James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

    – PiCTo
    Mar 27 at 23:20











  • Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

    – James
    Mar 27 at 23:30











  • Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

    – PiCTo
    Mar 27 at 23:40










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367437%2fcontinue-running-a-loop-until-a-button-is-pressed-using-gpiozero-button-press%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Welcome to the world of concurrency (somewhat)!



Just imagine your program running: the instructions are being executed one after the other in the order in which they are written, according to the execution flow that they define with the exception of shutterPressed which is asynchronously executed (maybe).



Therefore, imagine we enter the loop and are at the first line, <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


Now, shutterHasBeenPressed has been set to False and the condition that follows is verified so that we enter the if.



the program keeps running until, unexpectedly, the button is pressed. Say, it reached <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


At this point, shutterPressed runs, sets shutterHasBeenPressed to True. Then, back in our loop, the iteration finishes, we continue at the start of the loop and ... what's there?!



 shutterHasBeenPressed = False


and the button press just went completely unnoticed!



I believe this answers your question asking what you have got wrong.






share|improve this answer

























  • Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

    – James
    Mar 27 at 20:47











  • @James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

    – PiCTo
    Mar 27 at 23:20











  • Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

    – James
    Mar 27 at 23:30











  • Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

    – PiCTo
    Mar 27 at 23:40















0














Welcome to the world of concurrency (somewhat)!



Just imagine your program running: the instructions are being executed one after the other in the order in which they are written, according to the execution flow that they define with the exception of shutterPressed which is asynchronously executed (maybe).



Therefore, imagine we enter the loop and are at the first line, <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


Now, shutterHasBeenPressed has been set to False and the condition that follows is verified so that we enter the if.



the program keeps running until, unexpectedly, the button is pressed. Say, it reached <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


At this point, shutterPressed runs, sets shutterHasBeenPressed to True. Then, back in our loop, the iteration finishes, we continue at the start of the loop and ... what's there?!



 shutterHasBeenPressed = False


and the button press just went completely unnoticed!



I believe this answers your question asking what you have got wrong.






share|improve this answer

























  • Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

    – James
    Mar 27 at 20:47











  • @James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

    – PiCTo
    Mar 27 at 23:20











  • Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

    – James
    Mar 27 at 23:30











  • Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

    – PiCTo
    Mar 27 at 23:40













0












0








0







Welcome to the world of concurrency (somewhat)!



Just imagine your program running: the instructions are being executed one after the other in the order in which they are written, according to the execution flow that they define with the exception of shutterPressed which is asynchronously executed (maybe).



Therefore, imagine we enter the loop and are at the first line, <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


Now, shutterHasBeenPressed has been set to False and the condition that follows is verified so that we enter the if.



the program keeps running until, unexpectedly, the button is pressed. Say, it reached <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


At this point, shutterPressed runs, sets shutterHasBeenPressed to True. Then, back in our loop, the iteration finishes, we continue at the start of the loop and ... what's there?!



 shutterHasBeenPressed = False


and the button press just went completely unnoticed!



I believe this answers your question asking what you have got wrong.






share|improve this answer













Welcome to the world of concurrency (somewhat)!



Just imagine your program running: the instructions are being executed one after the other in the order in which they are written, according to the execution flow that they define with the exception of shutterPressed which is asynchronously executed (maybe).



Therefore, imagine we enter the loop and are at the first line, <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


Now, shutterHasBeenPressed has been set to False and the condition that follows is verified so that we enter the if.



the program keeps running until, unexpectedly, the button is pressed. Say, it reached <here>:



 while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")


At this point, shutterPressed runs, sets shutterHasBeenPressed to True. Then, back in our loop, the iteration finishes, we continue at the start of the loop and ... what's there?!



 shutterHasBeenPressed = False


and the button press just went completely unnoticed!



I believe this answers your question asking what you have got wrong.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 18:47









PiCToPiCTo

3482 silver badges11 bronze badges




3482 silver badges11 bronze badges















  • Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

    – James
    Mar 27 at 20:47











  • @James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

    – PiCTo
    Mar 27 at 23:20











  • Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

    – James
    Mar 27 at 23:30











  • Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

    – PiCTo
    Mar 27 at 23:40

















  • Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

    – James
    Mar 27 at 20:47











  • @James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

    – PiCTo
    Mar 27 at 23:20











  • Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

    – James
    Mar 27 at 23:30











  • Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

    – PiCTo
    Mar 27 at 23:40
















Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

– James
Mar 27 at 20:47





Thank you, so basically, the way it is written, the button would need to be pressed exactly at the moment between the shutterHasBeenPressed assignment and the if statement?

– James
Mar 27 at 20:47













@James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

– PiCTo
Mar 27 at 23:20





@James Exactly! And this window certainly lasts less than a microsecond and happens (roughly) every 100 milliseconds : you'd have to be very lucky!

– PiCTo
Mar 27 at 23:20













Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

– James
Mar 27 at 23:30





Ok cool, so I moved the shutterHasBeenPressed = False to the end of my while looo so that it will always get through the main part of the programme. I also thought that I didn't want an accidental press while the main code was running to cause it to immediately run again.

– James
Mar 27 at 23:30













Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

– PiCTo
Mar 27 at 23:40





Yes, that works because then the shutterHasBeenPressed = False is now only executed when needed. Don't you think an if-else would be more readable than a continue? Also, in Python, you don't have (and shouldn't except in specific cases) to test against booleans: "truthiness" should be enough. Finally, i is used to keep track of time: try to look up better ways to do so which would be more accurate (not really capital, here) and readable.

– PiCTo
Mar 27 at 23:40








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367437%2fcontinue-running-a-loop-until-a-button-is-pressed-using-gpiozero-button-press%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해