from machine import Pin, PWM, I2C
from ssd1306 import SSD1306_I2C
# Define OLED display
i2c = I2C(scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
# Define LED, buzzer, and servo motor pins
led = Pin(23, Pin.OUT)
buzzer = PWM(Pin(18), 1000)
servo = PWM(Pin(5), 50)
# Define product data
products = {
1: {"name": "Coke"},
2: {"name": "Chips"},
3: {"name": "Candy"},
}
# Define global variables
current_product = None
inserted_amount = 0.0
def clear_screen():
oled.fill(0)
oled.show()
def display_welcome():
clear_screen()
oled.text("Welcome to the Vending Machine!", 0, 0)
oled.text("Please select a product:", 0, 16)
for i, product in enumerate(products):
oled.text(f"{i+1}. {products[product]['name']}", 0, 32 + (i * 8))
oled.show()
def select_product(product_id):
global current_product
current_product = product_id
clear_screen()
oled.text(f"You selected {products[current_product]['name']}.", 0, 0)
oled.text("Please insert coins.", 0, 16)
oled.show()
def insert_coin(coin_value):
global inserted_amount
inserted_amount += coin_value
clear_screen()
oled.text(f"Inserted amount: ${inserted_amount:.2f}", 0, 0)
# Check if inserted amount is sufficient
if inserted_amount >= get_price(current_product):
oled.text("Sufficient amount inserted.", 0, 16)
else:
oled.text(f"Remaining amount: ${get_price(current_product) - inserted_amount:.2f}", 0, 16)
oled.show()
def get_price(product_id):
# Implement your logic to retrieve product price from external source (e.g., database, file)
# This example assumes a static price list
return {
1: 1.5,
2: 1.0,
3: 0.5,
}[product_id]
def dispense_product():
global inserted_amount
# Turn on servo motor to dispense product
servo.duty(50)
time.sleep(1)
servo.duty(0)
# Play sound
buzzer.duty(1023)
time.sleep(0.2)
buzzer.duty(0)
# Turn on LED
led.on()
time.sleep(0.5)
led.off()
# Return change
change = inserted_amount - get_price(current_product)
if change > 0:
# TODO: Implement coin dispenser to return change
print(f"Returning change: ${change:.2f}")
inserted_amount = 0.0
current_product = None
display_welcome()
def main():
display_welcome()
while True:
# Read input from user
# TODO: Implement user input using buttons or other interface
user_input = int(input("Enter your selection: "))
if 1 <= user_input <= len(products):
select_product(user_input)
elif user_input == 5:
insert_coin(0.25)
elif user_input == 10:
insert_coin(0.10)
elif user_input == 12:
dispenser_product()
else:
print("Invalid selection.")
if _name_ == "_main_":
main()