from machine import Pin, I2C
import ssd1306 # Make sure ssd1306.py is uploaded to your device
import time
# --- CONFIGURATION ---
# I2C Pins (Using GPIO 17 & 18)
I2C_SDA = 17
I2C_SCL = 18
oled_width = 128
oled_height = 64
OLED_ADDR = 0x3C # Try 0x3D if display doesn't work
# Switch Pins (Input Pull-Up)
SW1_PIN = 14
SW2_PIN = 15
SW3_PIN = 16
# --- INITIALIZATION ---
# Set up I2C with custom pins
i2c = I2C(0, sda=Pin(I2C_SDA), scl=Pin(I2C_SCL), freq=400000)
# Initialize OLED (128x64)
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c, addr=OLED_ADDR)
# Configure switches with internal pull-ups
sw1 = Pin(SW1_PIN, Pin.IN, Pin.PULL_UP)
sw2 = Pin(SW2_PIN, Pin.IN, Pin.PULL_UP)
sw3 = Pin(SW3_PIN, Pin.IN, Pin.PULL_UP)
# --- HELPER FUNCTIONS ---
def show_message(line1, line2):
"""Display two-line message on OLED and print to console"""
oled.fill(0) # Clear display
oled.text(line1, 0, 10) # Line 1 (Y=10px)
oled.text(line2, 0, 30) # Line 2 (Y=30px)
oled.show()
print(f"Display: {line1} | {line2}") # Serial output
def wait_button_release(pin):
"""Wait for button to be released (debounce)"""
while pin.value() == 0: # While still pressed (active-low)
time.sleep_ms(20)
# --- MAIN PROGRAM ---
# Startup message
show_message("System Ready", "Press any button")
while True:
# Check Switch 1 (Active LOW)
if sw1.value() == 0:
show_message("Button 1 active", "Sensor 1 activated")
wait_button_release(sw1)
# Check Switch 2
elif sw2.value() == 0:
show_message("Button 2 active", "Sensor 2 activated")
wait_button_release(sw2)
# Check Switch 3
elif sw3.value() == 0:
show_message("Button 3 active", "Sensor 3 activated")
wait_button_release(sw3)
# Small delay to reduce CPU usage
time.sleep_ms(50)