Aug-12-2020, 09:39 PM
I cleaned up two scripts that I am trying to merge. You can see that there are common elements to this. The first script I got working was the "keg1.py" and it uses the variable "keg1" for the weight values. While this is running, I get appropriate LED activity and a scroll of values in the shell window. The "keg1scale.py" is demonstrated at https:/youtu.be/2Oh-E0u8Jko.
keg1scale.py is this
compare_to_average.py is this:
The goal is to print "You got served" when the variable "keg1" decreases in value significantly within a few minutes of time. A pint of beer weighs about 9000 "units" on this scale system. How and where do I merge these two?
keg1scale.py is this
import time
import sys
from gpiozero import LED
#Define LED indicators - these are GPIO numbers
LED1 = LED(21)
LED2 = LED(26)
LED3 = LED(20)
LED4 = LED(19)
LED5 = LED(16)
LED6 = LED(13)
EMULATE_HX711=False
referenceUnit = 1
if not EMULATE_HX711:
import RPi.GPIO as GPIO
from hx711 import HX711
else:
from emulated_hx711 import HX711
def cleanAndExit():
print("Cleaning...")
if not EMULATE_HX711:
GPIO.cleanup()
print("Bye!")
sys.exit()
hx = HX711(5, 6)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(1)
hx.reset()
hx.tare()
print("Initialized, put the keg on...")
LED6.blink(2,3)
while True:
try:
keg1 = hx.get_weight(5)
print(keg1)
if ( keg1 < 50000 ):
LED1.blink(.2,.2)
else:
LED1.off()
if ( keg1 < 115000 ):
LED2.blink(.5,.5)
else:
LED2.off()
if ( keg1 > 115000 ):
LED3.on()
else:
LED3.off()
if ( keg1 > 253000 ):
LED4.on()
else:
LED4.off()
if ( keg1 > 380000 ):
LED5.on()
else:
LED5.off()
if ( keg1 > 500000 ):
LED6.on()
else:
LED6.off()
hx.power_down()
hx.power_up()
time.sleep(0.1)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()Here is the cleaned up "compare_to_average.py" that I am trying to incorporate. I am getting bombarded with indention errors and it keeps stopping. What do I keep of this and where do I put it?compare_to_average.py is this:
import time
def moving_average(prev_average, new_value, num_steps = 20.0):
return (prev_average * (num_steps - 1) + new_value) / num_steps
average = weight = keg1
w_threshold = 100
while True:
weight = keg1
lost_weight = average - weight
if lost_weight > w_threshold:
print("You got served")
average = moving_average(average, weight)
time.sleep(30) The goal is to print "You got served" when the variable "keg1" decreases in value significantly within a few minutes of time. A pint of beer weighs about 9000 "units" on this scale system. How and where do I merge these two?
