Updating code and calibration

Fixing all misspellings of calibration. Also tweaks to code after running long-term with larger weight.
This commit is contained in:
BlitzCityDIY 2022-07-13 09:52:47 -04:00
parent dc3ec4bafa
commit 7cf6458cf3
3 changed files with 104 additions and 53 deletions

View file

@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
calibration = {
'offset_val' : 29.06,
'weight' : 30 # in grams
}

View file

@ -1,7 +0,0 @@
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
callibration = {
'offset_val' : 60.8006, # default offset for 6 lbs
'weight' : 2721.55 # 6 lbs in grams
}

View file

@ -6,25 +6,25 @@ import board
from digitalio import DigitalInOut, Direction, Pull from digitalio import DigitalInOut, Direction, Pull
from adafruit_ht16k33.segments import Seg14x4 from adafruit_ht16k33.segments import Seg14x4
from cedargrove_nau7802 import NAU7802 from cedargrove_nau7802 import NAU7802
from callibration import callibration from calibration import calibration
# I2C setup with STEMMA port # I2C setup with STEMMA port
i2c = board.STEMMA_I2C() i2c = board.STEMMA_I2C()
# alphanumeric segment displpay setup # alphanumeric segment displpay setup
# using two displays together # using two displays together
display = Seg14x4(i2c, address=(0x71, 0x70)) display = Seg14x4(i2c, address=(0x70, 0x71))
# start-up text # start-up text
display.print("*HELLO* ") display.print("*HELLO* ")
# button LEDs # button LEDs
blue = DigitalInOut(board.A0) blue = DigitalInOut(board.A1)
blue.direction = Direction.OUTPUT blue.direction = Direction.OUTPUT
green = DigitalInOut(board.A2) green = DigitalInOut(board.A3)
green.direction = Direction.OUTPUT green.direction = Direction.OUTPUT
# buttons setup # buttons setup
blue_btn = DigitalInOut(board.A1) blue_btn = DigitalInOut(board.A0)
blue_btn.direction = Direction.INPUT blue_btn.direction = Direction.INPUT
blue_btn.pull = Pull.UP blue_btn.pull = Pull.UP
green_btn = DigitalInOut(board.A3) green_btn = DigitalInOut(board.A2)
green_btn.direction = Direction.INPUT green_btn.direction = Direction.INPUT
green_btn.pull = Pull.UP green_btn.pull = Pull.UP
# nau7802 setup # nau7802 setup
@ -70,15 +70,15 @@ def find_average(num):
count = count + n count = count + n
average = count / len(num) average = count / len(num)
return average return average
# callibration function # calibration function
def calculateCallibration(array): def calculateCalibration(array):
for _ in range(10): for _ in range(10):
blue.value = True blue.value = True
green.value = False green.value = False
nau7802.channel = 1 nau7802.channel = 1
#value = read_raw_value() #value = read_raw_value()
print("channel %1.0f raw value: %7.0f" % (nau7802.channel, read_raw_value())) print("channel %1.0f raw value: %7.0f" % (nau7802.channel, abs(read_raw_value())))
array.append(read_raw_value()) array.append(abs(read_raw_value()))
blue.value = False blue.value = False
green.value = True green.value = True
time.sleep(1) time.sleep(1)
@ -103,26 +103,44 @@ time.sleep(3)
# zeroing each channel # zeroing each channel
nau7802.channel = 1 nau7802.channel = 1
zero_channel() # Calibrate and zero channel zero_channel() # Calibrate and zero channel
nau7802.channel = 2
zero_channel() # Calibrate and zero channel
display.fill(0) display.fill(0)
display.print("STARTING") display.print("STARTING")
# variables and states # variables and states
clock = time.monotonic() # time.monotonic() device clock = time.monotonic() # time.monotonic() device
reset_clock = time.monotonic()
long_clock = time.monotonic()
mode = "run" mode = "run"
mode_names = ["SHOW OZ?", " GRAMS?", " ZERO?", "CALIBRTE", " OFFSET?"] mode_names = ["SHOW OZ?", " GRAMS?", " ZERO?", "CALIBRTE", " OFFSET?"]
#offset_val = callibration['offset_val']
stage = 0 stage = 0
zero_stage = 0
weight_avg = 0 weight_avg = 0
zero_avg = 0 zero_avg = 0
show_oz = True show_oz = True
show_grams = False show_grams = False
zero_out = False zero_out = False
callibrate_mode = False calibrate_mode = False
blue_btn_pressed = False blue_btn_pressed = False
green_btn_pressed = False green_btn_pressed = False
run_mode = True run_mode = True
avg_read = []
values = []
val_offset = 0
avg_values = []
for w in range(5):
nau7802.channel = 1
value = read_raw_value()
# takes value reading and divides with by the offset value
# to get the weight in grams
grams = value / calibration['offset_val']
avg_read.append(grams)
if len(avg_read) > 4:
the_avg = find_average(avg_read)
oz = the_avg / 28.35
display.print(" %0.1f oz" % oz)
avg_read.clear()
time.sleep(1)
while True: while True:
# button debouncing # button debouncing
@ -136,22 +154,35 @@ while True:
if run_mode is True and (time.monotonic() - clock) > 2: if run_mode is True and (time.monotonic() - clock) > 2:
nau7802.channel = 1 nau7802.channel = 1
value = read_raw_value() value = read_raw_value()
print(value)
value = abs(value) - val_offset
print(value)
#value = abs(value)
values.append(value)
# takes value reading and divides with by the offset value # takes value reading and divides with by the offset value
# to get the weight in grams # to get the weight in grams
grams = value / callibration['offset_val'] grams = value / calibration['offset_val']
# convert grams to ounces
oz = grams / 28.35 oz = grams / 28.35
# display in ounces (default)
if show_oz is True: if show_oz is True:
if oz < 0: # append reading
oz = 0 avg_read.append(oz)
display.print(" %0.2f oz" % oz) label = "oz"
# display in grams
if show_grams is True: if show_grams is True:
if grams < 0: avg_read.append(grams)
grams = 0 label = "g"
display.print(" %0.2f g" % grams) print(avg_read)
if len(avg_read) > 10:
the_avg = find_average(avg_read)
display.print(" %0.1f %s" % (the_avg, label))
avg_read.clear()
val_offset += 10
clock = time.monotonic() clock = time.monotonic()
if (time.monotonic() - reset_clock) > 43200:
run_mode = False
show_oz = False
show_grams = False
zero_out = True
reset_clock = time.monotonic()
# if you press the change mode button # if you press the change mode button
if (not green_btn.value and not green_btn_pressed) and run_mode: if (not green_btn.value and not green_btn_pressed) and run_mode:
green.value = True green.value = True
@ -177,33 +208,37 @@ while True:
if (not blue_btn.value and not blue_btn_pressed) and mode == 0: if (not blue_btn.value and not blue_btn_pressed) and mode == 0:
# show_oz is set as the state # show_oz is set as the state
show_oz = True show_oz = True
label = "oz"
blue.value = False blue.value = False
# goes back to weighing mode # goes back to weighing mode
run_mode = True
mode = "run" mode = "run"
blue_btn_pressed = True blue_btn_pressed = True
display.print(" %0.1f %s" % (the_avg, label))
run_mode = True
# if you select show_grams # if you select show_grams
if (not blue_btn.value and not blue_btn_pressed) and mode == 1: if (not blue_btn.value and not blue_btn_pressed) and mode == 1:
# show_grams is set as the state # show_grams is set as the state
show_grams = True show_grams = True
label = "g"
blue.value = False blue.value = False
# goes back to weighing mode # goes back to weighing mode
run_mode = True
mode = "run" mode = "run"
blue_btn_pressed = True blue_btn_pressed = True
display.print(" %0.1f %s" % (the_avg, label))
run_mode = True
# if you select zero_out # if you select zero_out
if (not blue_btn.value and not blue_btn_pressed) and mode == 2: if (not blue_btn.value and not blue_btn_pressed) and mode == 2:
# zero_out is set as the state # zero_out is set as the state
# can zero out the scale without full recallibration # can zero out the scale without full recalibration
zero_out = True zero_out = True
blue.value = False blue.value = False
mode = "run" mode = "run"
blue_btn_pressed = True blue_btn_pressed = True
# if you select callibrate_mode # if you select calibrate_mode
if (not blue_btn.value and not blue_btn_pressed) and mode == 3: if (not blue_btn.value and not blue_btn_pressed) and mode == 3:
# callibrate_mode is set as the state # calibrate_mode is set as the state
# starts up the callibration process # starts up the calibration process
callibrate_mode = True calibrate_mode = True
blue.value = False blue.value = False
mode = "run" mode = "run"
blue_btn_pressed = True blue_btn_pressed = True
@ -212,15 +247,26 @@ while True:
# displays the curren offset value stored in the code # displays the curren offset value stored in the code
blue.value = False blue.value = False
display.fill(0) display.fill(0)
display.print("%0.4f" % callibration['offset_val']) display.print("%0.4f" % calibration['offset_val'])
time.sleep(5) time.sleep(5)
mode = "run" mode = "run"
# goes back to weighing mode # goes back to weighing mode
run_mode = True
show_oz = True show_oz = True
label = "oz"
display.print(" %0.1f %s" % (the_avg, label))
run_mode = True
blue_btn_pressed = True blue_btn_pressed = True
# if the zero_out state is true # if the zero_out state is true
if zero_out and mode == "run": if zero_out and zero_stage == 0:
blue_btn_pressed = True
# clear the scale for zeroing
display.fill(0)
display.print("REMOVE ")
zero_stage = 1
blue.value = True
green.value = True
if (not blue_btn.value and not blue_btn_pressed) and zero_stage == 1:
green.value = False
# updates display # updates display
display.fill(0) display.fill(0)
display.print("ZEROING") display.print("ZEROING")
@ -228,18 +274,20 @@ while True:
# runs zero_channel() function on both channels # runs zero_channel() function on both channels
nau7802.channel = 1 nau7802.channel = 1
zero_channel() zero_channel()
nau7802.channel = 2
zero_channel()
display.fill(0) display.fill(0)
display.print("ZEROED ") display.print("ZEROED ")
zero_out = False zero_out = False
zero_stage = 0
# goes into weighing mode # goes into weighing mode
val_offset = 0
run_mode = True run_mode = True
show_oz = True show_oz = True
# the callibration process label = "oz"
display.print(" %0.1f %s" % (the_avg, label))
# the calibration process
# each step is counted in stage # each step is counted in stage
# blue button is pressed to advance to the next stage # blue button is pressed to advance to the next stage
if callibrate_mode is True and stage == 0: if calibrate_mode is True and stage == 0:
blue_btn_pressed = True blue_btn_pressed = True
# clear the scale for zeroing # clear the scale for zeroing
display.fill(0) display.fill(0)
@ -255,8 +303,6 @@ while True:
blue.value = False blue.value = False
nau7802.channel = 1 nau7802.channel = 1
zero_channel() zero_channel()
nau7802.channel = 2
zero_channel()
display.fill(0) display.fill(0)
display.print("ZEROED ") display.print("ZEROED ")
stage = 2 stage = 2
@ -271,7 +317,7 @@ while True:
display.print("AVG ZERO") display.print("AVG ZERO")
# runs the calculateCallibration function # runs the calculateCallibration function
# takes 10 raw readings, stores them into an array and gets an average # takes 10 raw readings, stores them into an array and gets an average
zero_avg = calculateCallibration(zero_readings) zero_avg = calculateCalibration(zero_readings)
stage = 3 stage = 3
display.fill(0) display.fill(0)
display.print("DONE") display.print("DONE")
@ -279,7 +325,7 @@ while True:
# stage 4 # stage 4
if (not blue_btn.value and not blue_btn_pressed) and stage == 3: if (not blue_btn.value and not blue_btn_pressed) and stage == 3:
# place the known weight item # place the known weight item
# item's weight matches callibration['weight'] in grams # item's weight matches calibration['weight'] in grams
blue_btn_pressed = True blue_btn_pressed = True
blue.value = False blue.value = False
display.fill(0) display.fill(0)
@ -294,10 +340,10 @@ while True:
display.print("WEIGHING") display.print("WEIGHING")
weight_readings = [] weight_readings = []
# weighs the item 10 times, stores the readings in an array & averages them # weighs the item 10 times, stores the readings in an array & averages them
weight_avg = calculateCallibration(weight_readings) weight_avg = calculateCalibration(weight_readings)
# calculates the new offset value # calculates the new offset value
callibration['offset_val'] = (weight_avg-zero_avg) / callibration['weight'] calibration['offset_val'] = (weight_avg-zero_avg) / calibration['weight']
display.marquee("%0.2f - CALLIBRATED " % callibration['offset_val'], 0.5, False) display.marquee("%0.2f - CALIBRATED " % calibration['offset_val'], 0.3, False)
stage = 5 stage = 5
display.fill(0) display.fill(0)
display.print("DONE") display.print("DONE")
@ -305,10 +351,15 @@ while True:
# final stage # final stage
if (not blue_btn.value and not blue_btn_pressed) and stage == 5: if (not blue_btn.value and not blue_btn_pressed) and stage == 5:
blue_btn_pressed = True blue_btn_pressed = True
callibrate_mode = False zero_readings.clear()
weight_readings.clear()
calibrate_mode = False
blue.value = False blue.value = False
# goes back into weighing mode # goes back into weighing mode
show_oz = True show_oz = True
label = "oz"
display.print(" %0.1f %s" % (the_avg, label))
val_offset = 0
run_mode = True run_mode = True
# resets stage # resets stage
stage = 0 stage = 0