#BONGINKOSI RADEBE NKULULEKO
#0210105746080
from machine import Pin
import time
# Define pins
PIR_PIN = Pin(11, Pin.IN)
RED_LED_PIN = Pin(15, Pin.OUT)
GREEN_LED_PIN = Pin(16, Pin.OUT)
BUZZER_PIN = Pin(21, Pin.OUT)
CAR_BUTTON_PIN = Pin(22, Pin.IN, Pin.PULL_UP)
BIKE_BUTTON_PIN = Pin(26, Pin.IN, Pin.PULL_UP)
TRUCK_BUTTON_PIN = Pin(19, Pin.IN, Pin.PULL_UP)
# Vehicle Prices
VEHICLE_PRICES = {'B': 9.50, 'C': 18.75, 'T': 37.00} # Bike, Car, Truck
def initialize_variables():
"""Initialize the counters and toll price list."""
counts = {
'BikeCount': 0,
'CarCount': 0,
'TruckCount': 0
}
return counts
def activate_buzzer():
BUZZER_PIN.value(1)
time.sleep(1)
BUZZER_PIN.value(0)
def register_vehicle(vehicle_type, counts):
"""Update the counter based on vehicle type."""
if vehicle_type == 'B':
counts['BikeCount'] += 1
elif vehicle_type == 'C':
counts['CarCount'] += 1
elif vehicle_type == 'T':
counts['TruckCount'] += 1
else:
print("Invalid vehicle type. Please enter B, C, or T.")
def print_counts(counts):
"""Print the current counts of vehicles and total amount."""
bike_count = counts['BikeCount']
car_count = counts['CarCount']
truck_count = counts['TruckCount']
total_count = bike_count + car_count + truck_count
total_amount = (bike_count * VEHICLE_PRICES['B'] +
car_count * VEHICLE_PRICES['C'] +
truck_count * VEHICLE_PRICES['T'])
print(f"Bike Count: {bike_count}")
print(f"Car Count: {car_count}")
print(f"Truck Count: {truck_count}")
print(f"Total Count of All Vehicles: {total_count}")
print(f"Total Amount: R{total_amount:.2f}")
def end_day(counts):
"""Print the end day report showing total counts and amounts."""
bike_count = counts['BikeCount']
car_count = counts['CarCount']
truck_count = counts['TruckCount']
total_count = bike_count + car_count + truck_count
total_bike_amount = bike_count * VEHICLE_PRICES['B']
total_car_amount = car_count * VEHICLE_PRICES['C']
total_truck_amount = truck_count * VEHICLE_PRICES['T']
total_amount = total_bike_amount + total_car_amount + total_truck_amount
print("\nEnd of Day Report")
print(f"Total Bike Count: {bike_count}")
print(f"Total Car Count: {car_count}")
print(f"Total Truck Count: {truck_count}")
print(f"Total Count of All Vehicles: {total_count}")
print(f"Total Amount for Bikes: R{total_bike_amount:.2f}")
print(f"Total Amount for Cars: R{total_car_amount:.2f}")
print(f"Total Amount for Trucks: R{total_truck_amount:.2f}")
print(f"Grand Total Amount: R{total_amount:.2f}")
def wait_for_motion():
"""Wait for motion to be detected."""
RED_LED_PIN.value(0) # Ensure red LED is off initially
while PIR_PIN.value() == 0:
time.sleep(0.1) # Small delay to prevent busy waiting
RED_LED_PIN.value(1) # Turn on red LED when motion is detected
def display_menu():
"""Display the menu options."""
print("\nMenu\n1.Register vehicle\n2.Show totals\n3.End day\n4.Write report\n5.Exit")
def main():
counts = initialize_variables()
try:
Tolling = True
# Display the menu immediately when the program starts
display_menu()
while Tolling:
# Wait for motion and then turn on the red LED
wait_for_motion()
if PIR_PIN.value() == 1:
activate_buzzer()
user = input("\nEnter choice\n")
if user == "1":
print("Right button truck, middle button car, left button bike")
Vehicle_detected = True
while Vehicle_detected:
if CAR_BUTTON_PIN.value() == 0:
RED_LED_PIN.value(0) # Turn off red LED
register_vehicle('C', counts)
GREEN_LED_PIN.value(1)
time.sleep(2) # Keep green LED on for 2 seconds
GREEN_LED_PIN.value(0)
time.sleep(2) # Debounce delay
Vehicle_detected = False
elif BIKE_BUTTON_PIN.value() == 0:
RED_LED_PIN.value(0) # Turn off red LED
register_vehicle('B', counts)
GREEN_LED_PIN.value(1)
time.sleep(2) # Keep green LED on for 2 seconds
GREEN_LED_PIN.value(0)
time.sleep(2) # Debounce delay
Vehicle_detected = False
elif TRUCK_BUTTON_PIN.value() == 0:
RED_LED_PIN.value(0) # Turn off red LED
register_vehicle('T', counts)
GREEN_LED_PIN.value(1)
time.sleep(2) # Keep green LED on for 2 seconds
GREEN_LED_PIN.value(0)
time.sleep(2) # Debounce delay
Vehicle_detected = False
# Display the menu again after registering a vehicle
display_menu()
elif user == "2":
print_counts(counts)
elif user == "3":
end_day(counts)
elif user == "4":
print("Not available")
elif user == "5":
print("Exiting program.")
break
else:
print("Invalid choice. Please select a valid option.")
except KeyboardInterrupt:
print("Program terminated.")
finally:
pass
# No GPIO cleanup needed for Pico
if __name__== "__main__":
main()