#
import machine
import time
# Informative statement
print("The system will start running once motion is detected by the PIR sensor. Please simulate motion if necessary.")
# Initialize Pins
led_indicator = machine.Pin(1, machine.Pin.OUT) # LED indicator for toll operator input
pir_sensor = machine.Pin(27, machine.Pin.IN) # PIR sensor for motion detection
bike_button = machine.Pin(13, machine.Pin.IN, pull=machine.Pin.PULL_DOWN) # Button for bike detection
car_button = machine.Pin(14, machine.Pin.IN, pull=machine.Pin.PULL_DOWN) # Button for car detection
truck_button = machine.Pin(15, machine.Pin.IN, pull=machine.Pin.PULL_DOWN) # Button for truck detection
# Price list for vehicles (index: 0 = bike, 1 = car, 2 = truck)
toll_prices = (9.50, 18.75, 37.00)
# Initialize vehicle counters
vehicle_counts = {'Bike': 0, 'Car': 0, 'Truck': 0}
def calculate_fees():
"""Calculate total fees based on vehicle counts."""
return {
'Bike': vehicle_counts['Bike'] * toll_prices[0],
'Car': vehicle_counts['Car'] * toll_prices[1],
'Truck': vehicle_counts['Truck'] * toll_prices[2]
}
def display_totals(fees, total_fee):
"""Print the total fees and counts in a user-friendly format."""
print(f"\nFees:\n- Bike: R{fees['Bike']:.2f}\n- Car: R{fees['Car']:.2f}\n- Truck: R{fees['Truck']:.2f}")
print(f"Total Fee: R{total_fee:.2f}")
def reset_vehicle_counts():
"""Reset vehicle counts to zero."""
return {'Bike': 0, 'Car': 0, 'Truck': 0}
def save_report(fees, total_fee):
"""Write the report to a text file."""
with open("report.txt", 'w') as file:
file.write(f"Total number of vehicles: {sum(vehicle_counts.values())}\n")
file.write(f"Bikes: {vehicle_counts['Bike']}\n")
file.write(f"Cars: {vehicle_counts['Car']}\n")
file.write(f"Trucks: {vehicle_counts['Truck']}\n")
file.write(f"Fees collected for bikes: R{fees['Bike']:.2f}\n")
file.write(f"Fees collected for cars: R{fees['Car']:.2f}\n")
file.write(f"Fees collected for trucks: R{fees['Truck']:.2f}\n")
file.write(f"Total amount collected for the day: R{total_fee:.2f}\n")
def register_vehicle():
"""Register a vehicle based on the button input."""
while True:
if bike_button.value() == 1:
vehicle_counts['Bike'] += 1
vehicle_type = 'Bike'
fee = fees['Bike']
elif car_button.value() == 1:
vehicle_counts['Car'] += 1
vehicle_type = 'Car'
fee = fees['Car']
elif truck_button.value() == 1:
vehicle_counts['Truck'] += 1
vehicle_type = 'Truck'
fee = fees['Truck']
else:
continue
print(f"\n{vehicle_type} detected!")
print(f"Number of {vehicle_type.lower()}s passed: {vehicle_counts[vehicle_type]}")
print(f"Total amount for {vehicle_type.lower()}s: R{fee:.2f}")
# Indicate registration with LED
led_indicator.on()
time.sleep(2)
led_indicator.off()
time.sleep(2)
break
# Main loop
motion_detected = True
while motion_detected:
if pir_sensor.value() == 1:
while True:
# Calculate fees
fees = calculate_fees()
total_fee = sum(fees.values())
# Menu options
choice = input("\nMenu:\n1. Register Vehicle\n2. Show Totals\n3. End Day\n4. Write Report\n5. Exit Program\n")
if choice == "1":
print("Press the corresponding button for the vehicle type:")
print("- 'B' for Bike")
print("- 'C' for Car")
print("- 'T' for Truck")
register_vehicle()
elif choice == "2":
display_totals(fees, total_fee)
elif choice == "3":
print(f"\nTotal Vehicles:\n- Bikes: {vehicle_counts['Bike']}")
print(f"- Cars: {vehicle_counts['Car']}")
print(f"- Trucks: {vehicle_counts['Truck']}")
print(f"Total Fee Collected: R{total_fee:.2f}")
vehicle_counts = reset_vehicle_counts()
elif choice == "4":
save_report(fees, total_fee)
print("Report has been written to 'report.txt'.")
elif choice == '5':
motion_detected = False
break