Would it be possible to change the duration of a sleep step in a code that is already running? Could I have it import the variable from another script that I could update without interrupting the running code? For example, I have this code running with "time.sleep(3600") so that it loops every hour. I'd like to change it to every 12 hours for a few days, but I'd rather not stop and restart it after changing the time. Thinking that if I update the "ShaneKeys" to change the SLEEPDUR = 43200 it will import that and use it as SLEEPDUR at the end of the
Here's the code with the edits - note the "from ShaneKeys import ARKEY1, SLEEPDUR" and the "time.sleep(SLEEPDUR)" near the end of the loop.
Here's the code with the edits - note the "from ShaneKeys import ARKEY1, SLEEPDUR" and the "time.sleep(SLEEPDUR)" near the end of the loop.
Code:
import timeimport sysimport RPi.GPIO as GPIOfrom hx711 import HX711import requestsfrom ShaneKeys import ARKEY1, SLEEPDURfrom gpiozero import LEDLED2 = LED(12) # Red LED to indicate low level.LED1 = LED(16) # Green LED to indicate high level.def cleanAndExit(): print("Cleaning...") print("Bye!") sys.exit() LED1.on() # Indication that the program has started and that LED indications are functional.LED2.on() # Indication that the program has started and that LED indications are functional.hx = HX711(20, 21)hx.set_reading_format("MSB", "MSB")referenceUnit = 114hx.set_reference_unit(referenceUnit)hx.reset()hx.tare()LED2.off() # Indication that reset and tare functions are complete.time.sleep(5) # Take a moment to reflect. LED1.off() # Indication that reset and tare functions are complete.print("Scale has been set to zero, add weight now...") # This is when I know that the "zero" or "calibration" is done. If not using VNC, I use the LEDs to tell me when to load the load.while True: try: from ShaneKeys import SLEEPDUR val = hx.get_weight(5) wholeval = (val * -1) # For some reason the "val" was reporting a negative value. This makes it positive. newval = wholeval / 100 # This makes the value in a unit of whole pounds (2154 becomes 21.54 pounds). roundedval = round(newval, 2) # This ensures that the pound value is limited to two decimal places. print(roundedval) # This displays the measured weight in pounds. weight = str(roundedval) if roundedval > 10: # If the volume is greater than 25%, then power on the green LED on GPIO16. LED2.off() LED1.on() else: # If the volume is less than 25%, then power the red LED on GPIO12. LED2.on() LED1.off() r = requests.post(ARKEY1 + "|water|" + str(roundedval)) #This is where the script sends the weight to my phone - the variable "roundedval" is the weight of the keg in pounds. hx.power_down() hx.power_up() time.sleep(SLEEPDUR) #Sleep for XXX minutes then recheck the weight of the water. except (KeyboardInterrupt, SystemExit): cleanAndExit()
Statistics: Posted by duckredbeard — Wed Jan 22, 2025 7:16 pm — Replies 0 — Views 29