"Latching" variables in function

G

Grawburg

I've probably used the wrong term - I'm thinking of what I do when writing PLC code - so I can't find how to do this in my reference books.
This is part of a project I'm working on with a Raspberry Pi and an MCP23017 port expander.
I have a N/O pushbutton that I want to "latch" a value to a variable when it's been pressed.  I have this function that gets called periodically in
a 'while True' statement:


def button():
   pushbutton = 0
  button_value = 0
   pushbutton=bus.read_byte_data(address,GPIOB)
   if pushbutton > 0:
        button_value = 1
   return button_value




I need button_value to become '1' when the button is pressed and to remain '1' until the entire program (only about 25 lines) ends with a sys.exit()


What do I use to 'latch' button_value?




Brian Grawburg
North Carolina
 
D

Denis McMahon

def button():
   pushbutton = 0
  button_value = 0
   pushbutton=bus.read_byte_data(address,GPIOB)
   if pushbutton > 0:
        button_value = 1
   return button_value

Every time your function is called, you start out with button_value of 0.

You may need a global variable that starts out as False (or 0), and once
flipped to True (or 1) and then stays there:

button_value = False # or: button_value = 0

def button():
global button_value
   pushbutton = bus.read_byte_data( address, GPIOB )
   if pushbutton > 0:
        button_value = True # or: button_value = 1
   return button_value

Also I think I'd probably pass the IO address as a parameter to the
button function.
 
M

Mark H Harris

I have a N/O pushbutton that I want to "latch" a value to a variable when it's been pressed.
I need button_value to become '1' when the button is pressed and to remain '1' until ...
What do I use to 'latch' button_value?

Philosophically speaking buttons don't latch. You push the button, an
event is generated, and the call-back handles the event to do something
in your project.

You might try setting a global variable on the button-push event.

Or, if I understand you, you might want to use a configurable, like a
radio button or a check box, either of which are designed to be "latched".


marcus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top