-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighScale.py
More file actions
173 lines (139 loc) · 4.64 KB
/
weighScale.py
File metadata and controls
173 lines (139 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#"module" is not the final name for this interface
#every data generating device (camera, microphone, thermometer)
#should have its own module class which implements setup and
#poll functions
import random
import time
import digitalio
import board
def readCount():
i = 0
Count = 0
DT = Pin(5, Pin.OUT)
DT.high()
DT.low()
DT = Pin(5, Pin.IN)
while DT.value() == 1:
i = 0
for i in range(24):
SCK.high()
Count = Count << 1
SCK.low()
if DT.value() == 1:
Count += 1
SCK.high()
Count = Count ^ 0x800000
SCK.low()
return Count
#dummy class representing a generic sensor, not required
class HX711():
def read_data(self):
i = 0
Count = 0
DT.direction = digitalio.Direction.OUTPUT
DT.value = 1
DT.value = 0
DT.direction = digitalio.Direction.INPUT
while DT.value == 1:
i = 0
for i in range(24):
SCK.value = 1
Count = Count << 1
SCK.value = 0
if DT.value == 1:
Count += 1
SCK.value = 1
Count = Count ^ 0x800000
SCK.value = 0
return Count
class weighScale():
name = "Weight Scale" #put the name of your sensor/project here
sensor = HX711()
polling_interval = .5 #time between polls in seconds
last_poll = time.monotonic()
debug = False
def __init__(self):
print(f"instance of {self.name} created")
def setup(self):
SCK = digitalio.DigitalInOut(board.GP19)
SCK.direction = digitalio.Direction.OUTPUT
LED = digitalio.DigitalInOut(board.GP20)
LED.direction = digitalio.Direction.OUTPUT
LED.value = 0
DT = digitalio.DigitalInOut(board.GP18)
a = -4.482101*10**(-5)
b = 381.3089
switch = digitalio.DigitalInOut(board.GP21)
switch.switch_to_input(pull=digitalio.Pull.DOWN)
state = 1
print("Flip switch to start.")
while (state == 1):
if switch.value == True:
print("Remove any objects on the scale.")
while (switch.value == True):
LED.value = 1
time.sleep(0.15)
LED.value = 0
time.sleep(0.15)
state = 0
break
time.sleep(0.15)
while (state == 0):
LED.value = 0
if switch.value == True:
# Read zero weight data here
print("Place weight on scale.")
while (switch.value == True):
LED.value = 1
time.sleep(0.15)
LED.value = 0
time.sleep(0.15)
state = 1
break
time.sleep(0.15)
while (state == 1):
LED.value = 1
if switch.value == True:
# Read calibration weight and calculate factor
print("Flip switch to continue.")
while (switch.value == True):
LED.value = 1
time.sleep(0.15)
LED.value = 0
time.sleep(0.15)
state = 0
break
time.sleep(0.15)
sampling_interval = 5
weights = []
LED.value = 0
#don't need to modify this
def wrap_poll(self):
if ((time.monotonic() - self.last_poll) > self.polling_interval):
self.last_poll = time.monotonic()
return self.poll()
return None
def poll(self):
#data_file.write("Date, Time, Weight\n")
a = -4.482101*10**(-5)
b = 381.3089
sampling_interval = 5
weight = readCount()
weight = a * weight + b
result = round(weight, 3)
weights.append(result)
print("weight:", weight)
if len(weights) == sampling_interval:
weights.sort()
weight = weights[sampling_interval // 2]
timestring = time.localtime()
month = timestring[1]
day = timestring[2]
hour = timestring[3]
minute = timestring[4]
sec = timestring[5]
timestring1 = str(month) + "/" + str(day) + "," + str(hour) + ":" + str(minute) + ":" + str(sec)
#data_file.write(timestring1 + "," + str(weight) + "\n")
#data_file.flush()
weights = []
return timestring1 + "," + str(weight) + "\n"