66from digitalio import DigitalInOut , Direction , Pull
77from adafruit_ht16k33 .segments import Seg14x4
88from cedargrove_nau7802 import NAU7802
9- from callibration import callibration
9+ from calibration import calibration
1010
1111# I2C setup with STEMMA port
1212i2c = board .STEMMA_I2C ()
1313# alphanumeric segment displpay setup
1414# using two displays together
15- display = Seg14x4 (i2c , address = (0x71 , 0x70 ))
15+ display = Seg14x4 (i2c , address = (0x70 , 0x71 ))
1616# start-up text
1717display .print ("*HELLO* " )
1818# button LEDs
19- blue = DigitalInOut (board .A0 )
19+ blue = DigitalInOut (board .A1 )
2020blue .direction = Direction .OUTPUT
21- green = DigitalInOut (board .A2 )
21+ green = DigitalInOut (board .A3 )
2222green .direction = Direction .OUTPUT
2323# buttons setup
24- blue_btn = DigitalInOut (board .A1 )
24+ blue_btn = DigitalInOut (board .A0 )
2525blue_btn .direction = Direction .INPUT
2626blue_btn .pull = Pull .UP
27- green_btn = DigitalInOut (board .A3 )
27+ green_btn = DigitalInOut (board .A2 )
2828green_btn .direction = Direction .INPUT
2929green_btn .pull = Pull .UP
3030# nau7802 setup
@@ -70,15 +70,15 @@ def find_average(num):
7070 count = count + n
7171 average = count / len (num )
7272 return average
73- # callibration function
74- def calculateCallibration (array ):
73+ # calibration function
74+ def calculateCalibration (array ):
7575 for _ in range (10 ):
7676 blue .value = True
7777 green .value = False
7878 nau7802 .channel = 1
7979 #value = read_raw_value()
80- print ("channel %1.0f raw value: %7.0f" % (nau7802 .channel , read_raw_value ()))
81- array .append (read_raw_value ())
80+ print ("channel %1.0f raw value: %7.0f" % (nau7802 .channel , abs ( read_raw_value () )))
81+ array .append (abs ( read_raw_value () ))
8282 blue .value = False
8383 green .value = True
8484 time .sleep (1 )
@@ -103,26 +103,44 @@ def blink(led, amount, count):
103103# zeroing each channel
104104nau7802 .channel = 1
105105zero_channel () # Calibrate and zero channel
106- nau7802 .channel = 2
107- zero_channel () # Calibrate and zero channel
108106display .fill (0 )
109107display .print ("STARTING" )
110108
111109# variables and states
112110clock = time .monotonic () # time.monotonic() device
111+ reset_clock = time .monotonic ()
112+ long_clock = time .monotonic ()
113113mode = "run"
114114mode_names = ["SHOW OZ?" , " GRAMS?" , " ZERO?" , "CALIBRTE" , " OFFSET?" ]
115- #offset_val = callibration['offset_val']
116115stage = 0
116+ zero_stage = 0
117117weight_avg = 0
118118zero_avg = 0
119119show_oz = True
120120show_grams = False
121121zero_out = False
122- callibrate_mode = False
122+ calibrate_mode = False
123123blue_btn_pressed = False
124124green_btn_pressed = False
125125run_mode = True
126+ avg_read = []
127+ values = []
128+ val_offset = 0
129+ avg_values = []
130+
131+ for w in range (5 ):
132+ nau7802 .channel = 1
133+ value = read_raw_value ()
134+ # takes value reading and divides with by the offset value
135+ # to get the weight in grams
136+ grams = value / calibration ['offset_val' ]
137+ avg_read .append (grams )
138+ if len (avg_read ) > 4 :
139+ the_avg = find_average (avg_read )
140+ oz = the_avg / 28.35
141+ display .print (" %0.1f oz" % oz )
142+ avg_read .clear ()
143+ time .sleep (1 )
126144
127145while True :
128146 # button debouncing
@@ -136,22 +154,35 @@ def blink(led, amount, count):
136154 if run_mode is True and (time .monotonic () - clock ) > 2 :
137155 nau7802 .channel = 1
138156 value = read_raw_value ()
157+ print (value )
158+ value = abs (value ) - val_offset
159+ print (value )
160+ #value = abs(value)
161+ values .append (value )
139162 # takes value reading and divides with by the offset value
140163 # to get the weight in grams
141- grams = value / callibration ['offset_val' ]
142- # convert grams to ounces
164+ grams = value / calibration ['offset_val' ]
143165 oz = grams / 28.35
144- # display in ounces (default)
145166 if show_oz is True :
146- if oz < 0 :
147- oz = 0
148- display .print (" %0.2f oz" % oz )
149- # display in grams
167+ # append reading
168+ avg_read .append (oz )
169+ label = "oz"
150170 if show_grams is True :
151- if grams < 0 :
152- grams = 0
153- display .print (" %0.2f g" % grams )
171+ avg_read .append (grams )
172+ label = "g"
173+ print (avg_read )
174+ if len (avg_read ) > 10 :
175+ the_avg = find_average (avg_read )
176+ display .print (" %0.1f %s" % (the_avg , label ))
177+ avg_read .clear ()
178+ val_offset += 10
154179 clock = time .monotonic ()
180+ if (time .monotonic () - reset_clock ) > 43200 :
181+ run_mode = False
182+ show_oz = False
183+ show_grams = False
184+ zero_out = True
185+ reset_clock = time .monotonic ()
155186 # if you press the change mode button
156187 if (not green_btn .value and not green_btn_pressed ) and run_mode :
157188 green .value = True
@@ -177,33 +208,37 @@ def blink(led, amount, count):
177208 if (not blue_btn .value and not blue_btn_pressed ) and mode == 0 :
178209 # show_oz is set as the state
179210 show_oz = True
211+ label = "oz"
180212 blue .value = False
181213 # goes back to weighing mode
182- run_mode = True
183214 mode = "run"
184215 blue_btn_pressed = True
216+ display .print (" %0.1f %s" % (the_avg , label ))
217+ run_mode = True
185218 # if you select show_grams
186219 if (not blue_btn .value and not blue_btn_pressed ) and mode == 1 :
187220 # show_grams is set as the state
188221 show_grams = True
222+ label = "g"
189223 blue .value = False
190224 # goes back to weighing mode
191- run_mode = True
192225 mode = "run"
193226 blue_btn_pressed = True
227+ display .print (" %0.1f %s" % (the_avg , label ))
228+ run_mode = True
194229 # if you select zero_out
195230 if (not blue_btn .value and not blue_btn_pressed ) and mode == 2 :
196231 # zero_out is set as the state
197- # can zero out the scale without full recallibration
232+ # can zero out the scale without full recalibration
198233 zero_out = True
199234 blue .value = False
200235 mode = "run"
201236 blue_btn_pressed = True
202- # if you select callibrate_mode
237+ # if you select calibrate_mode
203238 if (not blue_btn .value and not blue_btn_pressed ) and mode == 3 :
204- # callibrate_mode is set as the state
205- # starts up the callibration process
206- callibrate_mode = True
239+ # calibrate_mode is set as the state
240+ # starts up the calibration process
241+ calibrate_mode = True
207242 blue .value = False
208243 mode = "run"
209244 blue_btn_pressed = True
@@ -212,34 +247,47 @@ def blink(led, amount, count):
212247 # displays the curren offset value stored in the code
213248 blue .value = False
214249 display .fill (0 )
215- display .print ("%0.4f" % callibration ['offset_val' ])
250+ display .print ("%0.4f" % calibration ['offset_val' ])
216251 time .sleep (5 )
217252 mode = "run"
218253 # goes back to weighing mode
219- run_mode = True
220254 show_oz = True
255+ label = "oz"
256+ display .print (" %0.1f %s" % (the_avg , label ))
257+ run_mode = True
221258 blue_btn_pressed = True
222259 # if the zero_out state is true
223- if zero_out and mode == "run" :
260+ if zero_out and zero_stage == 0 :
261+ blue_btn_pressed = True
262+ # clear the scale for zeroing
263+ display .fill (0 )
264+ display .print ("REMOVE " )
265+ zero_stage = 1
266+ blue .value = True
267+ green .value = True
268+ if (not blue_btn .value and not blue_btn_pressed ) and zero_stage == 1 :
269+ green .value = False
224270 # updates display
225271 display .fill (0 )
226272 display .print ("ZEROING" )
227273 blue .value = False
228274 # runs zero_channel() function on both channels
229275 nau7802 .channel = 1
230276 zero_channel ()
231- nau7802 .channel = 2
232- zero_channel ()
233277 display .fill (0 )
234278 display .print ("ZEROED " )
235279 zero_out = False
280+ zero_stage = 0
236281 # goes into weighing mode
282+ val_offset = 0
237283 run_mode = True
238284 show_oz = True
239- # the callibration process
285+ label = "oz"
286+ display .print (" %0.1f %s" % (the_avg , label ))
287+ # the calibration process
240288 # each step is counted in stage
241289 # blue button is pressed to advance to the next stage
242- if callibrate_mode is True and stage == 0 :
290+ if calibrate_mode is True and stage == 0 :
243291 blue_btn_pressed = True
244292 # clear the scale for zeroing
245293 display .fill (0 )
@@ -255,8 +303,6 @@ def blink(led, amount, count):
255303 blue .value = False
256304 nau7802 .channel = 1
257305 zero_channel ()
258- nau7802 .channel = 2
259- zero_channel ()
260306 display .fill (0 )
261307 display .print ("ZEROED " )
262308 stage = 2
@@ -271,15 +317,15 @@ def blink(led, amount, count):
271317 display .print ("AVG ZERO" )
272318 # runs the calculateCallibration function
273319 # takes 10 raw readings, stores them into an array and gets an average
274- zero_avg = calculateCallibration (zero_readings )
320+ zero_avg = calculateCalibration (zero_readings )
275321 stage = 3
276322 display .fill (0 )
277323 display .print ("DONE" )
278324 blue .value = True
279325 # stage 4
280326 if (not blue_btn .value and not blue_btn_pressed ) and stage == 3 :
281327 # place the known weight item
282- # item's weight matches callibration ['weight'] in grams
328+ # item's weight matches calibration ['weight'] in grams
283329 blue_btn_pressed = True
284330 blue .value = False
285331 display .fill (0 )
@@ -294,21 +340,26 @@ def blink(led, amount, count):
294340 display .print ("WEIGHING" )
295341 weight_readings = []
296342 # weighs the item 10 times, stores the readings in an array & averages them
297- weight_avg = calculateCallibration (weight_readings )
343+ weight_avg = calculateCalibration (weight_readings )
298344 # calculates the new offset value
299- callibration ['offset_val' ] = (weight_avg - zero_avg ) / callibration ['weight' ]
300- display .marquee ("%0.2f - CALLIBRATED " % callibration ['offset_val' ], 0.5 , False )
345+ calibration ['offset_val' ] = (weight_avg - zero_avg ) / calibration ['weight' ]
346+ display .marquee ("%0.2f - CALIBRATED " % calibration ['offset_val' ], 0.3 , False )
301347 stage = 5
302348 display .fill (0 )
303349 display .print ("DONE" )
304350 blue .value = True
305351 # final stage
306352 if (not blue_btn .value and not blue_btn_pressed ) and stage == 5 :
307353 blue_btn_pressed = True
308- callibrate_mode = False
354+ zero_readings .clear ()
355+ weight_readings .clear ()
356+ calibrate_mode = False
309357 blue .value = False
310358 # goes back into weighing mode
311359 show_oz = True
360+ label = "oz"
361+ display .print (" %0.1f %s" % (the_avg , label ))
362+ val_offset = 0
312363 run_mode = True
313364 # resets stage
314365 stage = 0
0 commit comments