Pillepalle1203
Pillepalle1203

Reputation: 1

Trigger event on certain time or button pressed

I'm writing a python script for my Raspberry Pi for opening the door of my chicken coop at a certain time of day and and to be able to control it via buttons. It already works, but I'm not yet completely satisfied with the solution.

    while True:
        if current_Time > openDoorTime && currentTime < closeDoorTime:
            openDoor()
        if currentTime > closeDoorTime && currentTime < openDoorTime:
            closeDoor()

        if openDoorButton is pressed:
            openDoor()
        if closeDoorButton is pressed:
            closeDoor()

Is there a more elegant solution to listen to clock times and pressed buttons at the same time? Thank you!

Upvotes: 0

Views: 322

Answers (1)

kirgizmustafa17
kirgizmustafa17

Reputation: 101

It is like math.

openDoorTime is lower than current_Time and current_Time is lower than closeDoorTime, so open_door_time < current_time < close_door_time.

And you have to use and, or operants. And I recomended to use underscores when naming variables. Use open_door_time instead of openDoorTime.

while True:
    if open_door_time < current_time < close_door_time or open_door_button is pressed:
        open_door()
    if close_door_time < current_time < open_door_time or close_door_button is pressed:
        close_door()

Upvotes: 1

Related Questions