-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspberry.py
104 lines (77 loc) · 2.53 KB
/
raspberry.py
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
import RPi.GPIO as GPIO
import time
# GPIO Pins
sensor1_trigger = 17
sensor1_echo = 27
sensor2_trigger = 23
sensor2_echo = 24
sensor3_trigger = 5
sensor3_echo = 6
button_pin = 16
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor1_trigger, GPIO.OUT)
GPIO.setup(sensor1_echo, GPIO.IN)
GPIO.setup(sensor2_trigger, GPIO.OUT)
GPIO.setup(sensor2_echo, GPIO.IN)
GPIO.setup(sensor3_trigger, GPIO.OUT)
GPIO.setup(sensor3_echo, GPIO.IN)
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def measure_distance(trigger_pin, echo_pin):
GPIO.output(trigger_pin, True)
time.sleep(0.00001)
GPIO.output(trigger_pin, False)
pulse_start = time.time()
pulse_end = time.time()
while GPIO.input(echo_pin) == 0:
pulse_start = time.time()
while GPIO.input(echo_pin) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
def save_bill(items, total):
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
file_name = f"bill_{timestamp}.txt"
with open(file_name, "w") as file:
file.write("Inventory Bill\n")
file.write("Timestamp: " + timestamp + "\n\n")
file.write("Items:\n")
for item, quantity in items.items():
file.write(item + " - " + str(quantity) + "\n")
file.write("\nTotal: $" + str(total))
def print_inventory(items):
print("Current Inventory:")
for item, quantity in items.items():
print(item + ": " + str(quantity))
try:
items = {
"Item 1": 0,
"Item 2": 0,
"Item 3": 0
}
print("Inventory Management App")
print("------------------------")
while True:
print("\n1. Scan Inventory")
print("2. Print Inventory")
print("3. Save Bill")
print("4. Quit")
choice = input("Enter your choice: ")
if choice == "1":
# Perform inventory scan
items["Item 1"] = measure_distance(sensor1_trigger, sensor1_echo)
items["Item 2"] = measure_distance(sensor2_trigger, sensor2_echo)
items["Item 3"] = measure_distance(sensor3_trigger, sensor3_echo)
print("Inventory scanned successfully!")
elif choice == "2":
print_inventory(items)
elif choice == "3":
total = sum(items.values())
save_bill(items, total)
print("Bill saved successfully!")
elif choice == "4":
break
except KeyboardInterrupt:
GPIO.cleanup()