goglobal
Posts: 4
Joined: Thu May 27, 2021 11:28 am

Record a string with button with a time.sleep(3600)?

Thu May 27, 2021 11:38 am

Hello everyone,

I am doing a test. I have raspberry pi zero with a temperature sensor that gets readings every one hour. The code is inside a while loop.

Code: Select all

while True:
	#here is the code for reading temps
	
	if button.is_pressed:
		data = 'yes'
	else:
		data = 'no'
		
	print(data)
	time.sleep(3600)
So, when I do not press the button I continuously get every one hour the string "no". The issue I have is that even when I press the button I still get "no". Because, I am guessing, I need to press the button at the time that the while loop wakes up.

Is there a way to have a button that is constantly checking if pressed WHILST having a time.sleep(3600)?

Thanks

User avatar
thagrol
Posts: 5567
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Record a string with button with a time.sleep(3600)?

Thu May 27, 2021 3:31 pm

goglobal wrote:
Thu May 27, 2021 11:38 am
Hello everyone,

I am doing a test. I have raspberry pi zero with a temperature sensor that gets readings every one hour. The code is inside a while loop.

Code: Select all

while True:
	#here is the code for reading temps
	
	if button.is_pressed:
		data = 'yes'
	else:
		data = 'no'
		
	print(data)
	time.sleep(3600)
So, when I do not press the button I continuously get every one hour the string "no". The issue I have is that even when I press the button I still get "no". Because, I am guessing, I need to press the button at the time that the while loop wakes up.
Yep.
Is there a way to have a button that is constantly checking if pressed WHILST having a time.sleep(3600)?
Use a call back. You don't say which library you're using or which language so it's hard to give an example. There's probably an example in the docs for the GPIO library you're using though.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

goglobal
Posts: 4
Joined: Thu May 27, 2021 11:28 am

Re: Record a string with button with a time.sleep(3600)?

Thu May 27, 2021 4:32 pm

Thanks for the reply @thagrol. Apologies for not clarifying. I am using Python and the library I am using is GPIOZERO. I need to have a look around for a callback. I have never used one so I am currently unsure what it is!

User avatar
B.Goode
Posts: 12836
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Record a string with button with a time.sleep(3600)?

Thu May 27, 2021 5:23 pm

goglobal wrote:
Thu May 27, 2021 4:32 pm
Thanks for the reply @thagrol. Apologies for not clarifying. I am using Python and the library I am using is GPIOZERO. I need to have a look around for a callback. I have never used one so I am currently unsure what it is!

The documentation you need for gpiozero is probably here - https://gpiozero.readthedocs.io/en/stab ... tml#button


You probably won't see 'callback' explicitly mentioned there, but that's likely to be what the methods named wait_for_* and when_* are using behind the scenes.

User avatar
thagrol
Posts: 5567
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Record a string with button with a time.sleep(3600)?

Thu May 27, 2021 10:25 pm

B.Goode wrote:
Thu May 27, 2021 5:23 pm
goglobal wrote:
Thu May 27, 2021 4:32 pm
Thanks for the reply @thagrol. Apologies for not clarifying. I am using Python and the library I am using is GPIOZERO. I need to have a look around for a callback. I have never used one so I am currently unsure what it is!

The documentation you need for gpiozero is probably here - https://gpiozero.readthedocs.io/en/stab ... tml#button


You probably won't see 'callback' explicitly mentioned there, but that's likely to be what the methods named wait_for_* and when_* are using behind the scenes.
You (OP) won't. The method you're looking for is probably Button.when_pressed(). There's an example here: https://gpiozero.readthedocs.io/en/stab ... tml#button
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

pcmanbob
Posts: 11668
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Record a string with button with a time.sleep(3600)?

Fri Jul 30, 2021 1:54 pm

There are a couple of things to take in to consideration ,

1. You will be updating data to " Yes " from within a function , so you need to add a " global data " as the first so that the data variable in the main program loop is updated by the function.

2. You will need to reset data to " No " on each loop as the function call when the button is pressed will only update it to " Yes ".

like this

Code: Select all

from gpiozero import Button
import time

def pressed():
    global data
    data = "Yes"
    

button = Button(21)

button.when_pressed = pressed
data = "No"
while True:
    
    #here is the code for reading temps
    
    print(data)
    data = "No"
    time.sleep(4)

We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “General programming discussion”