from machine import Pin
import time
# ---------------------------------------------------------
# 1. HARDWARE SETUP
# ---------------------------------------------------------
# Button: GP22 to GND (PULL_UP logic)
button = Pin( 22, Pin.IN, Pin.PULL_UP)
# LEDs: GP27 (Red) and GP17 (Green)
red_led = Pin(27, Pin.OUT)
green_led = Pin(17, Pin.OUT)
file_name = "attendance.csv"
# ---------------------------------------------------------
# 2. DRINKS DATABASE (Numeric & Categorical)
# ---------------------------------------------------------
drinks = [
{"Name": "Orange Juice", "Type": "Fruit", "Sugar": 20, "Cals": 120},
{"Name": "Apple Juice", "Type": "Fruit", "Sugar": 25, "Cals": 130},
{"Name": "Green Tea", "Type": "Tea", "Sugar": 0, "Cals": 5},
{"Name": "Coffee", "Type": "Coffee", "Sugar": 2, "Cals": 10},
{"Name": "Chocolate Milk", "Type": "Milk", "Sugar": 22, "Cals": 150},
{"Name": "Water", "Type": "None", "Sugar": 0, "Cals": 0}
]
# ---------------------------------------------------------
# 3. AI & ANALYSIS FUNCTIONS
# ---------------------------------------------------------
# Feature: Analyze popularity from previous logs
def get_popular_drinks():
popularity = {}
try:
with open(file_name, "r") as f:
for line in f:
parts = line.strip().split(",")
if len(parts) >= 4:
# Assuming the 4th column (index 3) is the choice
chosen = parts[3]
popularity[chosen] = popularity.get(chosen, 0) + 1
except:
pass # If file doesn't exist yet
return popularity
# Feature: Multi-factor Recommendation Engine
def smart_recommend(pref_type, health_pref):
pop_data = get_popular_drinks()
recommendations = []
# Normalize inputs for better matching
p_type = pref_type.lower().strip()
h_pref = health_pref.lower().strip()
for d in drinks:
score = 0
reasons = []
# Logic A: Preference Match (+3 pts)
if p_type in d["Type"].lower():
score += 3
reasons.append("Matches your style")
# Logic B: Health Logic (Numeric Analysis) (+3 pts)
if h_pref == "low sugar" and d["Sugar"] < 5:
score += 3
reasons.append("Healthy low-sugar option")
elif h_pref == "low calories" and d["Cals"] < 50:
score += 3
reasons.append("Low-calorie choice")
elif h_pref == "low" and (d["Sugar"] < 5 or d["Cals"] < 50):
score += 2
reasons.append("General healthy pick")
# Logic C: Social Proof (Popularity Boost) (+1 pt)
if d["Name"] in pop_data:
score += 1
reasons.append("Popular among guests")
recommendations.append({
"Name": d["Name"],
"Score": score,
"Reason": " & ".join(reasons) if reasons else "Safe choice"
})
# Sort: Highest score first
recommendations.sort(key=lambda x: x["Score"], reverse=True)
return recommendations[:3]
# ---------------------------------------------------------
# 4. MAIN OPERATIONAL LOOP
# ---------------------------------------------------------
print("="*45)
print(" SMART AI GUEST SYSTEM - FULL VERSION")
print("="*45)
while True:
# --- STANDBY MODE ---
green_led.value(0)
red_led.value(1)
print("\n[READY] System Waiting... Press the button to start.")
# Wait for button press (Value 0 means connected to GND)
while button.value() == 1:
time.sleep(0.05)
# --- ACTIVE MODE ---
red_led.value(0)
green_led.value(1)
print("\n[EVENT] Guest detected! Initializing AI Analysis...")
# Data Input
name = input(" > Guest Name: ").strip()
p_drink = input(" > Drink Pref (Fruit/Tea/Milk/Coffee): ")
p_health = input(" > Health (Low Sugar/Low Calories/None): ")
# Run AI Logic
results = smart_recommend(p_drink, p_health)
top_pick = results[0]["Name"]
# Save to CSV
try:
with open(file_name, "a") as f:
f.write("{},{},{},{}\n".format(name, p_drink, p_health, top_pick))
print("✅ Data successfully logged to attendance.csv")
except Exception as e:
print("❌ Data Error:", e)
# Display Smart Recommendations
print("\n" + "-"*40)
print("AI TOP RECOMMENDATIONS FOR {}:".format(name.upper()))
for i, res in enumerate(results, 1):
print("{}. {} (Score: {})".format(i, res["Name"], res["Score"]))
print(" Reason: {}".format(res["Reason"]))
print("-" * 40)
# Finish Animation (Green Blinking)
print("\nProcessing complete. Resetting...")
for _ in range(3):
green_led.value(0); time.sleep(0.2)
green_led.value(1); time.sleep(0.2)
time.sleep(1) # Pause before returning to Red LED