0

I have a program with this structure:

variable_to_update = 1000


while True: 
    # this must be running in an infinite loop
    do_something(variable_to_update)

In some specific moments I need to update the variable variable_to_update with my keyboard:

Ex:

if key_pressed == 'up':
  variable_to_update += 10

Is there a way to achieve this???? Maybe with the Threading module?

ljuk
  • 701
  • 3
  • 12
  • please supply the code you are trying to figure out, your example wont help much in figuring out the problem – AmaanK Dec 27 '20 at 16:05
  • the code in the question must be enough, I'm just trying to figure out some basic idea of how to achieve this, not a complete response. – ljuk Dec 27 '20 at 16:09
  • It's not enough code to determine the conflict. Do you have race conditions? Does your code simply not update the variable at all? Does your program conflict between global and thread local variables? Does your program crash? There are many things that can create a conflict. Also, without any more code, people have to write their solution from scratch and download the `pypinput` library, when instead the solution might be obvious from a short code example. I know about threading and threading concepts, but don't know how `pypinput` works, but the problem is most likely not due to it. – Ted Klein Bergman Dec 27 '20 at 16:19
  • pyinput is not necessary. I just need a way to read my keyboard and update the variable, because I'm tunning a PID control for a drone, and 'variable_to_update' is one of the PID constants. The while loop is mandatory because the drone can't stop the power of motors. – ljuk Dec 27 '20 at 16:32

1 Answers1

0

if given the following program

variable_to_update = 1000


while True: 
    # this must be running in an infinite loop
    do_something(variable_to_update)

then you could make variable_to_update a list and pass it function

variable_to_update = [1000]


while True: 
    # this must be running in an infinite loop
    do_something(variable_to_update)

and inside function do_something

def do_something(variable):
    variable[0] = "New Value"
    print(variable[0])

I know the above is not the best approach but can be a workaround

AmaanK
  • 1,032
  • 5
  • 25