from machine import Pin, I2C
import time
import lcd
# Initialize the LCD
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=400000) # I2C on GPIO 21 (SCL) and GPIO 20 (SDA)
lcd_display = lcd.LCD1602(i2c)
# ATM Class to simulate basic ATM operations
class ATM:
def __init__(self):
self.balance = 1000 # Initial balance
self.pin = "1234" # Default PIN
def check_pin(self, entered_pin):
return entered_pin == self.pin
def check_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
return True
else:
return False
def print_balance(self):
return self.balance
# Keypad Class (Updated)
class Keypad:
def __init__(self, row_pins, col_pins, keymap):
self.rows = [Pin(pin, Pin.OUT) for pin in row_pins] # Set rows as outputs
self.cols = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in col_pins] # Set columns as inputs with pull-up resistors
self.keymap = keymap # Keymap for the 4x4 Keypad
def scan(self):
for row_num, row in enumerate(self.rows):
row.value(0) # Set the row low to scan
for col_num, col in enumerate(self.cols):
if col.value() == 0: # If the key is pressed (low signal)
key = self.keymap[row_num][col_num]
time.sleep(0.2) # Debounce delay
while col.value() == 0: # Wait until the key is released
pass
row.value(1) # Set the row back to high after scanning
return key
row.value(1) # Reset the row to high if no key is pressed
return None # Return None if no key is pressed
# Set up Keypad pins
keypad_pins = {
"rows": [0, 1, 2, 3], # GPIOs for Rows 1, 2, 3, 4
"cols": [4, 5, 6, 7] # GPIOs for Columns 1, 2, 3, 4
}
# Initialize the keypad
keypad_obj = Keypad(keypad_pins["rows"], keypad_pins["cols"], [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
])
# Create the ATM instance
atm = ATM()
# Set up LEDs
green_led = Pin(18, Pin.OUT)
red_led = Pin(19, Pin.OUT)
# Function to read a keypress from the keypad
def get_key():
return keypad_obj.scan()
# Function to enter the PIN
def enter_pin():
entered_pin = ""
while len(entered_pin) < 4:
key = get_key()
if key:
entered_pin += key
time.sleep(0.3) # Debounce delay
return entered_pin
# Function to display balance on LCD
def show_balance():
balance = atm.print_balance()
lcd_display.clear()
lcd_display.putstr(f"Balance: ${balance}")
time.sleep(2) # Display for 2 seconds
# Main ATM Menu
def atm_menu():
while True:
# Show menu options
lcd_display.clear()
lcd_display.putstr("ATM Menu:\n1. Balance\n2. Deposit\n3. Withdraw\n4. Exit")
key = get_key()
if key == '1':
# Check balance
show_balance()
green_led.value(1) # Green LED for success
time.sleep(1)
green_led.value(0)
elif key == '2':
# Deposit
lcd_display.clear()
lcd_display.putstr("Deposit Amt:")
amount = int(input()) # Simulating user input
atm.deposit(amount)
show_balance()
green_led.value(1) # Green LED for success
time.sleep(1)
green_led.value(0)
elif key == '3':
# Withdraw
lcd_display.clear()
lcd_display.putstr("Withdraw Amt:")
amount = int(input()) # Simulating user input
if atm.withdraw(amount):
show_balance()
green_led.value(1) # Green LED for success
time.sleep(1)
green_led.value(0)
else:
lcd_display.clear()
lcd_display.putstr("Insufficient funds.")
red_led.value(1) # Red LED for error
time.sleep(1)
red_led.value(0)
elif key == '4':
lcd_display.clear()
lcd_display.putstr("Thank you!")
time.sleep(2)
break
# Main Program Flow
def main():
lcd_display.clear()
lcd_display.putstr("Welcome to ATM!")
while True:
lcd_display.clear()
lcd_display.putstr("Enter PIN:")
pin = enter_pin()
if atm.check_pin(pin):
lcd_display.clear()
lcd_display.putstr("PIN Accepted.")
time.sleep(1)
atm_menu()
else:
lcd_display.clear()
lcd_display.putstr("Invalid PIN.")
red_led.value(1) # Red LED for error
time.sleep(1)
red_led.value(0)
# Start the ATM system
main()
Loading
pi-pico-w
pi-pico-w